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.
- [Answer]-WritableField field_from_native returned value not used in Serializer
- [Answer]-Django FileUploadHundler force file storage to Disk
- [Answer]-Django related _set with order_by showing duplicates
- [Answer]-Django – should I use only 1 view for all website?
Source:stackexchange.com