django - 如何在通用视图中传递 slug (Django)
问题描述
我如何在这个通用的“ListView”中传递一个 slug,我被 get_context_data 函数弄糊涂了。蛞蝓在“轮廓”模型中。
视图.py
class PostListView(ListView):
model = Post
template_name = 'feed/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 10
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
if self.request.user.is_authenticated:
liked = [i for i in Post.objects.all() if Like.objects.filter(user = self.request.user, post=i)]
context['liked_post'] = liked
return context
解决方案
正如 Indox 建议的那样,我不需要向视图添加任何额外的代码,我在 url 模式上使用了 slug:slug ,并且在我的模板中有一个 {% url 'home' user.profile.slug %} ,最后我错过了 user.profile.slug,在添加它工作正常之后。
推荐阅读
- javascript - Sequelize 中的 WHERE 或 WHERE
- python - Python客户端-服务器如何处理超时
- mysql - 批量添加和编辑帖子类别
- python - 希望将字典转换为数据框和 csv
- elasticsearch - 默认情况下,“关键字”字段在文本字段上禁用获取 Fielddata
- php - Nginx 中的反向代理问题在同一网络上的 2 台服务器之间,一台服务器使用 apache,另一台是 node.js
- python - 你如何对python中的字符串数字列表进行排序?
- c++ - 在 VS Code 中使用 Linux 上的 MinGW 交叉编译适用于 Windows 的 SDL 程序?
- linux - 如何在共享库中嵌入数据?
- discord.js - Discord.js setVoiceChannel