0

由于我正在创建的网站规模较小,我决定跳过 JSON,只创建一个 ajax 页面,该页面返回实际的 html 或单个单词来说明要做什么。代码好像有错误,因为在new_user_pass = new_user(request.POST)服务器突然返回500 (INTERNAL SERVER ERROR)网站检查用户之前是否登录过,然后做出决定:如果有,则在创建会话后重定向到主页(尚未编码)。如果他们没有:返回一个告诉 JQuery 向他们询问其他信息的单词。如果某处出现错误,它应该到达返回错误的文件末尾(尚未完全编码)。

这是 forms.py 文件:

class log_in(forms.Form):
    username = forms.EmailField()
    password = forms.PasswordInput()

class new_user(forms.Form):
    username = forms.EmailField()
    password = forms.PasswordInput()
    password2 = forms.PasswordInput()
    pen_name = forms.CharField(max_length=30)

views.py 文件:

def user_log_in(request):
    #add stuff for ajax request
    user_pass = log_in(request.POST)
    er = []
    if user_pass.is_valid():
        cleaned_info = user_pass.cleaned_data
        user_object = User.objects.filter(email = cleaned_info['username'])
        if user_object.exists():
            #create session
            return HttpResponseRedirect('/home')
        else:
            new_user_pass = new_user(request.POST)#where bug is
            cleaned_info = new_user_pass.cleaned_data
            if new_user_pass.is_valid():
                if cleaned_info['password'] == cleaned_info['password2']:
                    new_user = User(activated = 0, email = cleaned_info['username'], pen_name = cleaned_info['pen_name'], password = cleaned_info['password'])
                    new_user.save()
                    msg = ""
                    if send_mail('Activate', msg, '*******', [cleaned_info['username']], fail_silently=False):
                        return HttpResponse('e_act')
                    else:
                        er.append("Error Sending Email")
                else:
                    er.append('Passwords are not the same')
            elif cleaned_info['TN']:
                for e in new_user_pass.errors:
                    er.append(e)
            else:
                return HttpResponse('n_usr')
    return HttpResponse('SOS')

而且,虽然我不认为错误在这里,但 Jquery 函数:n_usr = false;

function log_in () {
    //add code to check if it's the wrong usr, pass, correct, or if they've never logged in before.
    //code to reveal section

编辑:Django给出了错误new_user' referenced before assignment

4

2 回答 2

2

您还没有将new_user表单导入到您的视图中。

而且我确实希望您没有创建自己的 User 类,您在其中以纯文本形式存储密码。那会很糟糕。

于 2012-07-30T14:12:31.513 回答
1

下一个片段是错误的:

def new_user():
    print "in new_user"

def f():
    new_user()
    if True:
        new_user = 23
        print "new_user"

f()

您不能调用 new_user(),因为编译器认为它在 f() 的范围内,因为 new_user 的初始化更远。

您将 new_user 用作两个不同的东西 - 作为表单的构造函数

new_user(request.POST)

并作为用户实例:

new_user = User(activated = 0, email = cleaned_info['username'], pen_name = cleaned_info['pen_name'], password = cleaned_info['password'])

就编程风格而言,它也不好——为完全不同的变量使用一个名称。

例如,您可以为局部变量使用另一个名称,并且它必须是可以的:

new_user_inst = User(activated = 0, email = cleaned_info['username'], pen_name = cleaned_info['pen_name'], password = cleaned_info['password'])
...
于 2012-07-30T14:17:19.337 回答