Django - Logout Redirect to Login Page - django

i am trying to redirect to login page after logout but some issues are coming.
urls.py
This is my actual logout routes and it works for me but it does not redirect me to login page
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
Changing logout.html -> login.html
It destroys the session and logout the user but the problem is that when i click logout it redirects to login page but login input fields are not showing
path('logout/', auth_views.LogoutView.as_view(template_name='users/login.html'), name='logout'),
And if i am using below path(route). It is not destroying session nor logout
path('login/', auth_views.LogoutView.as_view(template_name='users/login.html'), name='logout'),

Add in you settings file
LOGOUT_REDIRECT_URL = 'login/'
Refer https://docs.djangoproject.com/en/2.1/ref/settings/#logout-redirect-url
Also Changing logout.html -> login.html, is not required please revert it back

In my case I had to list my account/user application above admin.
before
'django.contrib.admin',
'account.apps.AccountConfig'
after
'account.apps.AccountConfig',
'django.contrib.admin',

There are multiple ways to do this.
Before doing anything, make sure to import
from django.contrib.auth import views as auth_views
from django.urls import path, reverse_lazy
Then, you can do this
path('/logout', auth_views.logout_then_login, name='logout')
You can define the login URL in settings.py like #nagesh mentioned.
There's another way to handle this and specially, comes handy when you have logout buttons at multiple places and you want the user to land to different pages after clicking different logout buttons in your interface.
path('logout/', auth_views.LogoutView.as_view(
next_page=reverse_lazy('Userauth:login') # you can use your named URL here just like you use the **url** tag in your django template
), name='logout'),
I suggest that you use reverse_lazy to define the redirect URL, it can create dynamic URL very easily and you don't have to worry if you ever change your URL structure.

Related

Two views same URL

I have a single dashboard_view URL path("", view=dashboard_view, name="dashboard").
On this page you can see the homepage unauthenticated. However, if you login, I present a modal popup to allow a user to populate a CreateForm.
The issue is that the dashboard_view doesn't have the form ( I have that in another view ). What is the best practice for this? Best for the user to have different options on the same page without having to switch pages.
You can use the login_required decorator. In login_required login_url is an optional parameter if you have declared the login path in the settings.py file. If your entire site has a login URL is the same. You can put LOGIN_URL = 'login_form_url' in the settings.py file.
from django.contrib.auth.decorators import login_required
#login_required(login_url='/login_form_url/')
def dashboard_view(request):
return render(request,'app_name/dashboard.html')

Django breaks on urls containing "logout"

I'm working on a Django web-app which has user accounts, and so has login and logout functions.
What I would like to have is mywebsite.com/accounts/login/ be the login page. This is working as expected right now.
This issue is with logout- what I would like to have is mywebsite.com/accounts/logout/ logout the user and redirect them to the login page. This, however, doesn't appear to work.
Here is my url configuration in accounts/urls.py:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', auth_views.logout, name='logout'),
]
With this configuration, login works fine. But when I go to mywebsite.com/accounts/logout/, I am just immediately sent back to the page I'm currently on.
However, if I change the logout url:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logMeOutPlease/$', auth_views.logout, name='logout'),
]
Then mywebsite.com/accounts/login works as intended and mywebsite.com/accounts/logMeOutPlease works as intended. Is there a reason the first configuration won't work?
If I however move the logout functionality to the top level (i.e just mywebsite.com/logout/), then it again works fine.
For reference, this is what is in the "top level" urls file:
urlpatterns = [
# other urls that I can't show here
url(r'^redirect/', include('mywebsite.apps.redirect.urls')),
url(r'^accounts/', include('mywebsite.apps.accounts.urls')),
url(r'^$', RedirectView.as_view(url='redirect/')),
url(r'^admin/', admin.site.urls),
]
I am using Django 2.0.6 and Python 3.5.2, and my laptop is on Ubuntu 16.04.
Ok, so from what I understand so far, that behavior is actually correct. Since, views logic was not provided in your question, I will assume that the views are currently not using any #login_required decorators and the logout view does what it says i.e. logout. So without seeing the views logic we assume there were no instructions about where to redirect to on successful logout and no decorator so opens the same page again.
So here's the advice.
In your settings.py file:-
Add:
LOGOUT_REDIRECT_URL = 'url_you_want_to_redirect_to_on_logout'
This will enable redirecting to the specified url whenever a user logs out.
And if it helps, similarly we also do have a
LOGIN_REDIRECT_URL = 'url_you_want_to_redirect_to_on_login'

How to extend django.contrib.auth views?

How I've Arranged Templates : I have placed my login.html in /templates/registration folder of Django. So, the Django takes necessary care of accounts/login ,accounts/logout url requests and renders as per request. And I haven't to code for the individual login and logout functions.
What I am trying to Achieve : I want to authenticate users at login request, when they requests the login page:
If user is anonymous user, I want to render the normal login page.
However, If the user is authenticated thats already logged in. I want to display an error and not the logged page.
I want to achieve this in the views.py and urls.py and not in the templates by:
{% if user.is_authenticated %}
{% if user.is_anonymous %}
Urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
urlpatterns=[
url(r'^register/$', views.register, name='register'),
url(r'^logout/$', auth_views.logout, {'next_page' : 'Homepage'}, name='logout'),
]
You can wrap the view with your own, which either redirects or calls the original.
def wrapped_login(request):
if request.user.is_authenticated:
return redirect('whatever')
else:
return auth_views.login(request)
Provide LOGIN_REDIRECT_URL = '/' in settings.py. Then use the following url for login page:
urlpatterns = [
url(r'^login/',
auth_views.LoginView.as_view(redirect_authenticated_user=True),
name='login'),
]
This will redirect your user to the URL provided in settings file if they try to login even after being authenticated.

how to change a default django admin login view to generate token on login to admin site

my site.py:
from django.contrib.admin import AdminSite
class OptiAdminSite(AdminSite):
def get_urls(self):
from django.conf.urls import patterns, url, include
from core import views
from django.contrib.contenttypes import views as contenttype_views
urlpatterns = patterns('',
#url(r'^$', wrap(self.index), name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
return urlpatterns
opti_site = OptiAdminSite()
I'm developing an authentication API. When user logs in to my API it generates a code which get destroyed once user hit logout.
My problem is that whenever I'm running my API and django admin site in same browser, then if I login into admin-site It automatically login me in my API too with out any token. When I try to logout in that case from my API it generates an error - 'Token does not exist'. I want to generate token when admin user login to admin-site.
I've tried to do it with above trick as in official documentation but didn't find the right way to do it.
Please suggest me the correct way to do it. Is it necessary to make a separate app for it?
Thanks! in advance.
This solution is almost complete... Almost, because you're simply creating your own admin site in opti_site variable, but probably not using it anywhere.
To make it work, you can monkey-patch default admin site with your site, using:
from django.contrib import admin
admin.sites.site = opti_site
admin.site = admin.sites.site
Remember that you must do it before root urlpatterns definition (especially before defining urls to your admin site).
Another approach is to change default admin to your admin in include of url patterns:
url(r'^admin/', include(opti_site.urls)),

Django password reset login link

I'm setting up password_reset for the admin, using the default templates, and it's all working well except for the last login link on password_reset_complete screen. I would like it to redirect to the admin login, but it's redirecting to accounts/login, which is the default url. I'm thinking I would want the accounts/login url to be different from the admin/login url if I ever needed a login that wasn't for the admin.
In the documentation it looks like there's a redirect_to_login function, but I'm not sure where I'm supposed to use it. It also looks like there's a current app argument for password_reset_complete, but my syntax isn't right if I try to add it.
I want to redirect to the admin login.
Here's what I have so far:
urlpatterns = patterns('',
('^accounts/', include('django.contrib.auth.urls')),
url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', dict(is_admin_site=True), name="admin_password_reset"),
url(r'^admin/password_reset_done/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'),
url(r'^admin/password_reset_confirm/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),
url(r'^admin/password_reset_complete/$', 'django.contrib.auth.views.password_reset_complete', name='password_reset_complete'),
#(r'^accounts/login/$', 'django.contrib.auth.views.login'),
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/login/', admin.site.login, name='login')
)