创建显示表单的循环时出现问题

时间:2019-08-21 13:37:16

标签: django

我有一个活动表,用户可以选择其中一些活动。根据他的选择,我将他重定向到一个新页面,在该页面中,他必须为每个活动选择一个先前选择的子活动列表。因此,我遍历了活动并过滤了每个表单的查询集(子活动)。它可以正确显示,但是在提交表单时无效。

起初,我以为它可能来自共享相同名称的表单,但是即使只有一个表单,错误仍然存​​在。在选择铝和塑料升级后,我试图打印出表格外观:

[21/Aug/2019 15:26:53] "POST /subactivities/ HTTP/1.1" 500 59980
False <tr><th><label for="id_subactivity">Select specific activities:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><select name="subactivity" required id="id_subactivity" multiple>
  <option value="Aluminium upcycling">Aluminium upcycling</option>

  <option value="Plastic Upcycling">Plastic Upcycling</option>

</select></td></tr>
Internal Server Error: /subactivities/
Traceback (most recent call last):
  File "C:\Users\lavoye\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\lavoye\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    "returned None instead." % (callback.__module__, view_name)
ValueError: The view blog.views.Subactivities didn't return an HttpResponse object. It returned None instead.

class Subactivities(TemplateView):
    template_name = 'blog/subactivities.html'
    context = {'title': 'Subactivities selection page'}
    def get(self,request):
        self.context['form_subact_list']=[]
        for activity in request.session['activities']:
            if Subactivity.objects.filter(business_models__business_model_name=request.session['business_model']['business_model_name'], activity__activity_name = activity['activity']):
                form_subact = SubactivitiesForm()
                form_subact.fields['subactivity'].queryset = Subactivity.objects.filter(business_models__business_model_name=request.session['business_model']['business_model_name'], activity__activity_name = activity['activity'])
                self.context['form_subact_list'].append({'activity': activity['activity'], 'form_subact':form_subact})
        form_target_group = TargetGroupForm()
        self.context['form_target_group'] = form_target_group
        return render(request,self.template_name, self.context)
    def post(self,request):
        form_subact = SubactivitiesForm(request.POST)
        form_target_group = TargetGroupForm(request.POST)
        print(form_subact.is_valid(), form_subact)
        print(form_subact.cleaned_data['subactivity'])
        if form_subact.is_valid():
            request.session['target_group'] = form_target_group.cleaned_data['target_group']
            for activity in request.session['activities']:
                subactivities = Subactivity.objects.filter(business_models__business_model_name=request.session['business_model']['business_model_name'], activity__activity_name__in = request.session['activities'])
                for subactivity in subactivities:
                    activity['subactivities'].append(subactivity.subactivity_name)
            subactivity_name = subactivity_dict['activity_name']
            request.session['activity'] = activity_name
            return redirect('/questionnaire', self.context)

和HTML:

{% extends "blog/index.html" %}
{% block content %}
    <h1>Subactivities selection</h1>
    {% for object in form_subact_list %}
    {% if object.form_subact %}
    <h2> {{ object.activity }} </h2>
    <form method="post">
        {% csrf_token %}
        {{ object.form_subact.as_ul}}
    </form>
    {% endif %}
    {% endfor %}
    <form method="post">
        {% csrf_token %}
        {{ form_target_group.as_ul }}
        <button type="submit">Submit</button>
    </form>
    <p> {{ context }} </p>
{% endblock content %}

理想情况下,我想获取列表(或字典)的列表,其中每个表单的答案都分开,但是页面上每个表单的提交按钮都是公用的。谢谢!

PS:问题在于无论我输入什么,该表格都是无效的,而不是它返回None的事实。

0 个答案:

没有答案