gpt4 book ai didi

python - 为什么在 View 之间切换时我的用户权限会消失?

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:07 26 4
gpt4 key购买 nike

我创建了一个 HackerNews 克隆。然后用户被授予投票、提交故事等权限。每个用户在登录时都有一个 session ID,我已经测试过,我相信它工作得很好。

但是,当我在 View 之间切换时(例如从 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>

有什么我遗漏的可以解决这个问题的吗?另外,如果您需要任何其他文件,请在评论中告诉我。卢卡

最佳答案

索引 View 之所以有效,是因为您在模板上下文中包含了 user。如果你有 auth context processor启用(默认情况下),那么您实际上不需要将用户包含在模板上下文中,因为您使用的是 render捷径。

return render(request, 'stories/index.html', {
'stories': stories,
'liked_stories': liked_stories,
'category': category,
})

category View 不起作用,因为您正在使用 render_to_response,并且您没有在模板上下文中明确包含 user

您可以通过包含 context_instance 使其与 render_to_response 一起工作(参见 the docs ),但最简单的解决方法是使用 render 也是您的类别 View 的快捷方式。

return render(request, template, {
'category': category
})

关于python - 为什么在 View 之间切换时我的用户权限会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com