I set the Urls like belows.
users>urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from users import views
....
path(
"changePassword/", auth_views.PasswordResetView.as_view(), name="password_reset"
), # 비밀번호 변경 (링크 발송)
path(
"changePasswordDone/",
auth_views.PasswordResetDoneView.as_view(),
name="password_reset_done",
), # 비밀번호 변경(링크 선택 후 화면)
path(
"changePasswordConfirm/<uidb64>/<token>/",
auth_views.PasswordResetConfirmView.as_view(),
name="password_reset_confirm",
), # 비밀번호 변경(사용자 전송 링크)
path(
"changePasswordComplete/",
auth_views.PasswordResetCompleteView.as_view(),
name="password_reset_complete",
), # 비밀번호 변경(사용자 새 비밀번호 입력 후)
]
but the auth_views doesn't appear in the swagger like below images.
I guess there is some problem in the settings.py.... but don't know the detail.
config>settings.py
# Rest_Framework JWT
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": (
# 'rest_framework.permissions.IsAuthenticated', #401 에러 회피
"rest_framework.permissions.AllowAny", # 401 에러 회피
),
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework_simplejwt.authentication.JWTAuthentication",
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
),
}
could you help kindly please?
Related
I'm getting this error when trying to signup after updating dj-rest-auth package to 3.0.0 from 1.1.0.
main urls.py
from allauth.account.views import confirm_email
from dj_rest_auth.registration.views import VerifyEmailView
from dj_rest_auth.views import PasswordResetConfirmView
from django.urls import include, path, re_path][1]][1]
path("api/v1/auth/", include("dj_rest_auth.urls")),
path(
"api/v1/auth/registration/", include("dj_rest_auth.registration.urls")
),
path("api/v1/auth/registration/verify-email/", VerifyEmailView.as_view()),
path(
"api/v1/auth/password/reset/confirm/<slug:uidb64>/<slug:token>/",
PasswordResetConfirmView.as_view(),
name="password_reset_confirm",
),
re_path(
r"^api/v1/auth/accounts-rest/registration/account-confirm-email/(?P<key>.+)/$",
confirm_email,
name="account_confirm_email",
),
Full Error:
Could not import 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: cannot import name 'smart_text' from 'django.utils.encoding'
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
And this is the pip freeze in the virtual env:
(backend) PS D:\js\backend> pip freeze
asgiref==3.5.1
Django==4.0.4
django-cors-headers==3.11.0
djangorestframework==3.13.1
djangorestframework-jwt==1.11.0
djangorestframework-simplejwt==5.1.0
mysqlclient==2.1.0
PyJWT==1.7.1
pytz==2022.1
sqlparse==0.4.2
tzdata==2022.1
in the middle of the error, it addresses some lines across views.py for decorators:
from http.client import HTTPResponse
from multiprocessing import context
from django.shortcuts import render
from django.http import HttpResponse, Http404, JsonResponse
from .models import Tweet
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import api_view, permission_classes
from rest_framework import status
I'm not sure if they're even related
'rest_framework_jwt.authentication.JSONWebTokenAuthentication' this is provided by djangorestframework-jwt wich is not not being maintained anymore . Just uninstall it
instead use 'rest_framework_simplejwt.authentication.JWTAuthentication'
that comes from djangorestframework-simplejwt
1 - install djangorestframework-simplejwt : pip install djangorestframework-simplejwt
2- Your 'DEFAULT_AUTHENTICATION_CLASSES' should be like this :
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
3 - in your root urls.py file (or any other url config), include routes for Simple JWT’s TokenObtainPairView and TokenRefreshView views:
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
...
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
...
]
for more information check the official documentation
I created CustomAuthentication class, this class extend JWTAuthentication class. I put this class in file auth.py and same location with settings.py.
in settings.py i modify:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
to
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
auth.CustomAuthentication',
),
}
But it it doest not work, it throws
"Could not import 'auth.CustomAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: Module "auth" does not define a "CustomAuthentication" attribute/class."
Here is content of auth.py:
from rest_framework_simplejwt.authentication import JWTAuthentication
class CustomJWTAuthentication(JWTAuthentication):
is_valid = True
What is wrong here? please help.
Thank you very much.
The class name is 'CustomJWTAuthentication' in auth.py and you are using 'CustomAuthentication' in settings.py, change it so both are same.
I am trying to configure my Django app (Djnago 3.05) to work from a server location.
The location is http://dev.apps.net/tac.
How do i include this in each request?
I managed to do this if adding in views.py the '/tac' prefix and add a folder named 'tac' in which all the templates exist.
myviews.py
#login_required
def item_list(request):
meta = "V1.1"
if not any(field in request.GET for field in set(UserFilter.get_fields())):
user_list = item.objects.order_by("-date").all()[:50]
else:
user_list = item.objects.order_by("-date").all()
user_filter = UserFilter(request.GET, queryset=user_list)
return render(request, "tac/items/item_list.html", {"filter": user_filter})
urls.py
urlpatterns = [
url(
r"^login/$",
admin_views.LoginView.as_view(template_name="registration/login.html"),
name="login",
),
url(r"^$", TemplateView.as_view(template_name="home.html"), name="home"),
url(r"^input/$", views.inputprocess, name="inputprocess"),
url(r"^items/$", views.item_list, name="item_list"),
url(r"^items/create/$", views.item_create, name="item_create"),
url(r"^items/(?P<pk>\d+)/update/$", views.item_update, name="item_update"),
url(r"^items/(?P<pk>\d+)/delete/$", views.item_delete, name="item_delete"),
url(r"^reports/$", views.reports, name="reports"),
url(r"^timeline/$", views.timeline, name="timeline"),
url(r"^support/$", views.support, name="support"),
url(r"^dbToools/$", views.dbTools, name="dbTools"),
url(r"^logout/$", views.logout_view, name="logout_view"),
url(r"^upload/$", views.upload, name="upload"),
path("accounts/login/", auth_views.LoginView.as_view()),
path("admin/", admin.site.urls),
]
I am not sure if this is the correct way. How could I do this more efficient? Could you guys please help me here?
Thanks!
I managed to solve this using this config for urls.py (i found it in another post here)
urlpatterns = [
url(
r"^tac/",
include(
[
url(
r"^login/$",
admin_views.LoginView.as_view(
template_name="registration/login.html"
),
name="login",
),
url(
r"^$", TemplateView.as_view(template_name="home.html"), name="home"
),
url(r"^input/$", views.inputprocess, name="inputprocess"),
url(r"^items/$", views.item_list, name="item_list"),
url(r"^items/create/$", views.item_create, name="item_create"),
url(
r"^items/(?P<pk>\d+)/update/$",
views.item_update,
name="item_update",
),
url(
r"^items/(?P<pk>\d+)/delete/$",
views.item_delete,
name="item_delete",
),
url(r"^reports/$", views.reports, name="reports"),
url(r"^timeline/$", views.timeline, name="timeline"),
url(r"^support/$", views.support, name="support"),
url(r"^dbToools/$", views.dbTools, name="dbTools"),
url(r"^logout/$", views.logout_view, name="logout_view"),
url(r"^upload/$", views.upload, name="upload"),
path("accounts/login/", auth_views.LoginView.as_view()),
path("admin/", admin.site.urls),
]
),
),
]
In the project urls.py i have set these password reset url for forgot password.These url works fine .it resets the password but after reset completed in the login link , the login link redirects to the accounts/login url instead of admin/login url.How to redirect to admin/login url ??
urls.py
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',
),
path('admin/', admin.site.urls),
Add the path to the success_url argument
path(
'reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(success_url='/admin/login/'),
name='password_reset_confirm',
),
UPDATE
url(r'^reset-password/$',
PasswordResetView.as_view(template_name='accounts/reset_password.html'),
{
'email_template_name': 'accounts/reset_password_email.html',
'success_url' : reverse_lazy('accounts:reset_password_done')
},
name='reset_password')