4

I want to add URL parameter in function based view how can I do?

http://127.0.0.1:8000/xxxxx/4

parameter is 4 I want to aces 4 in view. there is any way to do this?

1
  • pass in fuction call like def home(request, pk): Commented May 3, 2019 at 7:05

3 Answers 3

7

in Function-Based Views:

def Homepage(request, pk):

In class-based views

self.kwargs['pk']

Sign up to request clarification or add additional context in comments.

Comments

6
self.kwargs['blog_ID']

or

def blogpost(request,blog_ID):

Comments

2

You're looking for the tutorial.

The solution for your example looks like this.

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('xxxx/<int:your_number>/', views.your_view_name),
]

views.py

def your_view_name(request, your_number):
    # do things with your_number here.
    # if your_number is optional, define a default
    return render(...)

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.