如何在python(apiview,Django)中创建动态url?

2024-10-02 02:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想动态地向urlpattern添加一个id,并将该id打印到我的服务器上。例如,在http://127.0.0.1:8000/experience/3上,我想将3打印到我的服务器上

from django.contrib import admin
from django.urls import path,include
from core.views import TestView
from core.views import ExperienceView
from core.views import ProjectView
from rest_framework.authtoken.views import obtain_auth_token

urlpatterns = [

    path('experience/<str:id>', ExperienceView.as_view(),name='experience'),

]
class ExperienceView(APIView):
    ...
    def delete(self,request,*args,**kwargs,id):
        connection = sqlite3.connect('/Users/lambda_school_loaner_182/Documents/job-search-be/jobsearchbe/db.sqlite3')
        cursor = connection.cursor()
        req = request.data
        print(id)
        return Response(data)


Tags: pathdjangofromcoreimport服务器iddata
1条回答
网友
1楼 · 发布于 2024-10-02 02:24:18

url参数被传递给argskwargs,在这种情况下,您可以从kwargs获取对象id

class ExperienceView(APIView):
    ...
    # don't change the signature of delete method
    def delete(self,request,*args,**kwargs):
        # print args and kwargs to see what parameters url passed.
        print(args)
        print(kwargs)
        id = kwargs.get('id')
        return Response(data)

相关问题 更多 >

    热门问题