Django can't overwrite logout view - django

I use django.contrib.auth.urls to logout user. I want and I have view logout but I don't know why when I go to accounts/logout/ url django does not use mine, but default view for logout.
from django.urls import path, include
import django.contrib.auth.urls
from . import views
app_name = 'accounts'
urlpatterns = [
path('', views.index, name='index'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/sign_up/', views.sign_up, name="sign-up"),
path('accounts/logout/', views.logout_view, name="logout"),
]
def logout_view(request):
print(request.user)
context = {'user2': request.user}
logout(request)
return render(request, 'accounts/logged_out.html', context)

Since path('accouts/', …) is ordered first it will fire the logout/ path [GitHub] in the django.contrib.auth.urls module.
If you change the order, it will thus first match with the logout_view and fire that view:
urlpatterns = [
path('', views.index, name='index'),
path('accounts/logout/', views.logout_view, name='logout'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/sign_up/', views.sign_up, name='sign-up'),
]

Related

django.urls.exceptions.NoReverseMatch: 'admin' is not a registered namespace

I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"

django url is giving page not found (404) error

I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]

Django Rest-Framework Login View

It should display the Django REST framework login page when i press the login button in the browsable api, but it doesn't.
i got this HTTP response : "GET /api-auth/login/?next=/api-auth/login/ HTTP/1.1" 200 5415
Base URLs.py:
urlpatterns = [
path('', include('RunKeeper.urls')),
path('api-auth/', include('rest_framework.urls')),]
rest-framework URLs.py:
from __future__ import unicode_literals
from django.conf.urls import url
from django.contrib.auth import views
template_name = {'template_name': 'rest_framework/login.html'}
app_name = 'rest_framework'
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), template_name, name='login'),
url(r'^logout/$', views.LogoutView.as_view(), template_name, name='logout'),
I've included the rest-framework in the installed apps.
How can i solve the problem
Add BasicAuthentication and SessionAuthentication classes in DEFAULT_AUTHENTICATION_CLASSES class as below.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
And url patterns as below
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(template_name='rest_framework/login.html'), name='login'),
url(r'^logout/$', views.LogoutView.as_view(), name='logout'),
]
Why you have login template in logoutView?
Remove that one as well.

Error W005 URL namespace isn't unique

'Copied this code from the Django tutorial into my app's urls.py file...
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
When I start my server it produces the following error...
(urls.W005) URL namespace 'polls' isn't unique. You may not be able
to reverse all URLs in this namespace
I've tried using some other name besides 'polls' but with the same result. What am I doing wrong?
Check your root urls file, and be sure to have unique name:
(django 2+)
For example:
mysite/urls.py
-->
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), #polls.urls is unique
path('admin/', admin.site.urls), #admin.site.urls is unique
]
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9a-f-]+)/$',views.detail,name='detail'),
url(r'^(?P<question_id>[0-9a-f-]+)/results/$',views.results,name='results'),
url(r'^(?P<question_id>[0-9a-f-]+)/vote/$',views.vote,name='vote'),
]
Write your url file like shown above.

Error Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name.

I keep getting this error
Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name.
i am trying to use the default view from
from django.contrib.auth.views
from django.conf.urls import url
from accounts import views
from django.contrib.auth.views import (login,
logout,
password_reset,
password_reset_done,
password_reset_confirm,
)
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"), # views define a link to connecct this to views then to template
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^reset-password/$', password_reset, name= 'password_reset'),
url(r'^reset-password/done/$', password_reset_done, name='password_reset_done'),
url(r'^reset-password/confirm/$', password_reset_confirm, name='password_reset_confirm')
]
Please anybody help me out... i have check all... but couldnt find the fault.
it'll work if you just use path('', include('django.contrib.auth.urls')) in your main urls.py not the one in your app_name.
plz try this
be carefull to watch for some errors like not adding the $ at the end of some urls or maybe adding it. also pay attention to where success_url is given because the internal code uses it and is lost without it.
here in this code the application that I chose to manage users is called accounts, you can call yours anything.
the templates should be put inside a directory that is recognized by django otherwise it won't find them here is the code for settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
and here is the full urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from accounts.views import signup
urlpatterns = [
url(r'signup/$', signup, name='signup'),
url(r'login/$',auth_views.LoginView.as_view(template_name='login.html')),
url(r'logout/$',auth_views.LogoutView.as_view(template_name='logout.html')),
url(r'password_change/$',auth_views.PasswordChangeView.as_view(template_name='password_change.html',success_url='/accounts/password_change_done')),
url(r'password_change_done/',auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html')),
url(r'password_reset/$',auth_views.PasswordResetView.as_view(template_name='password_reset.html',email_template_name='password_reset_email.html',subject_template_name='password_reset_subject.txt',success_url='/accounts/password_reset_done/',from_email='support#yoursite.ma')),
url(r'password_reset_done/',auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html')),
url(r'password_reset_confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html',success_url='/accounts/password_reset_complete/')),
url(r'password_reset_complete/',auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html')),
]
I had the same problem but solved it by adding the success_url parameter in PasswordResetView :
Add success_url parameter of Class Based View PasswordResetView. This will replace default route of password_reset_done
url(r'^reset/$',PasswordResetView.as_view(
template_name='password_reset.html',
email_template_name='password_reset_email.html',
subject_template_name='password_reset_subject.txt',
...
success_url = reverse_lazy('accounts:password_reset_done')
...
...
),name='password_reset'),
These occurs when Django is updated from 1.xx to 2.xx (well, as in my own case).
This is my solution. I fixed it by including a dictionary with key "post_change_redirect" with it's value pointing to the password_change_done url. For password resetting use post_rest_redirect.
from django.contrib.auth import views as v
from django.conf.urls import url
urlpatterns =
......
url(r"password-change/$", v.password_change, {"post_change_redirect":"account:password_change_done"}, name="password_change")
.........
]
This particular error may also occur when Django was upgraded from version 1.xx to version 2.xx
To fix this:
Add this line in main url.py:
url(r'^', include('django.contrib.auth.urls'))
And then replace functions with classes in 'your_app/urls.py'. E.g.
password_reset => PasswordResetView.as_view()
For example:
url(r'^reset-password/$', PasswordResetView.as_view(), name='password_reset'),
url(r'^reset-password/done/$', PasswordResetDoneView.as_view(), name='password_reset_done'),
url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
url(r'^reset-password/complete/$', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
I found this code and this seems to work.
from django.conf.urls import url
from accounts import views
from django.contrib.auth.views import (login,
logout,
password_reset,
password_reset_done,
password_reset_confirm,
)
from django.contrib.auth import views as auth_views
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'accounts/registration/password_reset_email.html',
'subject_template_name':'accounts/registration/password_reset_subject.txt',
'post_reset_redirect':'accounts:password_reset_done',
'from_email':'accounts#django.com',
},name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, {'template_name': 'accounts/registration/password_reset_done.html'}, name='password_reset_done'),
in above code accounts in myapp name. where you can put your own apps name
This is for Django 1.11
You are missing an import, app_name, some templates and template routes.
Change your code for this:
from django.conf.urls import url
from accounts import views
from django.contrib.auth import views as auth_views
app_name = 'accounts' # Django 2.0+, if not add namespace = 'accounts' on the urls.py where you are including this set of urls.
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'registration/password_reset_email.html',
'subject_template_name':'registration/password_reset_subject.txt',
'post_reset_redirect':'accounts:password_reset_done',
'from_email':'accounts#django.com', # Yours
},name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, {'template_name': 'registration/password_reset_done.html'}, name='password_reset_done'),
]
Be sure in your folder "accounts" you have: /templates/registration/*.html with all your templates
For me the key info that was missing was the reverse resolve needing the name attribute set on the url entry!
path(
'password_change/done/',
auth_views.PasswordChangeDoneView.as_view(template_name='SyllabusTrackerApp/change-password-done.html'),
name="password_change_done"
),
The app_name namespace wasn't set either, but those weren't used in my setup for simplicity, so I could ignore that.
The most simple solution which I've found is as below, this may help.
for password reset link first import in urls.py file of your app.
from django.contrib.auth import views as auth_views
then add the following path in your app urls.py file
path('password_reset', auth_views.PasswordResetView, name='password_reset'),
finally you can use it in your HTML pages as below
<a href="{% url 'accounts:password_reset'%}" class="float-end">
I had same issue, I just added this to my main project url:
path('', include('django.contrib.auth.urls'))
Works like magic.