Can Django admin url be on a swagger file using drf-yasg? - django

I am deploying a Django API on Google Endpoint.
Google endpoint needs a documentatin generated with openapi2.0 to set all endpoint. If an url is not defined on the yaml file generated by openapi (here by drf-yasg) the endpoint can not be reached even if it is available.
Thing is, it would be very very very useful for me to access my django admin zone on my api but when I generate my yaml file it skips my admin url.
here is my url, basic:
urlpatterns = [
# back
path('', include('api.urls')),
path('', include(spectacular_url)),
path('admin/', admin.site.urls),
]
Do you know how to do this ?
Thanks

Related

How to use Postman to authenticate Google Login with dj_rest_auth

So I am following the official documentation for Google sign in with DjangoRestFramework using DJ Rest Auth (this link)
I intend to authenticate with Postman Oauth2 (by following the guide and generating an Access Token)
Postman is generating an access token successfully, but I cannot seem to use this authentication in my API calls. Please who knows which step I am missing - I want to handle everything in Postman.
urls.py
urlpatterns = [
path('', Home.as_view(), name='home'),
path('admin/', admin.site.urls),
path('accounts/', include(api_urls, namespace='api')),
path('accounts/login/', GoogleLogin.as_view(), name='google_login'),
path('accounts/', include('rest_framework.urls')),
]
views.py
class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
callback_url = 'http://localhost:8080/accounts/google/login/callback/'
client_class = OAuth2Client
On calling an API endpoint, I get an invalid token error:
If I however visit the Google Login view in my RestFramework UI (in my case http://localhost:8080/accounts/login), I get an endpoint to make a POST, and on making a POST request, a key is generated. Only this key (if used as a Bearer token) works in my API calls.
How can I authenticate on Google, and make my API calls independent of the DRF UI?
Callback URL has been configured on my Google Developer Client.
PS: I feel the answer is in step 6 of the documentation, but I am unable to figure out how to do this in Postman
POST code or token to specified URL(/dj-rest-auth/google/)
What I did here is from postman go to headers then put Authorization = Token youraccesskey
which in your case Authorization = Token ef057......
Hope it helps

Can I deploy a Django App if there is no "" (empty) url?

I'm trying to deploy a Django App with railways for the first time and I was wondering where exactly will be my landing page. So basically my urls.py looks something like this:
path('home/', views.home, name="home"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
After deploying my Blog website, let's say with the domain 12345xxx.com, where will I land?
You will get an error if you don't include a folder in the URL, as the URL patten won't be matched and the existance of non-admin URLs stops the default django page from showing. This will be either a 404 error or a 'Django tried these URL patterns, in this order:' type error if you have DEBUG=True on in settings.
Note that you don't have to provide a path (the path can be an empty string), and views can have multiple paths. In this case, perhaps
path('', views.home, name="home"),
path('home/', views.home, name="home_folder"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
would avoid an error.

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

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.