Django模板错误?

问题描述:

我正在尝试使用django-registration,django-registration-defaults和django-email-usernames为我的django应用程序实现注册和登录系统。Django模板错误?

一切都安装得很好。 django-email-usernames提供了一个自定义的登录表单,它允许将电子邮件用作用户名。这是表单的代码。

from django import forms 
from django.utils.translation import ugettext_lazy as _ 
from django.contrib.auth import authenticate 

... 

class EmailLoginForm(forms.Form): 
    email = forms.CharField(label=_(u"Email"), max_length=75, widget=forms.TextInput(attrs=dict(maxlength=75))) 
    password = forms.CharField(label=_(u"Password"), widget=forms.PasswordInput) 

    def clean(self): 
     # Try to authenticate the user 
     if self.cleaned_data.get('email') and self.cleaned_data.get('password'): 
      user = authenticate(username=self.cleaned_data['email'], password=self.cleaned_data['password']) 
      if user is not None: 
       if user.is_active: 
        self.user = user # So the login view can access it 
       else: 
        raise forms.ValidationError(_("This account is inactive.")) 
      else: 
       raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive.")) 

     return self.cleaned_data 

在django注册的urls.py中,有登录页面的模式。它使用默认的django.contrib.auth.views.login视图进行登录。

所以在urls.py我得到这个:

from django.conf.urls.defaults import * 
from django.views.generic.simple import direct_to_template 
from django.contrib.auth import views as auth_views 
from registration.views import activate 
from registration.views import register 

from email_usernames.forms import EmailLoginForm 

... 

url(r'^login/$', auth_views.login, {'template_name': 'registration/login.html', 'authentication_form': EmailLoginForm}, name='auth_login'), 

... 

的django.contrib.auth.views.login发生在一个TEMPLATE_NAME并形成使用。正如你在上面看到的那样,我正在传递这些内容。我正在设置模板,并将authentication_form设置为由django-email-usernames提供的模板。

TemplateSyntaxError在夹缝AttributeError的同时渲染/帐号/登录/ :“WSGIRequest”对象有没有属性“得到”

模板错误时浏览到登录页面,我收到以下错误

然后

在模板/Users/Amir/.virtualenvs/scvd/lib/python2.6/site-packages/registration_defaults/templates/registration/login.html,误差在管线16 夹缝AttributeError的而呈现: 'WSGIRequest' 对象没有属性'获得'

6 {% endif %} 
7 
8 <form method="post" action="{% url django.contrib.auth.views.login %}">{% csrf_token %} 
9 <table> 
10 <tr> 
11  <td>{{ form.username.label_tag }}</td> 
12  <td>{{ form.username }}</td> 
13 </tr> 
14 <tr> 
15  <td>{{ form.password.label_tag }}</td> 
16  <td>{{ form.password }}</td> 
17 </tr> 
18 </table> 
19 <p><a href="{% url auth_password_reset %}">Forgot</a> your password? <a href="{% url registration_register %}">Need an account</a>?</p> 
20 
21 <input type="submit" value="login" /> 
22 <input type="hidden" name="next" value="{{ next }}" /> 
23 </form> 
24 
25 {% endblock %} 
26 

我很坚持。我很确定我正确地做了urls.py配置。我不明白模板中第16行({{form.password}})发生的错误。

请让我知道还有什么我可以提供澄清我的问题。提前感谢您的帮助。

看来你干净的()不是那么清楚

user = authenticate(username=self.cleaned_data['email'], password=self.cleaned_data['password']) 

有不用彷徨缺少

user = authenticate(username=self.cleaned_data.get['email'], password=self.cleaned_data.get['password']) 

它的使用

def clean(self): 
    x = self.cleaned_data.get("username") 
    y = self.cleaned_data.get("password") 

那么你可以使用

好的做法
user = authenticate(username=x, password=y)