ImproperlyConfigured error with include to other url file - python

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) ;)

Related

Change the api's documentation in swagger

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',
}

Show the main page in the django multi-tenant

I am studing the djanto multi-tenant package (https://github.com/django-tenants/django-tenants).
When i try to access the main page (landing page) at http://127.0.0.1:8000/, I got the following message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Raised by: core.views.home
No tenant for hostname "127.0.0.1"
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to
False, and Django will display a standard 404 page.
How do I get the landing page to show correctly?
URL FILE
from core.views import home
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
THE VIEW OF LANDINGPAGE
from django.shortcuts import render
def home(request):
return render(request, 'core/index.html')
PARTIAL SETTINGS FILES
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
SHARED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tenants',
'customer',
'core', //landing page
]
TENANT_APPS = [
# The following Django contrib apps must be in TENANT_APPS
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
# your tenant-specific apps
'myclientapp',
]
INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))
Thank you very much!
The issue there is with the Django Tenants middleware. By default, django_tenants will show a Page 404 error if you go to a tenant that does not exist. You can change this behavior by over-modifying the middleware and having your own local copy or (the easiest solution) you can just provide a default case by adding SHOW_PUBLIC_IF_NO_TENANT_FOUND = True in your project's settings.py file. This will route to the public schema hence other url routes will work as they should.

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.

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',
),
}

django template page output No module named blog

Im starting with django but this get me one error and i cant run /blog/templates/index.html
ps:
i tried with
url(r'^$', 'FirstBlog.blog.views.home', name='home')
or
url(r'^$', include('views.home'))
but this dont work
i also tried
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
but....
can u help me?
full project: http://www.megafileupload.com/Uih/FirstBlog.tar.gz
django version: 1.7.7
First make sure you set TEMPLATES_DIRS in settings.py, it needs to be an absolute path.
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )
Next if your templates folder is within your app, all you need is the string 'index.html',
# Function Based View
from django.shortcuts import render
def home(request):
return render(request, 'index.html') # Render syntax requires (request, template)
# Class based view
from django.views import generic
class Home(generic.TemplateView):
template_name = 'index.html'
For class based views urls:
from blog.views import Home
urlpatterns = [
url(r'^$', Home.as_view(), name='home'),
]
For functions based views urls:
url(r'^$', 'blog.views.home', name='home')

Categories

Resources