DRF social authentication - django

Im implementing drf social oauth2 and while accessing the URL - localhost:8000/auth/login/facebook/ I get 'drf' is not a registered namespace, No ReverseMatch error and when I change my namespace to social, I get 'social' is not a registered namespace.
#URLPatterns
urlpatterns = [
path("admin/", admin.site.urls),
path('auth/', include('drf_social_oauth2.urls', namespace='social')),
path('api/', include("users.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#Installed Apps
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# rest
"rest_framework",
"corsheaders",
"rest_framework_simplejwt",
# Oauth
"oauth2_provider",
"social_django",
"drf_social_oauth2",
# apps
"accounts",
"inventory",
"cart",
"orders",
"users",
]

Related

Generating all third-party Django apps endpoints with drf-yasg

Is there a way for generating all third-party apps endpoints with drf-yasg?
So I have installed social_django app and want to include all URLs it provides to swagger UI
My urlpatterns
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('', include('user.urls')),
re_path(r'^$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
re_path(r'', include('social_django.urls', namespace='social')),
re_path(r'accounts/', include('django.contrib.auth.urls')),
]
Schema
schema_view = get_schema_view(
openapi.Info(
title="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact#snippets.local"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
Those are that defined by me, but I also want to see here /login/facebook/ , /login/google-oauth2/ etc. Or for example simple /admin/ that Django provides.
How can I do this?

csrf fail on restAuth with TokenAuthentication and corsheaders

I am trying to make auth apis for my django+react project. I have used restauth package. When I hit the api with postman, it is successful (200) on get request but fails on post request due to csrf, I looked on internet and disabled sessionauthentication but still in vain .Here is my settings.py minimal settings for restauth
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
"whitenoise.runserver_nostatic"
]
LOCAL_APPS = [
"users.apps.UsersConfig",
"corsheaders",
]
THIRD_PARTY_APPS = [
"rest_framework",
"rest_framework.authtoken",
"rest_auth_custom",
"rest_auth_custom.registration",
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.google",
]
INSTALLED_APPS += LOCAL_APPS + THIRD_PARTY_APPS
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ORIGIN_ALLOW_ALL = True
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
# 'rest_framework.authentication.SessionAuthentication'
)
}
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
here is the postman view. let me know if you need anything additional
You need to add a header X-CSRFToken while requesting a method that can potentially change a state on the server-side. For example, POST, PUT, PATCH, DELETE etc.
"headers" : { "X-CSRFToken":"token" }
The value of X-CSRFToken is set by the server when the user is authenticated. The server uses a header named set-cookie having a name csrf and value somevalue that can be used later in X-CSRF-Token header. Here is a link to complete documentation.
https://docs.djangoproject.com/en/3.1/ref/csrf/#acquiring-the-token-if-csrf-use-sessions-and-csrf-cookie-httponly-are-false

NoReverseMatch at /login/ Reverse for 'password_reset' not found. 'password_reset' is not a valid view function or pattern name

I'm having trouble with the login page for my django project.
Everything works perfectly fine with localhost but after deploying and going live it has this password_reset error.
I have googled for 3-4 hours now but I do not have a solution for this error.
It works perfectly fine on localhost but not live.
You can see it on roasitas.com/login/ if you want to see the problem
P.S: I have set to DEBUG = False, the error message still appeared
Why?
Thanks everyone!
image here
urlpatterns = [
path('',include('news.urls', namespace="news")),
path('admin/', admin.site.urls),
path('register/', users_view.register, name='register'),
path('profile/', users_view.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('password-reset/',
auth_views.PasswordResetView.as_view(
template_name='users/password_reset.html',
subject_template_name='users/password_reset_subject.txt',
success_url=reverse_lazy('users:password_reset_done')
),
name='password_reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(
template_name='users/password_reset_done.html'
),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(
template_name='users/password_reset_confirm.html'
),
name='password_reset_confirm'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(
template_name='users/password_reset_complete.html'
),
name='password_reset_complete'),
# path('web/', include('django.contrib.auth.urls')),
path('polls/', include('polls.urls')),
path('api/', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('/tinymce/', include('tinymce.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How to disable django REST framework docs page

I am currently using Django REST framework and it works very well for me. The problem is that the API documentation page is public in production and I would like it to only be seen in development (localhost) or by administrators users.
This is my urls.py:
schema_view = get_schema_view(title='My API')
router = DefaultRouter()
# router.register(...)
urlpatterns = [
url(r'', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API service')),
url(r'^schema/$', schema_view),
]
PS: I'm using the version 3.9.2
You can always check for the DEBUG value and do stuff if it is set (which is set to True in development mode). For example:
from django.conf import settings
urlpatterns = [
url(r'', include(router.urls)),
url(r'^schema/$', schema_view),
]
if settings.DEBUG:
urlpatterns += [
url(r'^docs/', include_docs_urls(title='My API service'))
]

Django REST Framework URLs with Django 2.0

I'm trying to set up Django REST Framework with Django 2.0 project which means url(r'^something/' ... has been replaced with path(something/ ....
I'm trying to work out how to set up my rest_framework patterns.
This is what I have:
router = routers.DefaultRouter()
router.register(r'regulations', api.RegulationViewSet)
router.register(r'languages', api.LanguageViewSet)
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
If I go to http://127.0.0.1:8000/regulations I simply get:
Page not found (404)
How should I set up my urlpatterns?
urlpatterns = [
...
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
with path('', include(router.urls)), you can get:
http://127.0.0.1:8000/regulations/
http://127.0.0.1:8000/languages/
with
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
you can get:
http://127.0.0.1:8000/api-auth/{other paths}
After registering the router you have to include it in the urlpatterns. The way how #Ykh suggested is technically correct, but with regards to content is missing the point.
urlpatterns = [
# here you include your router
path('', include(router.urls)),
# here you include the authentication paths
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Now you'll have the following routes:
http://localhost:8000/regulations/
http://localhost:8000/languages/
plus:
http://localhost:8000/api-auth/{other paths}