在django中构建复选框

时间:2013-05-28 10:52:25

标签: django django-models django-forms django-templates django-admin

views.py

def what(request):
      user = request.user
      report = Report.objects.get(user=user.id)
      reportnotesform=ReportNotes(instance=report)
      reportform = ReportForm(instance=report)
      typeList = Types.objects.filter(user=user, is_active=True,parent_type_id=None)
      list = []
      for type in typeList:
            if not type.parent_type_id:
                 list.append(type)
                 subtype = Types.objects.filter(parent_type_id=type.id, is_active=True)
                 for subtypes in subtype:
                      list.append(subtypes)
      if request.method == 'POST':
            ReportType.objects.filter(report=report).delete()
            checked_ones = [unicode(x) for x in subtype if unicode(x) in request.POST.keys()]
            reportnotesform=ReportNotes(request.POST,instance=report)
            if reportnotesform.is_valid():
                 report=reportnotesform.save(commit=False)     
                 report.user=request.user
                 report.save()
                 for entry in checked_ones:
                      r = ReportType()
                      r.report = report 
                      r.title = entry
                      r.save()
            return redirect('/member/where/')
      checked_ones = [x.title for x in ReportType.objects.filter(report=report)]  
      return render(request, 'incident/what.html',
      {
      'newreport_menu': True,
      'typeList':list,

      'checked_ones':checked_ones,
    #  'typelist': typelist,
      'ReportNotes':reportnotesform,
      'ReportForm':reportform
      }) 

models.py

class ReportType(models.Model):
    report = models.ForeignKey(Report)
    title = models.CharField('Incident Type', max_length=200)

模板

 {% for type in typeList%}
  {% if type.parent_type_id == None %}
  <h1>{{type.title}}</h1>
 {% else %}
 <p><input type="checkbox">{{type.title}}</input></p>
 {% endif %}
{% endfor %}

我正在使用模型表格动态显示复选框。

这是我的视图,我从数据库和显示中获取值。由typelist对象过滤的值需要显示值以及复选框。对于类型对象,无所谓。有什么办法在django中执行它

2 个答案:

答案 0 :(得分:1)

这应该适合你:

<h1> {% for type in types %}{{type.title}}<br />{% endfor %}</h1>          
<form action="" method="post">
{% for field in typelist %}
{% if field.title in checked_ones %}
<label><input type="checkbox" checked="true" name="{{field}}">{{ field }}</label><br /> 
{% else %}
<label><input type="checkbox" name="{{field}}">{{ field }}</label><br /> 
{%endif%}
{% endfor %} 
</form>


def what(request):
    user = request.user

    types = Types.objects.filter(user=user.id, parent_type_id=None).order_by('title')
    typelist=Types.objects.filter(user=user.id,parent_type_id__isnull=False).order_by('title')
    reporttypeForm = ReportTypeForm()
    if request.method == 'POST':
        ReportType.objects.filter(report=#your report).delete()
        checked_ones = [unicode(x) for x in typelists if unicode(x) in request.POST.keys()]
        for entry in checked_ones:
            r = ReportType()
            r.report = # the hardcoded report
            r.title = entry
            r.save()
        ...
        reporttypeForm = ReportTypeForm(request.POST)        
        if reporttypeForm.is_valid():
            reporttypeForm.save()
    checked_ones = [x.title for x in ReportType.objects.filter(report=#your report)]
    return render(request, 'incident/what.html',
        {
            'checked_ones':checked_ones,
            'newreport_menu': True,
            'types':types,
            'typelist': typelist,
            'reporttypeForm':ReportTypeForm
    })
祝你好运!

答案 1 :(得分:0)

在不知道你为什么试图这样做的情况下,当你只能使用models.BooleanField()时,看起来你过度复杂化你的问题,Django会自动显示为复选框。它更简单,也使代码更容易阅读。