Django 模板错误?

发布于 2024-12-02 13:11:22 字数 3236 浏览 0 评论 0原文

我正在尝试使用 django-registration、django-registration-defaults 和 django-email-usernames 为我的 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-registration 的 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 at /accounts/login/ 渲染时捕获 AttributeError:“WSGIRequest”对象没有属性“get”

模板错误

在模板 /Users/Amir/.virtualenvs/scvd/lib/python2.6/site-packages/registration_defaults/templates/registration/login.html 中,错误在第 16 行 渲染时捕获 AttributeError:“WSGIRequest”对象没有属性“get”

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 }}) 发生的错误。

请让我知道我还能提供什么来澄清我的问题。非常感谢您提前提供的帮助。

I am trying to implement a registration and login system for my django app using django-registration, django-registration-defaults, and django-email-usernames.

Everything installed just fine. django-email-usernames provides a custom login form which allows an email to be used as the username. Here is the code for the form.

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

In the urls.py of the django-registration, there is pattern for the login page. It uses the default django.contrib.auth.views.login view for the login.

So in urls.py I got this:

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/

The django.contrib.auth.views.login takes in a template_name and form to use. I'm passing those in as you can see above. I'm setting the template and setting the authentication_form to be the one provided by the django-email-usernames.

Then when browse to the login page I get the following error:

TemplateSyntaxError at /accounts/login/
Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'

Template error

In template /Users/Amir/.virtualenvs/scvd/lib/python2.6/site-packages/registration_defaults/templates/registration/login.html, error at line 16
Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'

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  

I'm pretty stuck. I am pretty sure I did urls.py configuration correctly. I don't understand the erro that is happening with line 16 ({{ form.password }}) in the template.

Please let me know what else I can provide to clarify my question. Thank you so much for your help in advance.

, auth_views.login, {'template_name': 'registration/login.html', 'authentication_form': EmailLoginForm}, name='auth_login'), ...

The django.contrib.auth.views.login takes in a template_name and form to use. I'm passing those in as you can see above. I'm setting the template and setting the authentication_form to be the one provided by the django-email-usernames.

Then when browse to the login page I get the following error:

TemplateSyntaxError at /accounts/login/
Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'

Template error

In template /Users/Amir/.virtualenvs/scvd/lib/python2.6/site-packages/registration_defaults/templates/registration/login.html, error at line 16
Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'

I'm pretty stuck. I am pretty sure I did urls.py configuration correctly. I don't understand the erro that is happening with line 16 ({{ form.password }}) in the template.

Please let me know what else I can provide to clarify my question. Thank you so much for your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2024-12-09 13:11:22

似乎你的 clean() 不太清楚,

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

有 .get 丢失,

 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)

Seems that your clean() is not so clear

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

there is .get missing

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

it's good practice to use

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

then you can use

 user = authenticate(username=x, password=y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文