0

I want to dynamically add an id to an urlpattern and print that id to my server. for example on http://127.0.0.1:8000/experience/3 I want to print 3 to my server.

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)

1
  • error syntax. and id in args Commented Jun 19, 2020 at 4:09

1 Answer 1

2

url parameters are passed to args and kwargs, in this case, you can get your object id from kwargs.

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.