1

我正在尝试编写我的方法以在注册时添加用户类型对象。这是代码:

视图.py

def registration(request):
c= {}
c.update(csrf(request))
state = "Please Register below..."
username = None
email = None
password = None
user_success = None
user_created = None
if request.POST:
    username = request.POST.get('username')
    email = request.POST.get('email')
    password = request.POST.get('password')
    print "username ",username

user = authenticate(username=username, password=password)
if user is not None:
    if user.is_active:
        user_created = True
    else:
        user_created = False
else:
        user = User.objects.create_user(username=username,password=password,email=email)
        user.save()
        user.is_active = True
        user_success = True

return render_to_response('register.html',{'success':user_success,'created':user_created,'username': username},context_instance = RequestContext(request))

表格.py

class RegistrationForm(forms.Form):

username = forms.CharField()
email = forms.EmailField()
password =  forms.CharField()

模板/register.html 是:

{% extends "base.html" %}
{% block pageContent %}
<div class="container">


    <form action="/register" method="post">

            {% csrf_token %}
            username:
            <input type="text" name="username" value="{{ username}}" /><br />
            password:
            <input type="password" name="password" value="" /><br />
            Email:
            <input type="text" name="email" value="{{email}}" /><br />

            <input type = 'submit' value='Register' />
    </form>


</div>
{% endblock %}

谁能帮我找出错误?也让我知道我是否正确注册

4

2 回答 2

1

此时的值username必须评估为 True。

user = User.objects.create_user(username=username,password=password,email=email)
#                                        ^^^^^^^^ this can't be a bool() false 

为什么?该错误消息非常具体..它是从django auth 中的这个特定位置调用的:

  def create_user(self, username, email=None, password=None, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not username:
            raise ValueError('The given username must be set')

你能 100% 肯定用户名是正确的值吗?这听起来像是一个空字符串。

于 2013-04-28T00:36:05.240 回答
0

我发现了错误。错误出现在 view.py 的缩进中。

if user is not None:

仅当请求类型为 POST 时,才应使用部分代码。所以需要缩进。

于 2013-04-28T00:50:36.533 回答