【发布时间】:2021-02-24 10:06:32
【问题描述】:
我正在构建一个 BlogApp,但遇到了一个问题。
我要做什么
我正在尝试像Featuring Post system 一样构建。如果用户单击此帖子的功能,则应将帖子添加到特色帖子中。
问题
当我点击精选此帖子时,它不会在精选帖子页面中添加帖子。
我尝试了什么
我还为特色帖子制作了另一个模型,但它不起作用,所以我再次在 Post Model 中实施
views.py
def featured(request,user_id):
post = get_object_or_404(Post,post_owner=user_id)
if request.user.is_authenticated:
if request.method == 'POST':
post.featured.add(post)
context = {'post':post}
return render(request, 'featured_posts.html', context)
models.py
class Post(models.Model):
post_owner = models.ForeignKey(User,default='',null=True,on_delete=models.CASCADE)
post_title = models.CharField(max_length=500,default='')
featured = models.ManyToManyField(User,related_name='featured',blank=True,default='')
urls.py
path('featured/<int:user_id>/',views.featured,name='featured'),
detail_view.html
{{ data.post_title }}
<a href="{% url 'mains:featured' data.id %}">Feature this Post</a>
featured_posts.html
{% for s in post.featured %}
{{ s.post_title }}
{% endfor %}
我不知道我做错了什么。
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
-
请分享 404 的完整堆栈跟踪。
-
没有显示
404 now。
标签: python html django django-models django-views