Django rest swagger api list contain its own url - django

My urls.py shows below:
urlpatterns = [
url(r'^v1/', include('apps.api.urls')),
url(r'^docs/$', schema_view)
]
I don't know why it shows docs, can i remove it and how

Related

Extra space showing in end url

I am having an issue with Panda. I am trying to add a new views to my website and when I type this line in the product/urls.py file I get an error stating that the endpoint was not found by Panda - Page not found (http://127.0.0.1:8000/products/new/)
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
products/ new
The current path, products/new/, didn't match any of these.
I have created a new function to hold the new products view
def new(request):
return HttpResponse('New Product')
Then I have mapped the endpoint in the products/urls.py file
urlpatterns = [
path('', views.index),
**path('new', views.new)**
]
P.S. The index is behaving as expected
The space is just formatting to show that these is a set of suburls. The problem is the trailing slash:
urlpatterns = [
path('', views.index),
path('new/', views.new)
]

Django REST Framework API schema generation with same URL prefix

I'm documenting my Django application using get_schema_view() method, but when I open the Swagger UI all the URLs are placed under the same group.
My urls.py look like:
router = routers.DefaultRouter()
router.register(r'stores', StoreViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(router.urls)),
path('api/register/', UserRegistrationView.as_view(), name='register'),
path('api/login/', views.obtain_auth_token, name='login'),
# Documentation
path('openapi/', get_schema_view(
title='Title API',
description='Some description goes here...',
version='v0.1'
), name='openapi-schema'),
path('docs/', TemplateView.as_view(
template_name='documentation.html',
extra_context={'schema_url': 'openapi-schema'}
), name='docs')
]
All the endpoints are placed under a group called api in the Swagger UI.
How can I specify the api/ prefix while generating the schema in order to group the endpoints by model?
Thanks.
I had the same problem. The simple solution is to define prefix in settings.
SPECTACULAR_SETTINGS = {
'SCHEMA_PATH_PREFIX': r'/api/v[0-9]',
}
Answered here : Custom Grouping on OpenAPI endpoints with Django Rest Framework

Django Rest Framework: incorrect hyperlink on second viewset of same model

I'm trying to provide two distinct APIs using DRF but I'm unable to get the second app to stop creating
hyperlinked references based on the first. It's essentially the same problem as Django Rest Framework with multiple Viewsets and Routers for the same object but I'm unable to get it working.
app1/urls.py:
router = SimpleRouter(trailing_slash=False)
router.register(prefix=r'article', viewset=app1.ArticleViewSet, basename=r'article')
urlpatterns = [path(f'', include(router.urls)]
app2/urls.py:
router = SimpleRouter(trailing_slash=False)
router.register(prefix=r'published', viewset=app2.ArticleViewSet, basename=r'published')
urlpatterns = [path(f'', include(router.urls)]
site/urls.py:
urlpatterns = [
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
While both viewsets are of the same model, the queryset & serializer for each is different.
When I GET an item from /app2/published, it has an app1 URL:
"url": "http://localhost:8000/app1/article/5461"
What I'm wanting is for items retrieved via app2 to have:
"url": "http://localhost:8000/app2/published/5461"
From looking at the docs, it appears that providing basename should do what I want, but I'm not having any luck with getting it to work.
Try the following code in your site/urls.py:
from app1.urls import router as app1_router
from app2.urls import router as app2_router
router = routers.DefaultRouter()
router.registry.extend(app1_router.registry)
router.registry.extend(app2_router.registry)
urlpatterns = [
path('', include(router.urls)), # default page to show api
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
You can see an example here, which has same structure as you need.

How do I integrate Django and react-router-dom?

How do I setup the Django urls.py and the React Components to establish this?
My requirement is to have Django render the homepage with urls file as such:
urlpatterns = [
path('', views.index),
]
React router should then render the subsequent links (eg. /home, /about)
To set up a catch-all route with the django 2.0 path syntax, use the <path:> converter. This will route any url to the react app served from views.index.
urlpatterns = [
path('<path:route>', views.index),
]
For regex routes, an empty string is a catch-all.
urlpatterns = [
re_path('', views.index),
]
If you have routes that should not be routed to the react app, you must include those routes first.
urlpatterns = [
path('admin/', admin.site.urls),
path('<path:route>', views.index),
]
Your React application is going to be served from a single page. I'm assuming your view.index is your blank html, which usually looks something like this:
<div id="root"></div>
<script src="/dist/bundle.js"></script>.
(make sure your application bundle is being served in the script tag). Django will serve the html file with the route associated. In your case, if you want the root of the application to be React, then your urls.py would look like:
urlpatterns = [
path('/', views.index),
]
Once Django serves up the html file, and the script tag finishes loading your app onto that page - at this point it will enter the root of your React application, and from there React-Router will take over. Django will not be aware of any further routes because once React takes over the routing is 'virtual'. I hope this answers your question - please let me know if you need any further information.

Django Rest Swagger -> No Authentication + multiple apps

We are looking to use django swagger for generating REST API docs. Our app consists of many sub apps and all views use JWT authentication. Document seems very lacking and just mentions the adding of url. This shows me error 400 : ["The schema generator did not return a schema Document"] http://10.0.0.61:8001/API/?format=openapi
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title="HashCove REST API")
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'info/', BackendVersion.as_view()),
url(r'^transaction/', include('kyc_rest_services.kyc_connect_transaction_manager.urls')),
url(r'^drive/', include('kyc_rest_services.carbon_drive.urls')),
url(r'^channels/', include('kyc_rest_services.channels.urls')),
url(r'^accounts/', include('kyc_rest_services.kyc_connect_accounts.urls')),
url(r'^API/', schema_view, name="docs")
]
Can it show all the APIs in the other apps and no authentication required.