Password reset function in django admin is not working - python

urls.py
from django.contrib import admin
from django.urls import path,include
from django.urls import re_path
from App.views import *
from.router import router
from django.views.generic import TemplateView
from rest_framework_simplejwt.views import ( TokenObtainPairView,TokenRefreshView)
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', TemplateView.as_view(template_name="social_app/index.html")), #social_app/index.html
path('admin/', admin.site.urls), #admin api
path('api/',include(router.urls)), #api
path('accounts/', include('allauth.urls')), #allauth
re_path('rest-auth/', include('rest_auth.urls')), #rest_auth
path('api-auth/', include('rest_framework.urls')),
re_path('/registration/', include('rest_auth.registration.urls')),
path('api/token/', MyObtainTokenPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('jobview/',job),
path('timelog/',timelogview),
path('chaining/', include('smart_selects.urls')),
path('admin/password_reset/',auth_views.PasswordResetView.as_view(),name='admin_password_reset',),
path('admin/password_reset/done/',auth_views.PasswordResetDoneView.as_view(),name='password_reset_done',),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(),name='password_reset_confirm',),
path('reset/done/',auth_views.PasswordResetCompleteView.as_view(),name='password_reset_complete',),
] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
I have given the password reset function in the admin page login, it is showing as "Forgotten your password or username?" in the page but when I click it the url changes http://127.0.0.1:8000/admin/login/?next=/admin/password_reset/ to this but the same login page is loading i didnt redirected to any other reset page.
I have tried but couldn't able to fix it, kindly help me to fix this issue.

Add all urls with admin before path('admin/', admin.site.urls)
Then add this line of code in your admin.py file and run the server and connect with your admin account then go to Site model and edit domain example.com with http://127.0.0.1:8000 when you're in development (in production it will be your server link) and edit name example.com with whatever name you want for your site.
from django.contrib.sites.models import Site
from django.contrib import admin
#admin.register(Site)
class SiteAdmin(admin.ModelAdmin):
list_display = ('domain', 'name')
search_fields = ('domain', 'name')

Related

Prevent admin url from being overridden Django

I have a urls file that looks like this, however I cannot access the admin page. Is there any way to specify that the admin URL should never be overridden?
from django.contrib import admin
from django.urls import path
from latency import views
urlpatterns = [
path("admin/", admin.site.urls),
path("<str:chain>", views.regions),
path("<str:chain>/<str:region>", views.chain),
path("", views.index),
]```

Why DRF giving me 404

I am keep getting 404 in DRF whenever I send request from postman even though the url is correct.
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('backend.urls'))
]
backend/urls.py
from django.urls import path, include
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('/post', views.PostView, basename='post')
urlpatterns = [
path('', include(router.urls)),
]
From postman I am sending post request on the endpoint http://127.0.0.1:8000/post but I am keep getting 404 not found error. What am I doing wrong?
This is what works for me.
Create a app named backend using python manage.py startapp {app_name} where app_name is backend
Add the newly created app to INSTALLED_APPS in your project settings.py
In your project url.py file do this:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("backend.urls")),
]
In your app urls.py file. (where app is backend):
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
app_name = 'backend'
urlpatterns = [
path('', include(router.urls)),
]
Then in your app(backend) views.py define PostView
This should work.The endpoint should be at :
http://localhost:8000/post/ .
if your using port 8000
Try to change backend/urls.py:
import views
from django.urls import path, include
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
urlpatterns = router.urls

How to redirect CBV login_required to a specific route?

I have a HomeView Class which I added a login_required, but I don't know how to redirect it with the custom login page that I made.
Here are the urls.py and settings.py in my project:
myblog\urls.py
from django.contrib import admin
from django.urls import path,include
from .settings import DEBUG, STATIC_URL, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT
from django.conf.urls.static import static
# from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myblog_app.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
# + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
if DEBUG:
urlpatterns += static(STATIC_URL,document_root =STATIC_ROOT )
urlpatterns += static(MEDIA_URL,document_root = MEDIA_ROOT)
myblog_app\urls.py
from django.urls import path
from django.contrib.auth.decorators import login_required
from .views import HomeView,ArticleDetailView,AddPostView,UpdatePostView,DeletePostView,AddCategoryView,CategoryView,CategoryListView,LikeView,AddCommentView
urlpatterns = [
path('', login_required(HomeView.as_view()),name='home'),
path('article/<int:pk>', ArticleDetailView.as_view(),name='article-detail'),
path('add_post/', AddPostView.as_view(),name='add_post'),
path('article/<int:pk>/comment/', AddCommentView.as_view(),name='add_comment'),
path('article/edit/<int:pk>', UpdatePostView.as_view(),name='update_post'),
path('article/<int:pk>/remove', DeletePostView.as_view(),name='delete_post'),
path('add_category/', AddCategoryView.as_view(),name='add_category'),
path('category/<str:cats>', CategoryView,name='category'),
path('category-list', CategoryListView,name='category_list'),
path('like/<int:pk>', LikeView,name='like_post'),
]
members/urls.py
from django.urls import path
from .views import UserRegisterView,UserEditView,UserLoginView,PasswordsChangeView,ShowProfilePageView,EditProfilePageView
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('login/',UserLoginView, name='login'),
path('register/',UserRegisterView.as_view(), name='register'),
path('edit_profile/',UserEditView.as_view(), name='edit_profile'),
# path('password/',auth_views.PasswordChangeView.as_view(template_name='registration/change-password.html')),
path('password/',PasswordsChangeView.as_view(template_name='registration/change-password.html')),
path('password_success/',views.password_success,name='password_success'),
path('<int:pk>/profile/',ShowProfilePageView.as_view(),name='show_profile_page'),
path('<int:pk>/edit_profile_page/',EditProfilePageView.as_view(),name='edit_profile_page'),
]
settings.py
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL='home'
LOGOUT_REDIRECT_URL='login'
What it's doing now is that it basically just redirects to the
path('', login_required(HomeView.as_view()),name='home') and it doesn't know where to get the login url from, so instead it should redirect to path('login/',UserLoginView, name='login') but I have no idea how to do it
Okay I found the answer, so I just add in login_url='([login url path])' so that it redirects to the login url that I wanted... now it should look like this:
path('',login_required(HomeView.as_view(),login_url='/members/login/'),name='home'),

Error in Django: Using the URLconf defined in django_test.urls, Django-3 tried these URL patterns, in this order:

I've tried setting these URL's in this project, but I get a 404 when trying to access the registration.html file and the api-auth URL that is related to a REST API
For reference:
index = app
django_test = site
index/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', views.homepage, name="homepage"),
path('/register', views.registration, name="registration"),
path('api', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
]
django_test/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')),
path('admin/', admin.site.urls)
]
index/views.py
from django.shortcuts import render
def homepage(request):
return render(request,"home/homepage.html")
def registration(request):
return render(request, 'home/registration.html')
All of our HTML files are located in index/templates/home directory
Can you try changing name="registration" to name="register" ?
Change the URL "register/" to "/registration"

Why my applied changes in urls.py is not reflecting after I tried reloading the Django web page?

In views.py
from django.http import HttpResponse
def home_page(request):
return HttpResponse("<h1>Hello, World</h1>")
In urls.py
from django.contrib import admin
from django.urls import path
from .veiws import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Starting development server at http://127.0.0.1:8000/
Your import says .veiws instead of .views

Categories