I'm trying to make a facebook like Newsfeed. I have a Post
which contains all post. After sending it from the view I load the posts to the templates with {% for p in posts %}
, for each post I add 2 buttons (share, like) and an input (comment).
我正在努力制作像新闻一样的Facebook。我有一个包含所有帖子的帖子。从视图中发送后,我将帖子加载到模板中{%for p in posts%},每个帖子我添加2个按钮(分享,像)和输入(评论)。
How can I know which of them was clicked so I can send the post.id back to the view, including the name, and value and trigger a function to handle them in the database?
我怎么知道它们中的哪一个被点击了所以我可以将post.id发送回视图,包括名称和值,并触发一个函数来处理它们在数据库中?
I want to know after I click an input what was it, what it included and for which post it belongs.
我想知道点击输入后是什么,它包含什么以及它属于哪个帖子。
1 个解决方案
#1
0
The way I would have approached this is something as follows:
我接近这个的方式如下:
I would have specific views to handle share and like functionalities, I would rather have them as two separate views. For them you then create urls, definitely you will be also providing post identifier as a parameter to this url.
我会有特定的观点来处理共享和类似的功能,我宁愿把它们作为两个单独的视图。对于他们你然后创建网址,绝对你也将提供帖子标识符作为该网址的参数。
For eg:
class LikePost(View):
def post(self, request, post_id, *args, **kwargs):
code
For this class, you can then have a url like ^/post/(?P<post_id>\d+)/$
.
对于这个课程,你可以有一个像^ / post /(?P
Now for more simplicity in your Post model class you can write a model method that returns you the url for LikePost view for an instance of post.
现在,为了更简单地在Post模型类中,您可以编写一个模型方法,该方法返回post的实例的LikePost视图的URL。
Something like this:
像这样的东西:
def like_post_url(self):
return reverse('appname:reversename-for-url', args=(self.id,))
So now when you loop posts in template you can simply assign this url as value for href in anchor tag you use for button. So every button will have its own url for share, like and also for input.
所以现在当你在模板中循环帖子时,你可以简单地将这个url分配为你用于按钮的锚标记中的href的值。因此,每个按钮都有自己的分享网址,也可以用于输入。
I think this should make it clear on how you can proceed with simplicity.
我认为这应该清楚说明如何进行简单化。
#1
0
The way I would have approached this is something as follows:
我接近这个的方式如下:
I would have specific views to handle share and like functionalities, I would rather have them as two separate views. For them you then create urls, definitely you will be also providing post identifier as a parameter to this url.
我会有特定的观点来处理共享和类似的功能,我宁愿把它们作为两个单独的视图。对于他们你然后创建网址,绝对你也将提供帖子标识符作为该网址的参数。
For eg:
class LikePost(View):
def post(self, request, post_id, *args, **kwargs):
code
For this class, you can then have a url like ^/post/(?P<post_id>\d+)/$
.
对于这个课程,你可以有一个像^ / post /(?P
Now for more simplicity in your Post model class you can write a model method that returns you the url for LikePost view for an instance of post.
现在,为了更简单地在Post模型类中,您可以编写一个模型方法,该方法返回post的实例的LikePost视图的URL。
Something like this:
像这样的东西:
def like_post_url(self):
return reverse('appname:reversename-for-url', args=(self.id,))
So now when you loop posts in template you can simply assign this url as value for href in anchor tag you use for button. So every button will have its own url for share, like and also for input.
所以现在当你在模板中循环帖子时,你可以简单地将这个url分配为你用于按钮的锚标记中的href的值。因此,每个按钮都有自己的分享网址,也可以用于输入。
I think this should make it clear on how you can proceed with simplicity.
我认为这应该清楚说明如何进行简单化。