Search code examples
pythondjangomiddleware

How I access the string from url in middleware


How I access the url parameter in my middleware

Here is my urls.py

from django.conf.urls import include, url
from django.contrib import admin
from views import *

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^my_url/(?P<string>[\w\-]+)/$', my_view),
]

Now I want to access the string vale in my middleware. Here is my middle ware code

class MyMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Here I want a string variable value

Is there any solution to access the value of string variable


Solution

  • The URL, along with all request data, is held in attributes of the request object.

    Try request.path_info or request.path