DEV Community

Phansivang
Phansivang

Posted on

Django User Login and Logout View

Login.html

<form method="POST">
    {% csrf_token %}
    {{ form }}
    <button type="submit" class="btn btn-outline-info">LOGIN</button>
        <a href="{% url 'register' %}">SIGN UP</a>
    </form>

Enter fullscreen mode Exit fullscreen mode

urls.py (main project)

from django.contrib import admin
from django.urls import path
from django.contrib.auth import views as user_views

urlpatterns = [
    path('admin/', admin.site.urls),
path('login/',user_views.LoginView.as_view(template_name='app/login.html'),name='login'),
    path('logout/',user_views.LogoutView.as_view(),name='logout'),
]


Enter fullscreen mode Exit fullscreen mode

setting.py

LOGIN_REDIRECT_URL = 'home' ('Put your URL you want to redirect')
LOGOUT_REDIRECT_URL = 'login' ('Put your URL you want to redirect')
Enter fullscreen mode Exit fullscreen mode

Enjoy !!

Top comments (0)