测试Django注册页面时出错不正确

时间:2018-05-23 08:46:09

标签: django python-3.x django-rest-framework django-views

我正在开发一个Django应用程序。我想为新用户创建一个注册页面。在Django获取提交的表单并存储已清理的数据后,我希望它检查并查看输入的用户名是否已存在。如果它存在,我希望Sjango重新显示页面并显示错误消息。如果用户名不存在,它可以正常工作。

但是当用户名存在时,我收到以下错误:

ValueError at /signup/
The view users.views.user_signup didn't return an HttpResponse object. It returned None instead.
Request Method: POST
Request URL:    http://127.0.0.1:8000/signup/
Django Version: 2.0.5
Exception Type: ValueError
Exception Value:    
The view users.views.user_signup didn't return an HttpResponse object. It returned None instead.
Exception Location: /Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 139
Python Executable:  /Users/omarjandali/anaconda3/envs/MySplit/bin/python
Python Version: 3.6.2
Python Path:    
['/Users/omarjandali/Desktop/demo/mysplit',
 '/Users/omarjandali/anaconda3/envs/MySplit/lib/python36.zip',
 '/Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6',
 '/Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6/lib-dynload',
 '/Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6/site-packages']

我现在看到的奇怪的事情是我有一个print语句,它会显示我写的查询结果,以检查用户名以及它是否存在于数据库中。提交表单时,它会抛出错误,并且在终端中的跟踪中不会打印语句。

我正在输入现有的用户名。它应该进行查询并打印结果。

这是我的代码:

def user_signup(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            email = cd['email']
            username = cd['username']
            password = cd['password']
            user_taken = User.objects.filter(username = username).first()
         ** print(user_taken)
            if user_taken == None:
                print('username is good')
                secure_password = make_password(password)
                user = User.objects.create(
                    username = username,
                    email = email,
                    password = secure_password
                )
                login(request, user)
                return redirect('user_profile')
            else:
                print('username is taken')
                form = SignupForm()
                error = "Username is already taken"
                parameters = {
                  'form':form,
                  'error':error
                }
                return render(request, 'users/signup.html', parameters)
    else:
        form = SignupForm
        parameters = {
            'form':form
        }
        return render(request, 'users/signup.html', parameters)

我把一个**放在打印声明应该打印的地方:

这是我在终端中获得的内容:

[23/May/2018 08:39:27] "POST /signup/ HTTP/1.1" 302 0
[23/May/2018 08:39:27] "GET /profile/ HTTP/1.1" 200 960
[23/May/2018 08:39:31] "GET /logout HTTP/1.1" 301 0
[23/May/2018 08:39:32] "GET /logout/ HTTP/1.1" 302 0
[23/May/2018 08:39:32] "GET /login/ HTTP/1.1" 200 516
[23/May/2018 08:39:32] "GET /logout HTTP/1.1" 301 0
[23/May/2018 08:39:32] "GET /logout/ HTTP/1.1" 302 0
[23/May/2018 08:39:32] "GET /login/?next=/logout/ HTTP/1.1" 200 516
[23/May/2018 08:39:34] "GET /signup/ HTTP/1.1" 200 609
Internal Server Error: /signup/
Traceback (most recent call last):
  File "/Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/omarjandali/anaconda3/envs/MySplit/lib/python3.6/site-packages/django/core/handlers/base.py", line 139, in _get_response
    "returned None instead." % (callback.__module__, view_name)
ValueError: The view users.views.user_signup didn't return an HttpResponse object. It returned None instead.
[23/May/2018 08:39:47] "POST /signup/ HTTP/1.1" 500 54487

它甚至不打印查询的预期结果。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:3)

当表单无效时,您没有返回POST个请求的回复。

通常的解决方法是对最后两行进行去缩进,以便始终在视图末尾返回响应。

def user_signup(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        ...
    else:
        form = SignupForm()   # Note you should instantiate the form instead of using the form class SignupForm 
    parameters = {
        'form':form,
    }
    return render(request, 'users/signup.html', parameters)

要调试表单无效的原因,您可以在视图或模板中查看form.errors

丹尼尔在评论中建议,验证应该以表格而不是视图进行。然后,您可以在模板中显示表单错误,而不需要将error添加到视图中的上下文中。如果您的表单是模型表单,则可能已经为您检查唯一性,在这种情况下,您需要做的就是在模板中显示表单错误。有关详细信息,请参阅working with form templates上的文档。

答案 1 :(得分:2)

如果您的表单无效,则您没有返回回复。

def user_signup(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        if form.is_valid():
            # YOU code here

        parameters = {
          'form': form,
           # if you need only errors messages
           # 'error': " ".join([x for x in form.errors.values()])
          'error': form.errors.as_ul()
        }
        return render(request, 'users/signup.html', parameters)