我有以下模板:
base.html & articles.html & article.html
base.html 包含标题:
<h6>User logged-in : {{full_name}}</h6>
articles.html 和 article.html 扩展了 base.html
在我的 django 应用程序“文章”中,我通过了
args['full_name'] = request.user.username
然后:
return render_to_response('article.html', args, context_instance=RequestContext(request))
我对功能文章也这样做。它允许我将变量传递给 articles.html 和 article.html 中的扩展 base.html header
这意味着我需要为 def edit_article 和函数 create_article 做同样的事情,我以复制自己结束,所以我打破了基本的 django 规则。
有没有其他更好的方法来显示谁登录并使用它为每个登录用户创建单独的 View ,如下所示:
每个用户都可以查看自己的观点、编辑观点、添加新文章并允许其他用户/用户组访问他的观点?
?
最佳答案
您可以使用(无需再次将其传递给上下文)访问用户
{{ request.user.username }}
关于django:如何在每个模板中显示用户登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467068/