Django中/ profile /中的ValueError:未返回HttpResponse对象

时间:2014-05-15 01:37:10

标签: python django

我在models.py中有这行代码

 @login_required
 def user_profile(request):
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/loggedin')
        else:
            user = request.user
            profile = user.profile
            form = UserProfileForm(instance=profile)
            args = {}
            args.update(csrf(request))
            args['form'] = form
            return render(request, 'profile.tml', args)

和forms.py

from django import forms
from models import UserProfile


class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('address', 'contactnos')
在我的loggedin.html

{% extends 'base.html' %}

{% block content %}
<h2> Hi {{ full_name }} you are logged in!</h2>
<p>click <a href="/accounts/logout/">here</a> to logout. </p>

<p>Click <a href="/profile/">here</a> to edit your profile info </p>

{% endblock %}

错误说是:

/ profile /的ValueError 视图userprofile.views.user_profile未返回HttpResponse对象。

1 个答案:

答案 0 :(得分:0)

您尚未考虑GET方案;不通过POST访问视图时会发生什么?你什么都不返回,所以你得到这个错误。

http.HttpResponse

时返回request.method != 'POST'

也许你打算取消缩进下半部else声明。

您尚未考虑GET方案;不通过POST访问视图时会发生什么?你什么都不返回,所以你得到这个错误。

http.HttpResponse

时返回request.method != 'POST'

也许你打算取消缩进下半部else声明。

@login_required
 def user_profile(request):
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/loggedin')
    else:
        user = request.user
        profile = user.profile
        form = UserProfileForm(instance=profile)
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'profile.tml', args)