1

I need one help. I need to implement the forget password functionality using Django. I am using the Django signup and login page. My code is below:

login.html:

{% extends 'base.html' %}

{% block content %}
 <h2>Log in</h2>
  {% if form.errors %}
    <p style="color: red">Your username and password didn't match. Please try again.</p>
  {% endif %}
  <form method="post">
    {% csrf_token %}
    {% for field in form %}
      <p>
        {{ field.label_tag }}<br>
        {{ field }}<br>
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
        {% if field.help_text %}
          <p><small style="color: grey">{{ field.help_text }}</small></p>
        {% endif %}
      </p>
    {% endfor %}
    <button type="submit">Log in</button>
    <a href="{% url 'signup' %}">New to My Site? Sign up</a>
  </form>
  {% endblock %}

views.py:

class Signup(View):

    """ this class is used for user signup """

    def get(self, request):
        """ this function used to get the sign up form """
        form = UserCreationForm()
        return render(request, 'plant/signup.html', {'form': form})

    def post(self, request):
        """ this function used for post the sign up data """
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')


class AuthLogin(View):

    """ Its for login """

    def get(self, request):
        """ this function used to get the login form """
        form = AuthenticationForm()
        return render(request, 'plant/login.html', {'form': form})

    def post(self, request):
        """ this function used for post the login data """
        form = AuthenticationForm(None, request.POST or None)
        if form.is_valid():
            login(request, form.get_user())
        return redirect('/')

urls.py:

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name="plant/index.html")),
    url(r'^logout/$', auth_views.logout,
        {'next_page': 'login'}, name='logout'),
    url(r'^login/$', core_views.AuthLogin.as_view(), name='login'),
    url(r'^signup/$', core_views.Signup.as_view(), name='signup'),
]

In login page I should have the forget password link. When user will click on it, the reset password page will open and another one condition is after trying 3 wrong attempt the forget password button will be invisible for 1 hour.

1

1 Answer 1

2

Do not create this functionality yourself but use the built-in Django auth views. https://docs.djangoproject.com/en/1.11/topics/auth/default/#built-in-auth-views

The only thing you need to do is add the contrib auth urls to your project:

urlpatterns = [
    url('^', include('django.contrib.auth.urls')),
]

This gives you all views like login, logout, password reset etc.

If you want to customise the templates, copy the templates form /path/to/site-packages/django/contrib/admin/templates/registration/ to your app project/app/templates/registration/ and make any customisations there.

If your app is listed before 'django.contrib.auth' in INSTALLED_APPS (it should always be like that) your custom templates will be picked first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.