Redirecting user to django template

I have a Django website, which is distributed depending on what type of user you have, I need to redirect users who are not allowed to see certain aspects of the site,

in my template i have

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

how would I redirect the specified store back to the site index?

==== ==== EDIT

 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] downloads_list[0].get_thumbnail() data = {} data['download_list'] = downloads_list return render_to_response('downloads.html', data, RequestContext(request)) 

I am implementing a response from Tornomada and now I get his error

 Environment: Request Method: GET Request URL: http://localhost:8000/professional/downloads Django Version: 1.1.1 Python Version: 2.6.2 Installed Applications: ['django.contrib.auth', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'sico.news', 'sico.store_locator', 'sico.css_switch', 'sico.professional', 'sico.contact', 'sico.shop', 'tinymce', 'captcha'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware') Traceback: File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response 92. response = callback(request, *callback_args, **callback_kwargs) File "/var/www/sico/src/sico/../sico/professional/views.py" in downloads 78. if request.user.get_profile().is_store(): File "/var/www/sico/src/sico/../sico/shop/models.py" in is_store 988. return not self.account is None File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py" in __get__ 191. rel_obj = self.related.model._base_manager.get(**params) File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py" in get 120. return self.get_query_set().get(*args, **kwargs) File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in get 305. % self.model._meta.object_name) Exception Type: DoesNotExist at /professional/downloads Exception Value: Account matching query does not exist. 
+5
source share
6 answers

You will want to do this, I think, in a view not in a 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 can also use @decorator for submission if you need to do this a lot.

+19
source

Use the original HTML redirect.

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

or specify the redirect 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 the memory is correct, it should be inside the "head" tag, but the modern browser is simpler, Firefox 4 resolved it in the body tag and worked fine.

+12
source

You really don't want to redirect the template, as said in all the other answers.

But if redirection in the view is not an option (for some reason), you can do this:

 {% if user.get_profile.is_store %} {% include '/path/to/template' %} {% else %} {% include '/path/to/another_template' %} {% endif %} 
+5
source

I think you can do a redirect in the view code.

For example, this will work in Django 1.1.

 from django.shortcuts import redirect def my_view(request): if request.user.get_profile().is_store: return redirect('index') # normal view code here return .... 

The documentation for the redirect shortcut is here: http://docs.djangoproject.com/en/dev/topics/http/shortcuts/ The arguments for redirection () can be (citing documents):

  • Model: get_absolute_url () function will be called.
  • View name, possibly with arguments: urlresolvers.reverse () will be used to reverse the name.
  • The URL to be used as-for for the redirect location.
+2
source

Of course, sometimes we have views imported from the official django code, or others that are independent of us. We cannot place redirects in these views, so the only way is to use a template that these (untouchable) views use.

+2
source

You would not do this in the template, but in the view. Instead of calling render_to_response (which I assume you are doing now), you call HttpResponseRedirect .

+1
source

All Articles