模板if
循环中的for
语句不起作用。它仍然显示不可用的项目。
这是我模板的代码
{% 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 %}
答案 0 :(得分:1)
以下是重构代码,它将根据您的代码对可用和不可用的项目起作用:
<强>事件/ 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)
<强>事件/ 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'),
]
<强>事件/ 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),
]
<强>计算器/事件/模板/事件/ 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/
时,它将显示如下形式:(此处'event_jecter中的'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,当您再次输入网址http://127.0.0.1:8000/event/1/
时,eventprojector中的'is_accept'字段将再次加1为3,然后它将设置eventprojector.projector.available = False
,此处您无法看到该元素,它在模板中是空的。
希望上面的代码可以帮助您正确地使用参数eventprojector渲染模板projectordetails.html,请注意渲染函数render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })
将仅使用参数event/projectordetails.html
呈现给tempalte eventprojector
。可能你的模板有些不匹配?
答案 1 :(得分:0)
models.py
class Projectors(models.Model):
pro = models.CharField(max_length=50)
sec = models.ForeignKey(Section)
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)
views.py
def projectoraccept(request, eventprojector_id):
eventprojector = get_object_or_404(Eventprojector, pk=eventprojector_id)
if request.method == 'POST':
eventprojector.is_accept = eventprojector.is_accept+1
eventprojector.save(update_fields=['is_accept'])
if eventprojector.is_accept == 3:
if eventprojector.projector.sec.sec_name == 'CS_dept':
eventprojector.projector.available = False
eventprojector.projector.save(update_fields=['available'])
elif eventprojector.projector.sec.sec_name == 'EC_dept':
eventprojector.projector.available = False
eventprojector.projector.save(update_fields=['available'])
elif eventprojector.projector.sec.sec_name == 'EEE_dept':
eventprojector.projector.available = False
eventprojector.projector.save(update_fields=['available'])
elif eventprojector.projector.sec.sec_name == 'IEEE':
eventprojector.projector.available = False
eventprojector.projector.save(update_fields=['available'])
elif eventprojector.projector.sec.sec_name == 'TCP_dept':
eventprojector.projector.available = False
eventprojector.projector.save(update_fields=['available'])
return render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })