如何让If语句在Django项目中起作用?

wbgh16ku  于 2023-02-20  发布在  Go
关注(0)|答案(1)|浏览(109)

if语句在我看来是正确的,但是当我保存并执行服务器时,打印出来的信息是不正确的。几乎就像信息没有被拾取一样。我在模型或视图中遗漏了什么吗?或者我必须添加到if语句中以允许它看到我试图让它发布的数据吗?有人能指出我遗漏了什么吗?

HTML code:
{% if leads.lead_status == "Active Deal" %}
    {% for lead in leads %}
        <tr>
            <td data-href="{% url 'leads:lead-detail' lead.pk %}"><i class="far fa-paper-plane"></i> {{ lead.id }}</td>
            <td>{{ lead.first_name|title }} {{ lead.last_name|title }}</td>
            <td>{{ lead.city|title }}</td>
            <td>{{ lead.state|upper }}</td>
            <td data-href="tel:{{ lead.phone }}">{{ lead.phone }} <i class="fas fa-phone-square"></i> </td>
            <td>$34,567.89</td>
            <td> 
                {% if lead.contract_out == "" %}
                --/--/----
                {% else %}
                {{ lead.contract_out }} 
                {% endif %}
            </td>
            <td>
                {% if lead.contract_back == "" %}
                --/--/----
                {% else %}
                {{ lead.contract_back }}
                {% endif %}
            </td>
            <td>
                {% if lead.hearing_date == "" %}
                --/--/----
                {% else %}
                {{ lead.hearing_date }}
                {% endif %}
            </td>
        </tr>
    {% endfor %}
{% endif %}
views.py

class DashboardPageView(LoginRequiredMixin, ListView):
    template_name = 'dashboard.html'
    context_object_name = "leads"

    def get_queryset(self):
        user = self.request.user
        
        queryset = Lead.objects.filter(agent__user=user)
        return queryset
models.py

class Lead(models.Model):
    PROBLEM_LEAD = 'Problem Lead'
    PAY_TOO_FAR = 'Payments Too Far'
    PAY_TOO_SMALL = 'Payments Too Small'
    NPI = 'No Payment Information'
    ACTIVE = 'Active Deal'
    NO_DEAL = 'No Deal'

    choices_lead_status = [
    (PROBLEM_LEAD, 'Problem Lead'),
    (PAY_TOO_FAR,'Payments Too Far'),
    (PAY_TOO_SMALL,'Payments Too Small'),
    (NPI,'No Payment Information'),
    (ACTIVE,'Active Deal'),
    (NO_DEAL,'No Deal')
    ]

    choices_lead_owner = [
        ("Settlement, New",'Settlement, New'),
        ("Lead, Sales",'Lead, Sales'),
        # ("REPS",'Doe, John')
    ]

    # pill one: Client details
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    dob = models.CharField(max_length=10, blank=True)
    phone = models.CharField(max_length=12, blank=True)
    secondary_phone = models.CharField(max_length=12, blank=True)
    email = models.EmailField(max_length=100, blank=True)
    address = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=20, blank=True)
    state = models.CharField(max_length=2, blank=True)
    zip = models.CharField(max_length=10, blank=True)
    notes = models.CharField(max_length=50000, blank=True)
    lead_status = models.CharField(max_length=100, choices=choices_lead_status, blank=True)
    lead_owner = models.CharField(max_length=100, choices=choices_lead_owner, blank=True)
    agent = models.ForeignKey(Agent, null=True, on_delete=models.SET_NULL, blank=True)
    contract_out = models.CharField(max_length=10, blank=True)
    contract_back = models.CharField(max_length=10, blank=True)
    hearing_date = models.CharField(max_length=10, blank=True)
6ss1mwsb

6ss1mwsb1#

你不能检查查询集内的元素是否有某个值或者其他什么,你应该遍历查询集的元素,然后检查每个元素是否有那个状态。
HTML代码:

#Remove the {% if leads.lead_status == "Active Deal" %}  line!
    {% for lead in leads %}
        {% if lead.lead_status == "Active Deal" %} #this checks if the lead has that lead_status
        <tr>
            <td data-href="{% url 'leads:lead-detail' lead.pk %}"><i class="far fa-paper-plane"></i> {{ lead.id }}</td>
            <td>{{ lead.first_name|title }} {{ lead.last_name|title }}</td>
            <td>{{ lead.city|title }}</td>
            <td>{{ lead.state|upper }}</td>
            <td data-href="tel:{{ lead.phone }}">{{ lead.phone }} <i class="fas fa-phone-square"></i> </td>
            <td>$34,567.89</td>
            <td> 
                {% if lead.contract_out == "" %}
                --/--/----
                {% else %}
                {{ lead.contract_out }} 
                {% endif %}
            </td>
            <td>
                {% if lead.contract_back == "" %}
                --/--/----
                {% else %}
                {{ lead.contract_back }}
                {% endif %}
            </td>
            <td>
                {% if lead.hearing_date == "" %}
                --/--/----
                {% else %}
                {{ lead.hearing_date }}
                {% endif %}
            </td>
        </tr>
        {% endif %} #add this endif
    {% endfor %}
#remove this{% endif %}

相关问题