Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom PasswordResetForm

I am trying to use the PasswordResetForm built-in function.

As I want to have custom form fields, I wrote my own form:

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

And here is my view in 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='[email protected]', email_template_name='registration/password_reset_email.html')
            print "EMAIL SENT"
        else:
            print "UNKNOWN EMAIL ADRESS"

And my email template is:

{% 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 %}

The problem is that I have a 'NoneType' object has no attribute 'get_host' error... The traceback log is telling me that in current_site = RequestSite(request) , request is None. Maybe I have something else to add in my save() in views.py ?

When I use the following method without custom fields in my form and built-in views, all is working good: http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html

like image 823
Erwan Avatar asked May 11 '14 09:05

Erwan


1 Answers

So you're getting that error because it's trying to call a method on a instance that's set to None. Here's the correct view you should use:

@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='[email protected]', email_template_name='registration/password_reset_email.html', request=request)
            print "EMAIL SENT"
        else:
            print "UNKNOWN EMAIL ADRESS"

Another option would be to enable the Django Sites Framework. Then you wouldn't have to pass in request because get_current_site would return the site current instance. Here's a link to that logic.

like image 160
schillingt Avatar answered Dec 06 '22 13:12

schillingt