L
L
Lord_Dantes2019-04-27 15:23:36
Django
Lord_Dantes, 2019-04-27 15:23:36

How to pass 2 parameters to urlspatterns?

I have
5cc4491b83701564497611.png
It sets the template for the main page of the site.
The page has posts (output models) that I would like to connect somehow.
According to the logic (apparently mine), you need to add just one more parameter to the screenshot above.
5cc4495ad5cbf809729180.png
But, of course, it did not work and knocked out an error. 5cc44978a25f7819371189.png
Please tell me, maybe I should somehow reformulate the function? Tipo so that she receives a model and then displays it in a loop.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-04-27
@Lord_Dantes

You need to add data to the context:

from django.views.generic.base import TemplateView

from articles.models import Article

class HomePageView(TemplateView):

    template_name = "home.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['latest_articles'] = Article.objects.all()
        return context

In context['latest_articles'] you will have articles.
In template:
<h1>Articles</h1>
<ul>
{% for article in latest_articles %}
    <li>{{ article.pub_date|date }} - {{ article.headline }}</li>
{% empty %}
    <li>No articles yet.</li>
{% endfor %}
</ul>

Or do it via ListView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question