Django模板中的两个ifequal链

时间:2017-07-23 16:16:18

标签: django python-3.x django-templates

我的问题是在 ifequal 链的最后一个模板中。 在最后一个模板中,我必须根据从两个模板中识别的两个参数来选择模型契约。我认为ifequal链的代码sintax出了问题。每个 ifequal 分离也不起作用。我觉得自己就像在墙内。

1.template:valoare_list.html

 {% for valoare in valoares %}
      <a href="{% url 'valoare_detail' valoare=valoare.tip_valoare %}">
           Contracte de valoare {{ valoare.tip_valoare }}
      </a><br>
 {% endfor %}

2。模板:valoare_detail.html

 {% for tip in tips %}
       <a href="{% url 'contract_list' valoare=valoare.tip_valoare tip=tip.tip_contract %}">
           Tip Contracte de {{ tip.tip_contract }}
       </a><br>
 {% endfor %}
  1. 模板:contract_list.html

    {{ valoare.tip_valoare }} <br> 
    {{ tip.tip_contract }} <br> <br>
    
    {% for contract in contracts %}
        {% ifequal contract.valoare valoare.tip_valoare %}
            {% ifequal contract.tip tip.tip_contract %}
                 {{ contract }} <br>
            {% endifequal %}
        {% endifequal %}
    {% endfor %}
    
  2. models.py

     from django.db import models
    
    
     class Valoare(models.Model):
         VALOARE_CHOICES = (
              ("MICA","mica"),
              ("MARE","mare"),
         )
         tip_valoare = models.CharField(max_length=4, choices=VALOARE_CHOICES, default="MICI", verbose_name='valoarea contractului')
         file_name = models.FileField(null=True, blank=True, upload_to='contracte/templates/contracte/img/', verbose_name='img path')
    
         def __str__(self):
              return self.tip_valoare
    
    
      class Tip(models.Model):
          TIP_CHOICES = (
              ("BUNURI","bunuri"),
              ("SERVICII","servicii"),
              ("LUCRARI","lucrari"),
          )
          tip_contract = models.CharField(max_length=8, choices=TIP_CHOICES, default="BUNURI", verbose_name='tipul contractului')
          file_name = models.FileField(null=True, blank=True, upload_to='contracte/templates/contracte/img/', verbose_name='img path')
    
        def __str__(self):
             return self.tip_contract
    
    
     class Contract(models.Model):
         valoare = models.ForeignKey('Valoare')
         tip = models.ForeignKey('Tip')
         data = models.DateField(blank=True, null=True)
         file_name = models.FileField(null=True, blank=True, upload_to='pdf/', verbose_name='contract file name')
    
         def __str__(self):
             return str(self.data)
    
         class Meta:
             ordering = ["data"]
    

    views.py

     from django.shortcuts import render, get_object_or_404
     from .models import Valoare, Tip, Contract
    
    
     def valoare_list(request):
         valoares = Valoare.objects.all()
         return render(request, 'contracte/valoare_list.html', {'valoares': valoares})
    
    
     def valoare_detail(request, valoare):
         valoare = get_object_or_404(Valoare, tip_valoare=valoare)
         tips = Tip.objects.all()
         return render(request, 'contracte/valoare_detail.html', { 'valoare': valoare, 'tips':tips })
    
    
     def contract_list(request, valoare, tip):
         valoare = get_object_or_404(Valoare, tip_valoare=valoare)
         tip = get_object_or_404(Tip, tip_contract=tip)
         contracts = Contract.objects.all()
         return render(request, 'contracte/contract_list.html', {'valoare': valoare, 'tip': tip, 'contracts':contracts})
    

    urls.py

     from django.conf.urls import url
     from . import views
    
    
     urlpatterns = [
         url(r'^$', views.valoare_list, name='valoare_list'),
         url(r'^valoare/(?P<valoare>[A-Z]{4})/$', views.valoare_detail, name='valoare_detail'),
         url(r'^valoare/(?P<valoare>[A-Z]{4})/(?P<tip>[A-Z]+)/$', views.contract_list, name='contract_list'),
     ]
    

    我无法理解 - 为什么 ifequal 在模板nr.3中不起作用?

1 个答案:

答案 0 :(得分:0)

我在

中管理的最后一个选择

views.py

 def contract_list(request, valoare, tip):
     valoare = get_object_or_404(Valoare, tip_valoare=valoare)
     tip = get_object_or_404(Tip, tip_contract=tip)
     finals = Contract.objects.filter(valoare__tip_valoare=valoare, tip__tip_contract=tip)
     return render(request, 'contracte/contract_list.html', {'valoare': valoare, 'tip': tip, 'finals':finals})

contract_list.html - 他们的模板是:

 {% for final in finals %}
         {{ final }}<br>
 {% endfor %}

决赛变量中我把&#34;双参数&#34;过滤器和QUERY工作完美。我是首发,我觉得自己打开了世界。