get URL parameter in function based view
I want to add URL parameter in function based view how can I do?
parameter is 4 I want to aces 4 in view. there is any way to do this?
def Homepage(request, pk):
In class-based views
How to access URL parameters in Django CBVs, (For each new view function, you have to remember to import it, and the import statement from django.conf.urls.defaults import * from mysite import views urlpatterns When there's a conflict, extra URLconf parameters get precedence over And speaking of branching based on request method, let's take a look at how we� view_function = MyView. as_view return view_function (request) The view function returned by the as_view() method is outer part of every class-based view. After called, the view pass the request to the dispatch() method, which will execute the appropriate method accordingly to the request type ( GET , POST , PUT , etc).
You're looking for the tutorial.
The solution for your example looks like this.
from django.urls import path from . import views urlpatterns = [ path('xxxx/<int:your_number>/', views.your_view_name), ]
def your_view_name(request, your_number): # do things with your_number here. # if your_number is optional, define a default return render(...)
Chapter 8: Advanced Views and URLconfs — Django Book 0.1 , This page is based on Django 2.2 LTS; see the previous Django 1.11 LTS Listing 2-11. Django view method extracting url parameters with request.GET. A view can be either function or a class-based view. CBVs inherit the method as_view() which uses a dispatch() method to call the appropriate method depending on the HTTP verb ( get , post , etc)
URL dispatcher | Django documentation, Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view). The view gets passed the following arguments: An instance of HttpRequest. If the matched URL pattern contained no named groups, then the matches from the regular expression are provided as positional arguments.
Django url parameters, extra options & query strings: Access in , The (?P<user_name>\w+) section means this is a url parameter (?P), and the parameter value should be passed to views.emp_detail function’s user_name argument. The \w+ is a regular expression which means this url parameter’s value should be more than one charactor or digits.
TemplateView is a class, not a function, so we point the URL to the as_view () class method instead, which provides a function-like entry to class-based views: # urls.py from django.urls import path from some_app.views import AboutView urlpatterns = [ path('about/', AboutView.as_view()), ]
- pass in fuction call like
def home(request, pk):