我有一个django网站,根据您的用户类型而溢出,我需要重定向无权查看网站某些方面的用户,
在我的模板中,我有
{% if user.get_profile.is_store %}
<!--DO SOME LOGIC-->
{%endif%}
Run Code Online (Sandbox Code Playgroud)
我如何将所述商店重定向回网站索引?
==== ====编辑
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))
Run Code Online (Sandbox Code Playgroud)
我实施了thornomad的答案,现在我得到了他的错误
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.
Run Code Online (Sandbox Code Playgroud)
tho*_*mad 20
我想,你想在不在模板中的视图中这样做.所以,像:
from django.http import HttpResponseRedirect
def myview(request):
if request.user.get_profile().is_store():
return HttpResponseRedirect("/path/")
# return regular view otherwise
Run Code Online (Sandbox Code Playgroud)
@decorator如果你发现自己需要做很多事情,你也可以使用a 作为视图.
Rob*_*rio 14
使用HTML的原始重定向.
{% if user.get_profile.is_store %}
<meta http-equiv="REFRESH" content="0;url=http://redirect-url">
{% endif %}
Run Code Online (Sandbox Code Playgroud)
或者提供重定向url作为上下文变量
{% if user.get_profile.is_store %}
<meta http-equiv="REFRESH" content="0;url={{ user.get_profile.store_url }}">
{% endif %}
Run Code Online (Sandbox Code Playgroud)
如果内存服务正确,这必须在"head"标签内,但现代浏览器更宽容,Firefox 4允许它在"body"标签内并且工作正常.
你真的不想在模板中重定向,正如所有其他答案中所说的那样.
但是如果在视图中重定向是没有选择的(为什么有),你可以这样做:
{% if user.get_profile.is_store %}
{% include '/path/to/template' %}
{% else %}
{% include '/path/to/another_template' %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23437 次 |
| 最近记录: |