gpt4 book ai didi

Django 自定义 PasswordResetForm

转载 作者:行者123 更新时间:2023-12-04 08:25:22 31 4
gpt4 key购买 nike

我正在尝试使用 PasswordResetForm 内置函数。

由于我想要自定义表单字段,因此我编写了自己的表单:

class FpasswordForm(PasswordResetForm):
email = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'autofocus': 'autofocus'}))
class Meta:
model = User
fields = ("email")
def clean_email(self):
email = self.cleaned_data['email']
if function_checkemaildomain(email) == False:
raise forms.ValidationError("Untrusted email domain")
elif function_checkemailstructure(email)==False:
raise forms.ValidationError("This is not an email adress.")

return email

这是我在 views.py 中的观点
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def fpassword(request):
form = FpasswordForm(request.POST or None)
if form.is_valid():
email = form.cleaned_data["email"]
if function_checkemail(email):
form.save(from_email='blabla@blabla.com', email_template_name='registration/password_reset_email.html')
print "EMAIL SENT"
else:
print "UNKNOWN EMAIL ADRESS"

我的电子邮件模板是:
{% autoescape off %}
You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url "django.contrib.auth.views.password_reset_confirm" uidb36=uid token=token %}
{% endblock %}

Your username, in case you've forgotten: {{ user.username }}

Thanks for using our site!

The {{ site_name }} team.

{% endautoescape %}

问题是我有一个 'NoneType' object has no attribute 'get_host'错误...回溯日志告诉我,在 current_site = RequestSite(request) , 请求是 None .
也许我还有其他东西要添加到我的 save() 中在 views.py 中?

当我在表单和内置 View 中使用没有自定义字段的以下方法时,一切正常: http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html

最佳答案

因此,您收到该错误是因为它试图在设置为 None 的实例上调用方法。 .这是您应该使用的正确 View :

@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def fpassword(request):
form = FpasswordForm(request.POST or None)
if form.is_valid():
email = form.cleaned_data["email"]
if function_checkemail(email):
form.save(from_email='blabla@blabla.com', email_template_name='registration/password_reset_email.html', request=request)
print "EMAIL SENT"
else:
print "UNKNOWN EMAIL ADRESS"

另一种选择是启用 Django Sites Framework .那么你就不必传入请求,因为 get_current_site将返回站点当前实例。这是一个 link到那个逻辑。

关于Django 自定义 PasswordResetForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591192/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com