Django Rest Swagger Not showing up all the APIs - django

I am working with Django Rest swagger. But it's not showing up all the APIs.
The url paths for /tickets are missing:
Even path to /dj-rest-auth/user/ is missing as well:
Backend/urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
#path('api/v1/', include(api_urlpatterns)),
#path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('api/', include('ticket.urls')),
path('swagger/', schema_view)
]
Tickets/urls.py
from django.urls import path
from ticket import views
urlpatterns = [
path('tickets/', views.ticket_list),
path('tickets/<int:pk>/', views.ticket_detail),
]
My directory structure:

Try with below code:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
.......
.......
}

I solved this by going in settings.py file and set DEBUG=True.

Related

You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs

the explorer in vscode Getting the error: You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs.
meetups/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('meetups/', views.index) #domain_name.com/meetups/
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('meetups.urls'))
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'templates/meetups/index.html')
You probably didn't add your app (meetups) into settings.INSTALLED_APPS, which can be found in your_project_name/settings.py.
Change meetups/templates/index.html to meetups/index.html also add name in your urlpatterns

How to remove last / character in root URL API in Django

API only work with url below:
http://127.0.0.1:1997/api/v1/groups/
How can I remove the last '/' so that it works like below:
http://127.0.0.1:1997/api/v1/groups
My config url code:
import os
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from rest_framework.schemas import get_schema_view
from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
from groups import views as group_views
API_VERSION = os.getenv('API_VERSION')
API_ROOT = f"api/{API_VERSION}/"
router = routers.DefaultRouter()
router.register('groups', group_views.GroupViewSet)
schema_view = get_schema_view(
title='next_blog',
renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])
urlpatterns = [
path('admin', admin.site.urls),
path('api_auth', include(
'rest_framework.urls', namespace='rest_framework')),
path('docs', schema_view, name='docs'),
path(API_ROOT, include(router.urls)),
]
Thanks everyone !
Set the trailing_slash argument to False when instantiating the router.
router = DefaultRouter(trailing_slash=False)
Django Rest Framework Default Router

Seeing only one endpoint in browsable API instead of two

How come I can only see one of my URLs (users) instead of both in the django rest browsable API?
urls:
urlpatterns = [
path("admin/", admin.site.urls),
path("api-auth/", include("rest_framework.urls")),
path("", include("accounts.urls")),
path("", include("properties.urls")),
]
I expected to see users AND properties
accounts.urls:
from django.urls import path, include
from accounts.views import UserViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r"users", UserViewSet)
urlpatterns = [path("", include(router.urls))]
properties.urls:
from django.urls import path, include
from properties.views import PropertyViewSet, UnitViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r"properties", PropertyViewSet)
router.register(r"units", UnitViewSet)
urlpatterns = [path("", include(router.urls))]

Preview custom 500 error page in Wagtail?

I've made custom 500 and 404 error pages in Wagtail. I can preview the 404 page by typing in a false url. I'm just wondering how I can preview the 500 page?
The custom page has links to static images that I need to check are working.
My urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from wagtail.contrib.sitemaps.views import sitemap
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
admin.autodiscover()
urlpatterns = [
url(r'django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^sitemap\.xml$', sitemap),
url(r'', include('puput.urls')),
url(r'', include(wagtail_urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if 'debug_toolbar' in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
The answer at https://stackoverflow.com/a/24660486/823020 has most of these details. You can make a view that raises a 500 error.
You can add a views.py to any app. In that file (taken directly from the linked answer):
from django.http import HttpResponseServerError
def my_test_500_view(request):
# Return an "Internal Server Error" 500 response code.
return HttpResponseServerError()
Supplement this in your urls.py with:
from django.conf import settings
from django.urls import path
# or for Django 1.x do
# from django.urls import url
from myapp import views
urlpatterns = [
# original content here
]
if settings.DEBUG:
urlpatterns += [
path('test_500/', views.my_test_500_view, name="test_500"),
# or for Django 1.x do
# url(r'^test_500/$', views.my_test_500_view, name="test_500"),
]
If it's not directly related to any Wagtail pages, then a utils Django app can work well for generic shared code.
Just add url with TemplateView to your urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^500/", TemplateView.as_view(template_name='500.html')),
]

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs