我在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')
{% 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 %}
错误说是:
答案 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)