Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

Add Django Rest Framework route to third party urls

I am using Djoser along with Django REST Framework. This is my urls.py:

from django.urls import path, include
from rest_framework.routers import DefaultRouter

router = DefaultRouter()

djoser_urls = [
    path("auth/", include("djoser.urls")),
    path("auth/", include("djoser.urls.authtoken")),
]

urlpatterns = router.urls + djoser_urls

When using the browsable API at localhost:8000/auth/ the djoser urls are available. So that is all working correctly.

What I need is to be able to browse to localhost:8000/ and have the above route displayed as an option to click on. Currently there are no routes displayed in the browsable API at localhost:8000/ even though if I manually type localhost:8000/auth/ into the url then I am taken to the djoser urls.

In other words, when opening the browsable API at localhost:8000/, I would like the following to be available to click on:

{
    "auth": "http://localhost:8000/auth/"
}
question from:https://stackoverflow.com/questions/65913779/add-django-rest-framework-route-to-third-party-urls

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't know how to do it in djozer, but this is a simple method:

In views.py:

def indexJson(request):
   data  = '{ "auth": "http://localhost:8000/auth/" }'
   return HttpResponse(data, content_type='application/json')

urls.py

path( route='', view=views.indexJson, name='index')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...