模板的if循环中的for语句不起作用。它仍然显示不可用的项目。
这是我模板的代码

form-template.html

{% for field in form %}
{% if eventprojector.projector.available %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">{{ field.errors }} </span>
    </div>
    <label class="control-label col-sm-2">{{ field.label_tag }} </label>
    <div class ="col-sm-10">{{ field }} </div>
    </div>
{%endif %}
{% endfor %}

最佳答案

以下是重构代码,该代码根据您的代码适用于可用和不可用的项目:

event / models.py:

from django.db import models

# Create your models here.
class Projectors(models.Model):
    pro = models.CharField(max_length=50)
    available = models.BooleanField(default=True)
    def __str__(self):
        return self.pro

class Eventprojector(models.Model):
    projector = models.ForeignKey(Projectors)
    date = models.DateField(blank=True, null=True)
    start = models.CharField(max_length=50, null=True)
    end = models.CharField(max_length=50, null=True)
    is_accept = models.IntegerField(default=0)

    def get_absolute_url(self):
        return reverse( 'retest:eventprojectorform')
    def __str__(self):
        return str(self.id)


event / urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /event/1/
    url(r'^(?P<eventprojector_id>[0-9]+)/$', views.projectoraccept, name='projectoraccept'),
]


event / views.py:

from django.shortcuts import render, get_object_or_404
from .models import Eventprojector
# Create your views here.

def projectoraccept(request, eventprojector_id):
    eventprojector = get_object_or_404(Eventprojector, pk=eventprojector_id)
    eventprojector.is_accept = eventprojector.is_accept+1
    eventprojector.save(update_fields=['is_accept'])
    if eventprojector.is_accept == 3:
        eventprojector.projector.available = False
        eventprojector.projector.save(update_fields=['available'])
    return render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })


statckoverflow / urls.py:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^event/', include('event.urls')),
    url(r'^admin/', admin.site.urls),
]


stackoverflow / event / templates / event / projectordetails.html:

{% if eventprojector.projector.available %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">TEST</span>
    </div>
    <label class="control-label col-sm-2">TESTTEST</label>
    <div class ="col-sm-10">TESTETST</div>
    </div>
{% endif %}


设置完所有环境后,通过管理控制台将一些测试数据添加到表中:

event_projectors: pro = 'TEST1' and available= True
event_eventprojector :  projector = 'TEST1' date = today, start = 1, end = 2, is_accept = 1


然后运行服务器开始测试:

1,第一次输入http://127.0.0.1:8000/event/1/时,它将显示如下形式:(这里eventprojector中的'is_accept'字段将加1等于2,检查视图功能)

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">TEST</span>
</div>
<label class="control-label col-sm-2">TESTTEST</label>
<div class ="col-sm-10">TESTETST</div>
</div>


2,当您再次输入URL http://127.0.0.1:8000/event/1/时,eventprojector中的'is_accept'字段将再次加1为3,然后将其设置为eventprojector.projector.available = False,此处您看不到元素,其在模板中为空。

希望上面的代码可以帮助您正确地使用参数eventprojector渲染模板projectiondetails.html,请注意,渲染功能render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })仅将参数event/projectordetails.html渲染为临时eventprojector。您的模板可能不匹配吗?

关于python - if语句在Django模板中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43704326/

10-10 07:27