1

I have a URL in the following format.

http://127.0.0.1:8000/accounts/login/?next=/event/contract-risk-management/review/

And I need to "parse" the "/event/contract-risk-management/review/" part from it in templates, but I don't know how to get the part after question mark.

I tried request.path but it returned only the first part of the URL (without a domain).

What should I use?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zoli
  • 831
  • 1
  • 11
  • 27

1 Answers1

5

In short, do request.GET.get('next') in your views.py to get the parameters.

According to Django documentation, in HttpRequest objects, GET and POST attributes are QueryDict, which just acts as a dictionary. You can also do normal dictionary lookups like request.GET['next'] or request.GET.get('next', None) on it.

Django documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • Sure it is in the GET, thanks, any reason no to use "request.GET.next" directly in the template? – Zoli Dec 22 '15 at 19:35
  • 1
    I'm not sure what's your use case, but you shouldn't have too much of the logic in your template, like manipulating urls, doing redirections, etc, those actions should stay in views.py. It also make your web app flow easier to track. When people read your code, they only look at views.py and knows everything, instead of checking view plus templates. – Shang Wang Dec 22 '15 at 19:40