Questions

How to know the current path in django website at any page?

0

I want to create a url and for that I want to know the current path of page.



asked 1 year, 10 months ago
Reputation: 1





1 Answer

0

To know the current path we need to add the request context preprocessor to settings.py. In most cases it should already be present but if it’s not there then make sure it’s added properly.

Settings.py

It should look something like the code below in settings.py.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },

    },
]

html template

Use {{request.path}} to show current url path.

That’s it.

answered 1 year, 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 10 months ago
Viewed:  591 times
Active:  1 year, 10 months ago