提交表单后出错,ValueError at / register视图blog.views.register未返回HttpResponse对象

时间:2012-01-19 15:10:56

标签: python database django forms

views.py:

def register(request):
    flag = True
    possible = '0123456789abcdefghijklmnopqrstuvwxyz'
    token = ''
    tempToken = ''

    current_datetime = datetime.datetime.now()

    user = UsersModelForm()
    if request.method == 'POST':
        userf = UsersModelForm(request.POST)
        username = userf.data['username']
        password = userf.data['password']
        passwordrepeat = userf.data['passwordrepeat']
        email = userf.data['email']

        tempSalt = bcrypt.gensalt()
        password = bcrypt.hashpw(password,tempSalt)
        passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)

        if password != passwordrepeat:
            flag = False
            passVariable = {'user':user, 'flag': False}
            return render_to_response('register.html', passVariable, context_instance=RequestContext(request))

        elif password == passwordrepeat:
            for i in range(1,10):
                temp = random.choice(possible)
                token = token + temp

            if userf.is_valid():
                check = userf.save(commit=False)
                check.email_token = token
                check.email_token_expiry = current_datetime + timedelta(1)
                check.password = password
                check.passwordrepeat = passwordrepeat
                check.save()
                subject, from_email, to = 'hello', 'xxx@gmail.com', 'xxx@yahoo.com'
                text_content = 'This is an important message.'
                html_content = '<a href="http://127.0.0.1:8000/confirmemail/' + token + '">Click this link to confirm email</a>'
                msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
                return render_to_response('index.html', {}, context_instance=RequestContext(request))

    else:
        return render_to_response('register.html', {"user": user, 'flag': True}, context_instance=RequestContext(request))

代码中的错误在哪里?

如果有人帮我修理,我们将非常感激吗?

我得到的错误:

ValueError at /register

The view blog.views.register didn't return an HttpResponse object.

Request Method:     POST
Request URL:    http://127.0.0.1:8000/register
Django Version:     1.3.1
Exception Type:     ValueError
Exception Value:    

The view blog.views.register didn't return an HttpResponse object.

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py in get_response, line 129
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/user1/djangoblog',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

1 个答案:

答案 0 :(得分:4)

考虑password == passwordrepeatuserf.is_valid()为假时会发生什么。执行在哪里?