1

视图.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
      }) 

模型.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 %}

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

这是我从数据库中获取值并显示的视图。类型列表对象过滤的值需要显示值以及复选框。对于类型对象,没关系。在 django 中有什么方法可以做到这一点。

4

2 回答 2

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
    })

祝你好运!

于 2013-05-29T15:58:11.513 回答
0

在不知道为什么要这样做的情况下,看起来您通过尝试使其动态化来使问题过于复杂化,而您可以只使用 a models.BooleanField(),Django 会自动将其显示为复选框。它会更简单,并且也使代码更易于阅读。

于 2013-05-28T12:16:42.343 回答