"没有返回HttpResponse对象。它返回了None而不是"在POST请求

时间:2018-03-14 16:53:31

标签: python html css django

我试图将选择从下拉表单作为POST请求传递到视图中,然后使用此选项从django查询某些数据。然后,我使用这些查询尝试按照this方法将django模型数据映射到highcharts。问题是我得到了一个"视图properties.views.property_list没有返回一个HttpResponse对象。它返回了None而不是"提交表单时出错。我已经在SO上查看了类似的问题,但没有一个解决方案似乎适用于我的案例。也许我的表格无效,但我不确定是什么造成了这种情况。以下是我撰写的代码:

views.py

def property_list(request):

if request.user.is_authenticated():
    current_user_groups = Group.objects.filter(id__in=request.user.groups.all())
    current_user_properties = Property.objects.filter(groups__in=current_user_groups)
    current_user_meters = Meter.objects.filter(meter_id__in=current_user_properties)
    property_meter_data = MeterData.objects.filter(meter__in=current_user_meters)

    class AccountSelectForm(forms.Form):
        accounts = forms.ModelChoiceField(queryset=current_user_meters)
        accounts.widget.attrs.update({'class' : 'dropdown-content'})

    form = AccountSelectForm()

    if request.method == "POST":
        if form.is_valid():
            selection = form.cleaned_data['accounts']

            current_user_groups = Group.objects.filter(id__in=request.user.groups.all())
            current_user_properties = Property.objects.filter(groups__in=current_user_groups)
            current_user_meters = Meter.objects.filter(meter_id__in=current_user_properties)

            selected_meters = Meter.objects.filter(name=selection)
            selected_meter_data = MeterData.objects.filter(name=selection)

            usage_data = {'usage': [], 'dates': []}

            for meter in selected_meter_data:
                usage_data['usage'].append(meter.usage)
                usage_data['dates'].append(meter.usage)

            # data passing for usage chart

            usage_xAxis = {"title": {"text": 'Date'}, "categories": usage_data['dates']}
            usage_yAxis = {"title": {"text": 'Usage'}, "categories": usage_data['usage']}
            usage_series = [
                {"data": usage_data['usage']},
            ]

            return HttpResponseRedirect('properties/property-selected.html', {
                'form': form,
                'usage_xAxis': usage_xAxis, 
                'usage_yAxis': usage_yAxis, 
                'usage_series': usage_series,
                'current_user_meters': current_user_meters,
                'selection': selection,
                'selectected_meters': selected_meters,
                'selected_meter_data': selected_meter_data,
            })

    else:

        current_user_groups = Group.objects.filter(id__in=request.user.groups.all())
        current_user_properties = Property.objects.filter(groups__in=current_user_groups)
        current_user_meters = Meter.objects.filter(meter_id__in=current_user_properties)
        property_meter_data = MeterData.objects.filter(meter__in=current_user_meters)

        return render(request, 'Properties/properties.html',{
        'form': form,
        'current_user_groups': current_user_groups,
        'current_user_properties': current_user_properties,
        'current_user_meters': current_user_meters,
        'property_meter_data': property_meter_data
    })

else:

    # If the usre isn't authenticated, the user is redirected to the Sign-n Page

    return render(request, 'SignIn/SignInPage.html')

template.html

<!-- Query based content for dropdown menu -->
<form method="POST" action="" id="property-select">
    {% csrf_token %}
    {{ form.accounts }}
    <input type="submit" value="Submit" class="submit-button" style="margin-top:30px;"/>
</form>

1 个答案:

答案 0 :(得分:3)

如果method =='POST'并且您的表单无效,则不会返回任何内容。