1

My model has a field call topic, and I want to create a page which can show the specific, and the urls can be dynamic path to different topics rendered by the same html template. How do I suppose to do it?

models.py:

TOPIC = (
    (0,"Finance"),
    (1,"Toys"),
    (2,"Foods"),
    (3,"Travel"),
)

class Post(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True, max_length=200)
    author = models.ForeignKey(User, on_delete=models.CASCADE,related_name='blog_posts')
    updated_on = models.DateTimeField(auto_now= True)
    created_on = models.DateTimeField(auto_now_add=True)
    body = RichTextField(null=True)
    status = models.IntegerField(choices=STATUS, default=0)
    topic = models.IntegerField(choices=TOPIC, default=0)
    cover_img = models.ImageField(upload_to='post_cover', null=True, default='post_cover/coming_soon.jpg')

def __str__(self):
    return self.name

def get_img(self):
    return f"{'/media/post_cover/'+self.cover_img}"

html

<div class="col-md-6 mt-3 ">
     <a href="{% url 'topic_list' %}">
        <img src="../media/topic/travel3.jpg" alt=''>
     </a>
</div>

views.py

def topic_list(request):
    posts = Post.objects.filter(topic=0)
    context = {'posts': posts, 'autor_list':autor_list}
    return render(request, 'topic_list.html', context)

urls.py

from django.urls import path
from . import views


urlpatterns = [
     path('', views.home, name='home'),
     path('topic/', views.topic_list, name='topic_list'),
     path('<slug:slug>/', views.post_detail, name='post_detail'),]
2
  • Are you asking 1) how to build URLs in the post_list template to each post or 2) how to make a correct URL pattern and a view for post_detail page? Commented Apr 6, 2020 at 17:00
  • actually I wamt to build a url for each topic of post list, so I dont need to make 4 pages for those 4 topic, just 1 dynamic url can go every topics Commented Apr 7, 2020 at 1:12

1 Answer 1

2

To generate a dynamic url in the template use url-reversion function with arguments

{% for post in post_list %}
   <a href="{% url 'post_detail' post.slug %}">{{ post.name}}</a>
{% endfor %}

this will reverse post_detail url pattern from your urls.py with post.slug value passed to '<slug:slug>/'

If you need to list posts per topic then fix your url pattern and the view like below:

urlpatterns = [
     ...
     path('topic/<int:pk>/', views.topic_post_list, name='topic-posts-list'),
]

def topic_post_list(request, pk):
    posts = Post.objects.filter(topic_id=pk, )
    context = {'posts': posts, }
    return render(request, 'post_list.html', context)
Sign up to request clarification or add additional context in comments.

4 Comments

what if I want to go to the post_list and filter the primary key by 'topic'? just like the hm_pg.html have a url can go to {% url 'post_list' pk=post.topic %}, the dynamic url for topic in models.py
You better fix your views and patterns names. If you want to see posts for particular topic - name it topic_post_list or something like that.
so what's next after I name it? how to go different topic post list by dynamic url?
In my code it is topic-posts-list, not post_list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.