视图返回None(未返回HttpResponse对象)

时间:2016-03-19 18:20:25

标签: python django

这个问题可能看似重复和/或太无聊。我已阅读thisthisthisthisthis个问题&答案。但是找不到适合我的问题的解决方案。

我是Django框架的新手。为了学习它我想创建简单的博客。当用户单击注册按钮(填写必填字段后)时,抛出以下错误:

  

/ user / register /上的ValueError视图user.views.register没有   返回一个HttpResponse对象。它改为返回None。

views.py

def register(request):
    """
    Registers new user. 
    """
    if request.POST:
        if request.method == 'POST':
            personal_info = UserFormModel(request.POST)
            if personal_info.is_valid():
                email = personal_info.cleaned_data['email']
                username = personal_info.cleaned_data['username']
                if User.objects.filter(email=email).exists():
                    return HttpResponse('email error')
                elif User.objects.filter(username=username).exists():
                    return HttpResponse('username error')
                else:
                    return HttpResponse('saved')
    else:
        personal_info = UserFormModel()
        return render_to_response('user/registration.html',
                            {
                                'title': 'Registration',
                                'username_error': 'Sorry, someone already has that username.',
                                'personal_info': personal_info,
                            }, 
                            context_instance=RequestContext(request))

如有必要,我可以共享任何文件内容。

任何有用的评论或答案都将不胜感激。

1 个答案:

答案 0 :(得分:0)

排队 if personal_info.is_valid():

如果个人信息无效,则返回无。(不返回任何内容)

添加其他条件,因为您没有处理案例。

if personal_info.is_valid():
    # code here
else:
    return HttpResponse('personal info provided not valid')

处理此类情况的更好方法之一是保持字典状态消息和结果在函数开始时初始化,并仅返回单个位置而不是在多个位置返回,并存储结果是result_dict。

result_dict = {'status': False}

并始终在函数结束时返回

return HttpResponse(json.dumps(result_dict))

这样你就不会错过返回值的情况。

所以最终代码应该是

def register(request):
    """
    Registers new user. 
    """
    result_dict = {'status': False}
    if request.POST:
        if request.method == 'POST':
            personal_info = UserFormModel(request.POST)
            if personal_info.is_valid():
                email = personal_info.cleaned_data['email']
                username = personal_info.cleaned_data['username']
                if User.objects.filter(email=email).exists():
                    result_dict['message'] = 'email error'
                elif User.objects.filter(username=username).exists():
                    result_dict['message'] = 'username error'
                else:
                    result_dict['message'] = 'saved'
                    result_dict['status'] = True
        else:
            result_dict['message'] = 'personal info provided not valid'
    else:
        personal_info = UserFormModel()
        return render_to_response('user/registration.html',
                            {
                                'title': 'Registration',
                                'username_error': 'Sorry, someone already has that username.',
                                'personal_info': personal_info,
                            }, 
                            context_instance=RequestContext(request))

    return HttpResponse(json.dumps(result_dict))