How to get URL of current page, including parameters, in a template?
Asked Answered
F

7

67

Is there a way to get the current page URL and all its parameters in a Django template?

For example, a templatetag that would print a full URL like /foo/bar?param=1&baz=2

Flagpole answered 14/7, 2010 at 17:14 Comment(2)
possible duplicate of Reading path in templatesMen
another possible duplicate is Get the current URL withing a Django templateMen
E
77

Write a custom context processor. e.g.

def get_current_path(request):
    return {
       'current_path': request.get_full_path()
     }

add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS settings variable, and use it in your template like so:

{{ current_path }}

If you want to have the full request object in every request, you can use the built-in django.core.context_processors.request context processor, and then use {{ request.get_full_path }} in your template.

See:

Emblazonry answered 14/7, 2010 at 17:44 Comment(1)
Don't forget to use urlencode i.e. {{request.get_full_path|urlencode}} if you need this for redirectionLaborsaving
R
27

Use Django's build in context processor to get the request in template context. In settings add request processor to TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS = (

    # Put your context processors here

    'django.core.context_processors.request',
)

And in template use:

{{ request.get_full_path }}

This way you do not need to write any new code by yourself.

Rogatory answered 25/1, 2014 at 11:38 Comment(2)
It should be noted that using this method overwrites the default TEMPLATE_CONTEXT_PROCESSORS so you will have to add those back to the list. The docs have a list of the defaults to include in the list.Klarrisa
It should be noted that in newer versions of Django this settings key has changed to TEMPLATES['OPTIONS']['context_processors'], and that the aformentioned context processor is included by default.Crafty
A
9

In a file context_processors.py (or the like):

def myurl( request ):
  return { 'myurlx': request.get_full_path() }

In settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
  ...
  wherever_it_is.context_processors.myurl,
  ...

In your template.html:

myurl={{myurlx}}
Andyane answered 14/7, 2010 at 17:44 Comment(1)
Sorry, downvoted by mistake.. please make a change so that I can undo that.Airwoman
G
6

If we are acessing the following URL: http://127.0.0.1:8000/home/?q=test

then

request.path = '/home/'
request.get_full_path() = '/home/?q=test'
request.build_absolute_uri() = 'http://127.0.0.1:8000/home/?q=test'
Gussie answered 19/2, 2021 at 8:49 Comment(0)
C
2

Django has a lot of built-in stuff, but if you don't explicit what do you want to use, it won't be used.

So, in MTV schema (Model, Template, View) the view receives a request and uses a template render to generate a response, passing on it a dictionary or all local variables (using the locals() function) of this view. Knowing this, we can insert the current url that came from the response, like this:

views.py:

from django.shortcuts import render

def page(request):
    currentUrl = request.get_full_path()
    return render(request, 'app/page.html', locals())

Then, in the template 'app/page.html' you just have to do the following to display the currentUrl variable that we just created and passed through via locals():

app/template/page.html:

{{ currentUrl }}
Cirque answered 30/11, 2011 at 3:59 Comment(0)
P
2

In addtition to sdolan's answer:

if you are using I18N and would like to pass next value to /i18n/setlang/ to change the language of the current page, then you will need to strip off current language code from the full path either. Something like:

full_path = request.get_full_path()
current_path = full_path[full_path.index('/', 1):]

This assumes that every path has format /LANG_CODE/any/other/stuff/with/?param='yay' and simply kicks off LANG_CODE whatever it is (e.g., /en/ will result into /).

Precious answered 19/1, 2014 at 16:26 Comment(0)
R
2

You can see if your url differs from the others.

{% if 'foo/bar/' in request.get_full_path %}
Rational answered 19/5, 2017 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.