django login redirect

To implement a login redirect in Django, you can follow these steps:

  1. Import the necessary modules:
  2. from django.contrib.auth.views import LoginView
  3. from django.urls import reverse_lazy

  4. Create a login view by subclassing the LoginView class:

  5. class CustomLoginView(LoginView):
  6. template_name = 'login.html'

  7. In your urls.py file, define the URL pattern for the login view:

  8. from .views import CustomLoginView
  9. urlpatterns = [
  10. path('login/', CustomLoginView.as_view(), name='login'),
  11. ]

  12. In your settings.py file, specify the URL to redirect to after a successful login:

  13. LOGIN_REDIRECT_URL = 'home'

  14. Optionally, you can also specify the URL to redirect to after a user logs out:

  15. LOGOUT_REDIRECT_URL = 'login'

  16. In your login template (login.html), make sure to include the form for user input and submit button:

  17. <form method="POST">
  18. {% csrf_token %}
  19. {{ form.as_p }}
  20. <button type="submit">Login</button>
  21. </form>

  22. Finally, you can customize the login view by adding any additional functionality or styling as per your requirements.

Note: Make sure you have the necessary templates and HTML files in place for the login and redirect pages.