I've built a blog kinda app and there is a location section. I can add a location for a post when I create it (with django taggit) and when I search for it let's say Paris http://127.0.0.1:8000/tag/paris/ it shows me all the posts that have that tag.
the thing i couldn't add is that on my homepage right next to the username I want to show the location that post has and when I click it I want to see the other posts that have that tag, so if post1 has the tag of Paris i'll click on it and it'll show me http://127.0.0.1:8000/tag/paris/
I've tried this just to show the tag the post has
location:<strong> {{post.tags}} </strong></a>-->
but it says location: blog.Post.None
btw http://127.0.0.1:8000/tag/paris/ and all other tags work. I can see all the posts that have that tag when I search this in the search bar. I just can't seem to put the link to it anywhere.
i'll try to put just the relevant parts of the code. home.html
{% for post in posts %}
<a href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
location:<strong> {{post.tags}} </strong></a>-->
{% endfor %}
views.py
class TagIndexView(ListView):
model = Post
paginate_by = '10'
context_object_name = "posts"
def get_queryset(self):
return Post.objects.filter(tags__slug=self.kwargs.get('slug')).order_by('-date_posted')
urls.py
path('', PostListView.as_view(), name="blog-home"),
path('user/<str:username>', UserPostListView.as_view(), name="user-posts"),
path('tag/<slug:slug>/', TagIndexView.as_view(), name='tagged'),
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
tags = TaggableManager()
CodePudding user response:
You need to loop through and create a link for each tag like below.
{% for post in posts %}
<a href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
{% for tag in post.tags.all %}
location:<strong> <a href="{% url 'tagged' tag.slug %}">{{ tag.name }}</a> </strong></a>-->
{% endfor %}
{% endfor %}
