简体   繁体   English

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

[英]ValueError at /profile/ in Django: didn't return an HttpResponse object

I have this line of code in models.py 我在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)

and in forms.py 并在forms.py中

from django import forms
from models import UserProfile


class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('address', 'contactnos')

in my loggedin.html 在我的loginin.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 %}

The error is says: 错误是说:

ValueError at /profile/ The view userprofile.views.user_profile didn't return an HttpResponse object. / profile /处的ValueError视图userprofile.views.user_profile没有返回HttpResponse对象。

You haven't accounted for a GET scenario; 您尚未考虑过GET场景; what happens when the view is not accessed via POST ? 不通过POST访问视图时会发生什么? You return nothing, therefore you get this error. 您什么也不返回,因此会出现此错误。

Return an http.HttpResponse when request.method != 'POST' request.method != 'POST'时返回一个http.HttpResponse

Perhaps you meant to un-indent the bottom half else statement. 也许您的意思是使else陈述的下半部分缩进。

You haven't accounted for a GET scenario; 您尚未考虑过GET场景; what happens when the view is not accessed via POST ? 不通过POST访问视图时会发生什么? You return nothing, therefore you get this error. 您什么也不返回,因此会出现此错误。

Return an http.HttpResponse when request.method != 'POST' request.method != 'POST'时返回一个http.HttpResponse

Perhaps you meant to un-indent the bottom half else statement. 也许您的意思是使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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM