社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

django模板中取不到user的问题

hu-xq11 • 10 年前 • 4690 次点击  
    {% if user.is_authenticated %}
    <li><a>{{ user.username }}</a></li>
    <li><a ng-click="logout()">logout</a></li>
    {% else %}
    <li class="loginHead"><a href='/#/login'>Login</a></li>
    <li class="registHead"><a href='/#/reg'>Regist</a></li>

如上,是我想要用来验证用户是否登录的模板代码,但是在模板中user.is_authenticated和user.username都取不到,用{{user.is_authenticated}}直接显示不出任何东西,我在settings.py中已经设置了

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.csrf"
)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/813
 
4690 次点击  
文章 [ 4 ]  |  最新文章 10 年前
走遍北京的德福小猪-weibo
Reply   •   1 楼
走遍北京的德福小猪-weibo    10 年前

也可以使用 render(request , 'index.html' , locals())来实现。

hu-xq11
Reply   •   2 楼
hu-xq11    10 年前

@Ziv_Luther-weibo 谢谢您,尝试了,问题解决了,学习了啊。

Ziv_Luther-weibo
Reply   •   3 楼
Ziv_Luther-weibo    10 年前

render_to_response('index.html', context_instance=RequestContext(request)) 这样写试试。 加上context_instance=RequestContext(request)

hu-xq11
Reply   •   4 楼
hu-xq11    10 年前

请问一下各位大神,还需要注意哪里,才能在模板中取到user?谢谢了