0

Suppose, we have a url path:

path('something/<int:some_param>/test/', views.some_view)

When a user hits this url, django makes an instance of HttpRequest, that will be passed to the some_view view. Is there a way to get the some_param url parameter from the request object outside the some_view code? (for instance, to do some processing that depends on some_param in a custom middleware).


One possible solution is to parse the HttpRequest.path attribute, but is there any prebuilt way to do this?

Dmytro
  • 190
  • 8
  • if you want to use in middleware check this thread [https://stackoverflow.com/questions/11775441/django-how-to-access-url-regex-parameters-inside-a-middleware-class](https://stackoverflow.com/questions/11775441/django-how-to-access-url-regex-parameters-inside-a-middleware-class), there is middleware hooks in django [check here](https://docs.djangoproject.com/en/3.2/topics/http/middleware/#other-middleware-hooks) – Shamsiddin Parpiev Nov 12 '21 at 20:22
  • @Shamsiddin, what I'm actually trying to do is setting some custom attributes to the request object to access them in the views later. Can I modify the request object in the `process_view` hook? Dunno, docs say it should return only `None` or `HttpResponse`. Is it sutable in my case? – Dmytro Nov 12 '21 at 22:02
  • Regarding whether it should return `None` or `HttpResponse`: lets say you are doing some checking (ex: if user is authenticated, or if request has some argument or anything) and if that checking is failed you do not want the view to be called and just return `HttpResponse` that checking is failed and again view will not be called. But in your case you just want to modify request object(you are not checking anything) so your process view func should return None, and your view will be called. Can I modify request object? I think yes – Shamsiddin Parpiev Nov 15 '21 at 12:01

2 Answers2

1

Django calls your view with the request object and some_param, which you have access to inside views.some_view. Your function will have the following signature:

def some_view(request, some_param):
    ....

Or:

def some_view(request, **kwargs):
    some_param=kwargs.get('some_param', None)

You can then use some_param inside your view to do whatever you need to do with it, save it in a database, put it in a cookie, do calculations with it, get some database data with it, etc. Then once you're done, you need to return a response object. Usually by calling render or TemplateResponse with a template or returning HttpResponse without a template. You render templates providing a context dictionary which you are free to put anything you like into (like some_param), which makes it available to be rendered in your HTML template. That rendered HTML template is then returned as response to your user through the magic of the render function or TemplateResponse class, which ends the view process. I.e. like so:

return TemplateResponse(request, 'template.html', context)

To store some_param in between views, you'll need to save it in the database, store it in the user's session, or use a cookie. Or pass it to the next view inside the url or outside the url via /?param=some_param. Without saying what you need some_param for later on, it's hard to solve your issue.

Limecat
  • 401
  • 1
  • 4
0

The one possible solution here is to use the resolve function from django.urls module. It is extremely uselful if you want to access the URL parameters from URL path that is related to a HttpRequest object outside a view function. For example, get the URL params and process them in the custom middleware or other parts of your code.

Example:

from django.urls import resolve

...

func, args, kwargs = resolve(some_request.path)

Dmytro
  • 190
  • 8