Django: get URL of current page, including parameters, in a template

前端 未结 6 1248
忘了有多久
忘了有多久 2020-12-01 00:05

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?p

6条回答
  •  孤城傲影
    2020-12-01 00:35

    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 /).

提交回复
热议问题