Redirecting a user in a django template


I have a django website that is spilt depending on what user type you are, I need to redirect users that are not entitled to see certain aspects of the site,

in my template, I have

{% if user.get_profile.is_store %}
    <!--DO SOME LOGIC-->

how would I go about redirecting said store back to the index of the site?


def downloads(request):
    Downloads page, a user facing page for the trade members to downloads POS etc
    if not authenticated_user(request):
        return HttpResponseRedirect("/professional/")

    if request.user.get_profile().is_store():
        return HttpResponseRedirect("/")

    user = request.user
    account = user.get_profile()

    downloads_list = TradeDownloads.objects.filter(online=1)[:6]
    data = {}
    data['download_list'] = downloads_list

    return render_to_response('downloads.html', data, RequestContext(request))

I implement the answer from thornomad, and now I get his error


Request Method: GET
Request URL: http://localhost:8000/professional/downloads
Django Version: 1.1.1
Python Version: 2.6.2
Installed Applications:
Installed Middleware:

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/sico/src/sico/../sico/professional/" in downloads
  78.   if request.user.get_profile().is_store():
File "/var/www/sico/src/sico/../sico/shop/" in is_store
  988.         return not self.account is None
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/" in __get__
  191.             rel_obj = self.related.model._base_manager.get(**params)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/" in get
  120.         return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/" in get
  305.                     % self.model._meta.object_name)

Exception Type: DoesNotExist at /professional/downloads
Exception Value: Account matching query does not exist.
4/17/2019 4:32:36 PM

Accepted Answer

You will want to do this, I think, in a view not in the template. So, something like:

from django.http import HttpResponseRedirect

def myview(request):
    if request.user.get_profile().is_store():
        return HttpResponseRedirect("/path/")

    # return regular view otherwise

You could also use a @decorator for the view if you found yourself needing to do this a lot.

11/30/2009 3:14:58 PM

Use the HTML's raw redirection.

{% if user.get_profile.is_store %}
    <meta http-equiv="REFRESH" content="0;url=http://redirect-url">
{% endif %}

or provide the redirection url as a context variable

{% if user.get_profile.is_store %}
    <meta http-equiv="REFRESH" content="0;url={{ user.get_profile.store_url }}">
{% endif %}

if memory serves right, this has to be inside the "head" tag, but modern browser are more forgiving, Firefox 4 allowed it inside the "body" tag and worked ok.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow