Change the api's documentation in swagger - python

I want to use swagger for my python-Django project and I want to documente my api, I'm using the file schema.yml and even if I edit it nothing change in my swagger interface I don't know where's the problem. Any help is highly appreciated.
This is my schema.yml :
openapi: 3.0.3
info:
title: My documentation
version: 0.0.0
paths:
/add-nlptemplate:
post:
operationId: add_nlptemplate_create
....
And this is my urls.py :
from django.contrib import admin
from django.urls import path, include
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [
path('', include('ocr.urls')),
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
]
I think the problem is in the second path because it always return the default schema.yml and not the edited one.
That how it look my interface :
the title from my file schema.yml didn't show
and this is my settings file :
from pathlib import Path
from datetime import timedelta
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-)agsdvdvddltcp7#&pg3_1tkxrwo0i&1#tl%*3jlb-)m8#$keq'
DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ocr.apps.OcrConfig',
'nlp.apps.NlpConfig',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_simplejwt',
'drf_spectacular',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.TokenAuthentication',
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
'DEFAULT_PERMISSION_CLASSES':(
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

Related

ModuleNotFoundError: No module named 'drf_spectacular.views'

urls.py
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView,
SpectacularSwaggerView
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/schema/', SpectacularAPIView.as_view(), name='api-schema'),
path('api/docs/', SpectacularSwaggerView.as_view(url_name='api-schema'), name='api-docs'),
path('api/redoc/', SpectacularRedocView.as_view(url_name='api-schema'), name='api-redoc'),
path('api/user/', include('user.urls')),
path('api/recipe/', include('recipe.urls')),
]
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',
'drf_spectacular',
'core',
'user',
'recipe',
]
requirements.txt
drf-spectacular
# drf-spectacular>=0.15,<0.16
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
I'm trying to add Swagger UI in Django API, but I'm getting this error
ModuleNotFoundError: No module named 'drf_spectacular.views'.

Import "blocktunes" could not be resolved Pylance report Missing Imports

urls.py
from django.contrib import admin
from django.urls import path, include
from blocktunes import views #error in blocktunes
urlpatterns = [
path('', include('blocktunes.urls')),
path('admin/', admin.site.urls),
path('', views.UserRegister_view)
]
Why is it showing missing imports?
settings.py
INSTALLED_APPS = [
'blocktunes.apps.blocktunesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
PLEASE HELP.
Image link of structure of apps
[1]: https://i.stack.imgur.com/YhPEg.png
By adding the following command in .vscode of the folder, I was able to import blocktunes.
"python.analysis.extraPaths": ["./etherly"]

django sitemap DoesNotExist at /sitemap.xml

when i add sitemap to my Django project i got this error ..
DoesNotExist at /sitemap.xml
Site matching query does not exist.
sitemap.py :
from django.contrib.sitemaps import Sitemap
from .models import Homepage
class DynamicSitemap(Sitemap):
changefreq = "monthly"
priority = 0.5
def items(self):
return Homepage.objects.all()
url.py :
from first_app.sitemaps import DynamicSitemap
from django.contrib.sitemaps.views import sitemap
sitemaps = {'dynamic': DynamicSitemap()}
urlpatterns = [
path('sitemap.xml', sitemap , {'sitemaps': sitemaps}, name='sitemaps'),
]
settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tinymce',
'first_app',
'django.contrib.sitemaps',
]
any help and thanks
You can try to add SITE_ID = 1 above INSTALLED_APPS.
According to the answer here comment out the 'django.contrib.sites' in the settings.py file under INSTALLED_APPS solve this problem.

ImproperlyConfigured error with include to other url file

I'm using django-cookiecutter to bootstrap my project which will include api paths. Following the steps in Two Scoops of Django 1.11 to configure my urls to follow a similar pattern to this:
api/foo_app/ # GET, POST
api/foo_app/:uuid/ # GET, PUT, DELETE
api/bar_app/ # GET, POST
api/bar_app/:uuid/ # GET, PUT, DELETE
When I try to setup my project like this I'm getting the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'my_project.core.api_urls' from /Users/username/Development/my_project/my_project/core/api_urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
My current setup:
my_project.config.settings.base.py
ROOT_URLCONF = 'config.urls'
DJANGO_APPS = [
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Useful template tags:
# 'django.contrib.humanize',
# Admin
'django.contrib.admin',
]
THIRD_PARTY_APPS = [
'crispy_forms', # Form layouts
'allauth', # registration
'allauth.account', # registration
'allauth.socialaccount', # registration
'rest_framework',
]
# Apps specific for this project go here.
LOCAL_APPS = [
# custom users app
'my_project.users.apps.UsersConfig',
'my_project.core.apps.CoreConfig',
]
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
my_project.config.urls.py
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
urlpatterns = [
url(r'^users/', include('my_project.users.urls', namespace='users')),
url(r'^api/', include('my_project.core.api_urls', namespace='api')),
]
my_project.core.api_urls.urls
from django.conf.urls import url
from django.views.defaults import page_not_found
urlpattenrs = [
url(
regex=r'^users/$',
view=page_not_found,
),
]
The core app does not contain any models right now. It's just where I'm organizing all the URLs
"urlpattenrs" should be "urlpatterns" (in my_project.core.api_urls.urls) ;)

How to get xml format in Django Rest Framework

I'm trying to get xml format in Django Rest FrameWork,I tried the tutorial provided by Django Rest Framework, I'm new to django, I did the following.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'books',
'users',
]
urls.py
from django.conf.urls import url
from django.contrib import admin
from books.views import *
from users.views import *
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^books/all/$', all_books),
url(r'^user/', get_user)
]
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html','xml'])
views.py
from rest_framework.response import Response
from rest_framework.decorators import api_view
from books.serializers import *
from books.models import *
# Create your views here.
#api_view(['GET'])
def all_books(request):
books = Book.objects.all()
serializers = BookSerializer(books,many=True)
return Response(serializers.data)
When I try to access the xml data, I Get this error by doing ?format=xml
{"detail":"Not found."}
the tutorial link http://www.django-rest-framework.org/api-guide/format-suffixes/
Actually your settings.py is missing the xml parser config.
install rest_framework_xml : pip install djangorestframework-xml
Update INSTALLED_APPS 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_xml',
'books',
'users',
]
Add xml parser in settings.py:
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': (
'rest_framework_xml.parsers.XMLParser',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_xml.renderers.XMLRenderer',
),
}

Categories

Resources