我创建了一个HackerNews克隆。然后,用户可以获得投票,提交故事等权限。每个用户在登录时都有会话ID,我已经测试过,我认为工作得很好。
但是,当我在视图之间切换时(例如,从localhost /到localhost / science),所有这些权限似乎都消失了。
以下是views.py:
的一部分def index(request, category_id=1):
stories = top_stories(top=30)
category = Category.objects.get(id = category_id)
if request.user.is_authenticated():
liked_stories = request.user.liked_stories.filter(id__in = [story.id for story in stories])
else:
liked_stories = []
return render(request, 'stories/index.html', {
'stories': stories,
'user': request.user,
'liked_stories': liked_stories,
'category': category,
})
这是我的类别功能,它基本上对特定类别的所有故事进行排序:
def category(request, category_name):
template = 'stories/category.html'
category = Category.objects.get(category_name = category_name)
return render_to_response(template, {
'category': category
})
当我转到localhost / {{category}}链接时,投票和提交故事等所有权限似乎都消失了。
这是我的base.html:
<body>
<div id = "content">
<div id = "header">
{% if user.is_authenticated %}
<div id = "user-info">{{user.username }} | <a href = "/story/">sumbit</a> | <a href = "/logout/">logout</a></div>
{% else %}
<div id = "user-info"><a href = "/login/">Login</a></div>
<div id = "user-info"><a href = "/register/">Sign Up</a></div>
{% endif %}
<div id = "site-title"><h1><a href = "/">newsfeed</a></h1></div>
</div>
{% block content %}
{% endblock content %}
</div>
</body>
index.html:
<ol>
{% for story in stories %}
<li>
<p class = "story-title">
{% if user.is_authenticated and story not in liked_stories %}
<a href = "/vote/" id = "story-vote-{{ story.id }}" class = "vote"><img src = "static/images/arrow.png" height = "20px" width = "20px"></a>
<a href = "{{ story.url }}" id = "story-title-{{ story.id }}" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>
{% else %}
<a href = "{{ story.url }}" style = "margin-left: 15px;" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>
{% endif %}
</p>
<p class = "story-info">
{{story.points}} points by {{ story.moderator.username }} {{story.created_at|age}} | <a href = "{{ story.category.category_name }}/{{ story.id }}/"> comment </a>
| <a href = "/{{ story.category.category_name }}/">{{ story.category }} </a>
</p>
</li>
{% endfor %}
</ol>
和category.html:
<ol>
{% for story in category.story_set.all %}
<li>
<p class = "story-title">
{% if user.is_authenticated and story not in liked_stories %}
<a href = "/vote/" id = "story-vote-{{ story.id }}" class = "vote"><img src = "static/images/arrow.png" height = "20px" width = "20px"></a>
<a href = "{{ story.url }}" id = "story-title-{{ story.id }}" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>
{% else %}
<a href = "{{ story.url }}" style = "margin-left: 15px;" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>
{% endif %}
</p>
<p class = "story-info">
{{story.points}} points by {{ story.moderator.username }} {{story.created_at|age}} | <a href = "{{ story.id }}/"> comment </a>
| <a href = "/{{ story.category.category_name }}/">{{ story.category }} </a>
</p>
</li>
{% endfor %}
</ol>
我有什么遗漏可以纠正这个问题吗? 此外,如果您需要任何其他文件,请在评论中告诉我。 卢卡
答案 0 :(得分:3)
索引视图有效,因为您在模板上下文中包含user
。如果您启用了auth context processor(默认情况下是这样),那么您实际上不需要将用户包含在模板上下文中,因为您使用的是render
快捷方式。
return render(request, 'stories/index.html', {
'stories': stories,
'liked_stories': liked_stories,
'category': category,
})
category
视图无效,因为您使用的是render_to_response
,并且未在模板上下文中明确包含user
。
您可以通过添加render_to_response
(the docs)来使其与context_instance
一起使用,但最简单的解决方法是使用类别视图的render
快捷方式好。
return render(request, template, {
'category': category
})