如何使主题标签可点击并显示在Django的帖子中? [英] how can make hashtag clickable and show it in the post in Django?
问题描述
用户可以制作主题标签.现在,我想使主题标签可点击.贝娄是我的Hashtag的Models.py:
User can make hashtag. Now I want to make hashtag clickable. Bellow is my Models.py for Hashtag:
class Hashtag(models.Model):
hashtagname = models.CharField(max_length=3000, blank=True, null=True)
hashtagcount = models.IntegerField(null=True, blank=True)
post_pk = models.ManyToManyField(PostUser)
atetime = models.DateTimeField(auto_now_add=True)
date = models.DateField(auto_now_add=True)
time = models.TimeField(auto_now_add=True)
user_pk = models.IntegerField()
在我的views.py文件中,我获取了所有主题标签,并将其作为字典代码通过字典发送到html:
and in my views.py I get all the hashtags and send it by dictionary to html as bellow codes:
mosttrending = Hashtag.objects.all().order_by('-hashtagcount')[:10]
并且我已经为所有帖子建立了模型
and already I have a model for all the Posts as bellow
class PostUser(models.Model):
posttype = models.CharField(max_length=3000, default='postuser')
content = models.TextField(null = True, blank= True)
media_image = models.FileField(null = True, blank= True)
media_video = models.FileField(null = True, blank= True)
per_to = models.CharField(max_length=300, null=True, blank=True, default='everyone')
status = models.CharField(max_length=3000, default='active')
date = models.DateField(auto_now_add=True)
time = models.TimeField(auto_now_add=True)
datetime = models.DateTimeField(auto_now_add=True)
like = models.IntegerField(null=True, blank=True)
comment = models.IntegerField(null=True, blank=True)
share = models.IntegerField(null=True, blank=True)
user_pk = models.IntegerField()
在带有for循环的html中,我得到了所有PostUser:
And in my html with a for loop I get all the PostUser:
{% for readposts in readposts %}
<p class="card-text">{{readposts.content|linebreaks}}</p>
{% endfor %}
如何为主题标签建立链接,当用户单击主题标签时,如何向用户显示所有帖子?
How Can I make the link for hashtag and when a user click on the hashtag show the user the all the post?
推荐答案
我在django应用中的文件夹名称templatetags中创建了app_extras.py和 init .py,然后在app_extras.py中编写了代码下面并且工作了
I made app_extras.py and init.py in a folder name templatetags in my django app then in the app_extras.py wrote the code bellow and worked
import re
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
register = template.Library()
def create_hashtag_link(tag):
url = "/tags/{}/".format(tag)
# or: url = reverse("hashtag", args=(tag,))
return '<a href="{}">#{}</a>'.format(url, tag)
@register.filter(name='hashchange')
def hashtag_links(value):
return mark_safe(
re.sub(r"#(\w+)", lambda m: create_hashtag_link(m.group(1)),
escape(value)))
然后在HTML中,我已经加载了app_extras.py
then in HTML I have loaded the app_extras.py
{% load app_extras %}
然后在我的循环html中,我调用了该函数
Then in my loop html I called the function
{% for readposts in readposts %}
<p class="card-text">{{readposts.content|hashchange|linebreaks}}</p>
{% endfor %}
还有我添加的urls.py
and also my urls.py I have added
url(r'^tags/(?P<tags>\w+)/$', views.tags, name='tags'),
然后当然是标签的视图,在这种情况下,我的视图是
then of course a view for tag which in this case my view is
def tags(request, tags):
readpostsuser = PostUser.objects.filter(content__icontains = tags).order_by('-pk')
return render(request, 'front/tags.html', {'readpostsuser':readpostsuser})
这篇关于如何使主题标签可点击并显示在Django的帖子中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!