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'.
Related
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"]
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.
I am getting errors in almost all imports please help.
i'm using django 2.2.5, python 3.7, but getting errors in unresolved import'django.contrib' ,unresolved import'django.urls',unresolved import'django.shortcuts' etc
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path("", include("hello.urls")),
path('admin/', admin.site.urls),
]
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
Message=No module named 'django'
File "C:\Users\Sharf\Desktop\python projects\p1\calc\urls.py", line 1,
in <module>
from django.urls import path
Can you post your settings.py file ? I think the problem comes from here ...
INSTALLED_APPS = [
'mainApp.apps.nameApp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels'
]
ROOT_URLCONF = 'app.urls'
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) ;)
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',
),
}