ROCA
ROCA

Reputation: 149

Show a list of Posts in a different URL

Django Blog APP comes with an out of the box index.html where it shows a list of all posts.

I need to show this list in a new URL, when I somply copy the html code from index.html and paste onto planoacao.html it doesn´t show anything.

This is the index.html:

 {% for post in post_list %}
                <div class="card mb-4" style="width: 18rem; ">
                    {% if post.get_finalizado_display  == 'Não' %}
                    <!--<p class="card-text text-muted h6"> NOK </p>-->
                    <div class="card-body" style="background-color:#FF0909;">
                    {% else %}
                    <!--<p class="card-text text-muted h6"> ok </p>-->
                    <div class="card-body">
                    {% endif %}
{% endfor %}

This is my views.py:

from django.views import generic
from .models import Post

class PostList(generic.ListView):
    queryset = Post.objects.filter(status=1).order_by('-created_on')
    template_name = 'index.html'

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'post_detail.html'

class Calendar(generic.DetailView):
   model = Post
   template_name = 'calendar.html'

class Planoacao(generic.DetailView):
   model = Post
   template_name = 'planoacao.html'

This is my urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
    url(r'^planoacao/', TemplateView.as_view(template_name="planoacao.html")),
    url(r'calendar', TemplateView.as_view(template_name="calendar.html")),
    url(r'^admin/', admin.site.urls),
    url(r'^', include('blog.urls'), name="Blog"),
    ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

How can I show a list of all posts in a different URL? xxx/planoacao.html ? Simply copying the html from index.html doesn´t work.

Note that I don´t want to change the regular index.html post list, I just want to add a second post list page.

Upvotes: 0

Views: 36

Answers (1)

voodoo-burger
voodoo-burger

Reputation: 2153

It seems you want to show your PostList view at /planoacao, so in urls.py you can hook this path up to that view:

url(r'^planoacao/', PostList.as_view()),

Note that in Django there is no direct link between the path of a view and the template used. You can use any template with any path, all you have to do is define which template to use in the view.

Upvotes: 1

Related Questions