Нow to solve this problem ModuleNotFoundError: No module named 'rest_frameworkdrf_yasg'? - drf-yasg

My requirements.txt:
asgiref==3.6.0
certifi==2022.12.7
charset-normalizer==3.0.1
coreapi==2.3.3
coreschema==0.0.4
Django==4.1.6
django-filter==22.1
django-jazzmin==2.6.0
djangorestframework==3.14.0
djangorestframework-jsonapi==6.0.0
drf-yasg==1.21.5
drf-yasg-json-api==0.9.1
idna==3.4
inflection==0.5.1
itypes==1.2.0
Jinja2==3.1.2
MarkupSafe==2.1.2
packaging==23.0
pytz==2022.7.1
requests==2.28.2
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.7
sqlparse==0.4.3
uritemplate==4.1.1
urllib3==1.26.14
My settings.py/INSTALLED APPS:
INSTALLED_APPS = [
'jazzmin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'book',
'project',
'user',
'rest_framework'
'drf_yasg',
]
I have already registered rest_framework and drf_yasg.
I don't know what else I can do.
It seems that I imported everything and registered it in settings.py and in requirements.txt

Related

(401 error) After installing dj-rest-auth and deleted a user from user table, server keeps returning 401 error for all endpoints

I'm trying to use login/connect social accounts using dj-rest-auth. I use postman to test api request.
I installed according to dj-rest-auth doc.
I worked smoothly until I delete a user from user table.
My testing process steps:
dj-rest-auth/login [user 1 credential] -> get correct result [user 1]
delete [user 1] in DB
dj-rest-auth/login [user 1 credential] -> user not found [401 unauthorize]
dj-rest-auth/login [user 2 credential] -> user not found [401 unauthorize] ( user 2 exist in table)
all other api get 401 error (user not found [401 unauthorize])
Do I need to add some setting for this case?
I have no idea How deleted user affected to all other api
Please advise me. Thank you.
I think you need to add those settings in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken', # this
'dj_rest_auth', # this
'myapp',
]
REST_USE_JWT = True # this (apply JWT auth)
JWT_AUTH_COOKIE = 'jwt-auth' # this
Check also this settings in project urls.py
from django.contrib import admin
from django.urls import path,include
from django.urls import path
urlpatterns = [
path("", include("myapp.urls")),
path('admin/', admin.site.urls),
path('', include('dj_rest_auth.urls')), # this
]
if still get an error add this
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
)
}
It is solved after I removed (JWT_AUTH_COOKIE = 'auth').

Django REST API don't see a URL

I think I have an import problem. After learning through all the tutorials and was able to get up to this point. I just want to see my database with .objects.all(). The problem is the code 404 where it can't see "receipts.url"
makemigrate and migrate is all done without any issues.
server was running with access to admin.
I am getting this problem, but I have it all mapped out. Please help and thanks again.
"Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/receipts/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
admin/
receipts/ [name='all']
The current path, receipts/, didn’t match any of these."
My project name is "project", my app name is "receipts". "project", "receipts" and "manage.py" all are in the same level.
I can see my database in the admin page, and as well as Mongodb compass.
Here are all of my files:
project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'receipts',
'rest_framework',
]
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('receipts/', include('receipts.urls')),
]
receipts/urls.py
from django.urls import path
from . import views
from .views import CustomersViews
urlpatterns = [
path(" ", CustomersViews.as_view(), name='all'),
receipts/views.py
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .serializers import CustomerSerializer
from .models import Customer
class CustomersViews(APIView):
def get_all(self, request):
customers = Customer.objects.all()
serializer = CutomerSerializer(customers, many=True)
return Response(serializer.data)
receipts/serializers.py
from rest_framework import serializers
from .models import Customer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ('firstname',
'lastname',
'date',
'address',
)

I am unable to import Static Files in Django website. Any on please explain what i am missing in code?

I am facing issue while loading static files in the project. I have created the folder named as static, added (JS,CSS and Image files to the folder). I have provided the path in settings.py file. Please any one explain me that what i am missing and where i have to do modifications.
STATIC_URL = "/static/"
STATICFILES_DIR = [os.path.join(BASE_DIR, "static")]
LOGIN_URL = "account:login"
LOGIN_REDIRECT_URL = "public:index"
LOGOUT_REDIRECT_URL = "public:index"
Installed Apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website.apps.accounts',]
maybe you missed to put the STATIC_URL in your urls.py main file, as is shown in the official documentation
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Link official documentation
I have already added the static root directory with in URLS.py file

Import "users" could not be resolved Pylance(reportMissingImports)

I wanted to import my views.py file from my app "users" and the urls.py of the project. However, I'm getting an error that the "Import "users" could not be resolved".
Picture of the problem
The urls.py file:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from users import views as user_views # Why is there an error?
urlpatterns = [
# Admin Page
path('admin/', admin.site.urls),
# Homepage
path('', include('blog.urls')),
path('register/', user_views.register, name = 'register'),
path('profile/', user_views.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'),
]
The app is also in "INSTALLED_APPS" in the settings.py file:
INSTALLED_APPS = [
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Folder structure
This is probably an easy-to-solve problem, however, I'm fairly new to Django and don't really understand why it says this is an error. In a similar project, everything works fine.
No error in a similar project, where I did everything the same way as far as I remember.
In your root directory in the vscode left pane, there is a folder .vscode.
click on that and the following key:value pair of your directories ex:["./users", "./blog"] it will resolve relative imports. It worked for me.
{
"python.analysis.extraPaths": ["./users", "./blog"]
}
This error has come because you have app users and blog which you have not written in settings.py file in INSTALLED_APPS
INSTALLED_APPS = [
'blog',
'users',
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and include urls also in project urls.py

Django not picking up the functions

I have tried every thing from making new function and saving every file thrice but nothing seems to work. And its my first time using Django if any one find anything please point it out...
Note: Django ver 3.1
Server Error:
path('', views.home_view),
AttributeError: module 'pages.views' has no attribute 'home_view'
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products', 'pages',
]
views.py
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("Hello world")
urls.py
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_view),
]
in urls.py change "from pages import views" to "from . import views"

Resources