无法获得社交网络项目中帖子的详细信息视图 [英] Can't get the detail view of a post in a social network project
问题描述
我正在django中建立一个简单的社交网络。
I am building a simple social network in django.
在我的社交网站的家中,我列出了所有用户发布的所有帖子,作者和出版日期。发布日期有指向URL的链接,该URL应该返回用户发布的特定帖子的详细视图。
In the "home" of my social, I have the list of all posts published by all users, with author and publishing date. The publishing date have a link to a url that should return the detail view of a specific post published by a user.
但是,当我单击它时,它向我显示它的作者发布的所有帖子的列表(当我单击该帖子的作者链接时,也可以使用此列表。)
However, as I click on it, it shows me the list of all posts published by its author (this also works when I click on the author link of the post).
因此,两者
www.mysocial.com/posts/by/ciccio/7 /
www.mysocial.com/posts/by/ciccio/7/
和
www.mysocial.com/posts/by/ciccio /
www.mysocial.com/posts/by/ciccio/
带我到同一页,这是ciccio的帖子列表。
take me to the same page, that is ciccio's posts list.
我将显示urls.py,views.py和都包含在我的帖子应用中的models.py(myproject>帖子)
I am going to show urls.py, views.py and models.py that all are contained in my "posts" app (myproject > posts)
这是我的urls.py
Here is my urls.py
# shows all user posts
url(r'by/(?P<username>[-\w]+)',
views.UserPosts.as_view(),
name='for_user'),
# shows specific post
url(r'by/(?P<username>[-\w]+)/(?P<pk>\d+)/$',
views.PostDetail.as_view(),
name='single'),
我的views.py
my views.py
class PostList(SelectRelatedMixin, generic.ListView):
model = Post
select_related = ('user', 'group')
class UserPosts(generic.ListView):
model = models.Post
template_name = 'posts/user_post_list.html'
def get_queryset(self):
try:
#prende i post fatti dal'username che è lo stesso di quello che è loggato per guardarli
self.post_user = User.objects.prefetch_related('posts').get(username__iexact=self.kwargs.get('username'))
except User.DoesNotExist:
raise Http404
else:
return self.post_user.posts.all()
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['post_user'] = self.post_user
return context
class PostDetail(SelectRelatedMixin, generic.DetailView):
model = models.Post
select_related = ('user', 'group') #group senza apostrofo?
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(
user__username__iexact=self.kwargs.get('username'),
# maybe smth is needed here?
)
我的模型.py
User = get_user_model()
# Create your models here.
class Post(models.Model):
user = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name="posts", null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.message
def save(self, *args, **kwargs):
self.message_html = m.html(self.message)
# in questo modo quando si fa un markdon si mette un link nel loro post
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('posts:single', kwargs={'username':self.user, 'pk':self.pk})
class Meta:
ordering = ['-created_at']
unique_together = ['user', 'message']
我的馅饼显示帖子作者和帖子发布日期的模板的e:
my piece of template showing author and publishing date of a post:
<span class="username">
<!-- mi porta a tutti i post fatti da quell'user -->
<a href="{% url 'posts:for_user' username=post.user.username %}">@{{ post.user.username }}</a>
</span>
<a> – </a>
<!-- il time tag posta il tempo -->
<time class="time">
<a href="{% url 'posts:single' username=post.user.username pk=post.pk %}">{{ post.created_at }}</a>
</time>
模型 User是django的默认模型。User
The model "User" is django's default models.User
问题必须出在PostDetail视图中,因为我通过单击url验证了我的模板显示了正确的帖子pk(post.pk)
The problem must be in PostDetail view, since I verified that by clicking on the url, my template shows the post correct pk (post.pk)
也许视图中缺少过滤器?看到我的#注释
Maybe a filter is missing in views? see my # comment
推荐答案
最后缺少一个 / $
第一个网址:
A /$
was missing at the end of the first url:
url(r'by/(?P<username>[-\w]+)/$',
views.UserPosts.as_view(),
name='for_user'),
但是我仍然不能理解用于显示所有用户帖子的视图的URL如何影响仅显示特定帖子的URL的行为。
But I still can't understend how could the url used to display the view showing all users's posts influence the behaviour of the url that should show only a specific post.
这篇关于无法获得社交网络项目中帖子的详细信息视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!