我得到这个错误:
Internal Server Error: /signup/
Traceback (most recent call last):
File "C:\python\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\python\lib\site-packages\django\utils\deprecation.py", line 142, in __call__
response = self.process_response(request, response)
File "C:\python\lib\site-packages\django\middleware\clickjacking.py", line 32, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'str' object has no attribute 'get'
尝试呈现此视图时:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
new_user.is_active = False
new_user.save()
current_site = get_current_site(request)
subject = "Activate your DC-Elects account"
message = render_to_string('registration/account_activation_email.html', {
'user': new_user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(new_user.id)),
'token': account_activation_token.make_token(new_user),
})
new_user.email_user(subject, message)
return reverse('elections:account_activation_sent')
else:
form = SignUpForm()
return render(request, 'registration/signup.html',{
'form': form,
})
这个错误信息不是很好的描述,但是邮件是用正确的链接发送的,所以我很困惑为什么会有错误。如何防止此错误并正确呈现下一个视图?
最佳答案
Django的view函数应该返回HttpResponse
对象。reverse
只是生成url字符串的方法,而不是响应对象,因此这一行:
return reverse('elections:account_activation_sent')
提出错误。
如果需要重定向,请使用
redirect
快捷方式:return redirect('elections:account_activation_sent')
关于python - 创建新用户时出现AttributeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48832773/