Django breaks on urls containing "logout" - django

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'

Related

why does <myproject>/accounts/profile/ show the <myproject>/profile/ page

Using django-allauth, after a successful login a user is redirected to http://<myproject>/accounts/profile/... However this URL doesn't exists, but yet it still successfully shows view from http://<myproject>/profile/
settings.py
urlpatterns = [
path('', include('pages.urls')),
path('admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url('album/', include('albums.urls')),
url('profile/', include('user_profile.urls')),
]
user_profile\urls.py
urlpatterns = [
path('', views.profile, name='profile'),
]
using show_urls I don't see anything for /accounts/* which would call the view.profile view
/accounts/confirm-email/ allauth.account.views.EmailVerificationSentView account_email_verification_sent
/accounts/confirm-email/<key>/ allauth.account.views.ConfirmEmailView account_confirm_email
/accounts/email/ allauth.account.views.EmailView account_email
/accounts/inactive/ allauth.account.views.AccountInactiveView account_inactive
/accounts/login/ allauth.account.views.LoginView account_login
/accounts/logout/ allauth.account.views.LogoutView account_logout
/accounts/password/change/ allauth.account.views.PasswordChangeView account_change_password
/accounts/password/reset/ allauth.account.views.PasswordResetView account_reset_password
/accounts/password/reset/done/ allauth.account.views.PasswordResetDoneView account_reset_password_done
/accounts/password/reset/key/<uidb36>-<key>/ allauth.account.views.PasswordResetFromKeyView account_reset_password_from_key
/accounts/password/reset/key/done/ allauth.account.views.PasswordResetFromKeyDoneView account_reset_password_from_key_done
/accounts/password/set/ allauth.account.views.PasswordSetView account_set_password
/accounts/signup/ allauth.account.views.SignupView account_signup
/accounts/social/connections/ allauth.socialaccount.views.ConnectionsView socialaccount_connections
/accounts/social/login/cancelled/ allauth.socialaccount.views.LoginCancelledView socialaccount_login_cancelled
/accounts/social/login/error/ allauth.socialaccount.views.LoginErrorView socialaccount_login_error
/accounts/social/signup/ allauth.socialaccount.views.SignupView socialaccount_signup
only the actual /profile/ url...
/profile/ user_profile.views.profile profile
help me to understand why /accounts/profile/ is showing the /profile/ view...
Actually redirecting to /accounts/profile/ is default behavior in django. In django global settings, it is defined as LOGIN_REDIRECT_URL. Django expects you to implement this page/url. Django django-allauth also uses same setting to redirect user after login.
If you want to redirect to any other page like /profile/, override this setting in your project settings.
LOGIN_REDIRECT_URL = '/profile/'
Or if you want django-allauth not to redirect at all set LOGIN_REDIRECT_URL = False in your settings as described here.
Your profile url path is not being limited to match only the start of the url:
url('profile/', include('user_profile.urls')),
This will match anything like gibberishprofile/ for example, including accounts/profile/. If you change the url config to
url('^profile/', include('user_profile.urls')), # note the ^ before profile
Then only profile/ will match.

Django - Logout Redirect to Login Page

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.

how to set admin.site.urls as home page in django

some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx
For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.
Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.

I can't get my Django URLs to work

My code is as follows:
root urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('app.urls')),
]
My applications url is like so
urlpatterns = [
url(r'^$',views.login, name='login'),
url(r'^homepage/$', views.homepage, name='homepage'),
]
The first screen the user will see is the login screen (views.login). At the moment I just want to set the login button to be a url that takes them to the homepage (just for practice) but it doesnt seem to work.
The login html is like so
<button type="button">Log-In</button>
This should go to my urls page above...find the name 'homepage' and take me to views.homepage which is as so:
def homepage(request):
return render(request, 'application/homepage.html', {})
but my homepage doesnt get rendered and I have absolutely no idea why its driving me crazy.
Any help would be appreciated.
This is nothing to do with Django, but a pure HTML problem. You can't put a link inside a button. A button needs to be part of a form, and submits to the action value of that form. Either do that, and take the a tag out; or, remove the button, and just use the a.

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')
)