[Answer]-Django template: access project root path

1👍

You can get the full path of the URl using request.full_path and write a template tag to check if it contains a string but this is a very bad way of trying to do what you want (redirect when unauthorized).

Instead, you should decorate your view using @login_required:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

0👍

Although I agree with Timmy that you shouldn’t be trying to do this sort of redirection in the template, note that there is an easy way of getting the full URL to a view: and that is using the {% url %} tag. That tag (and the reverse function in views) is aware of the full path to your site, which it gets from the WSGI environment, so there is no need to pass an extra variable. Since you shouldn’t be hard-coding URL paths anyway, using this is a win all round.

Leave a comment