O
O
ooker2020-10-23 03:51:13
Django
ooker, 2020-10-23 03:51:13

How to display a link from a view in a Django template?

Hey!
Where can I find info on how to drop links into a template from a view? When creating an article in the admin panel, a link to a page with an identical theme was added to the "a" tag. As I understand it, you need to write a model, but what if there are a lot of pages (you need a choice)? Who faced? html{% url 'name' %}

{% for post in object_list %}
<div class="blog vertical-blog col-lg-4 col-md-4 col-sm-6 col-xs-12">
 <div class="blog-foto"><a href="{{ post.get_absolute_url }}"><img src="/media/{{ post.article_image_field }}"></a></div>
<div class="blog-date">{{ post.article_datetime }}</div>
div class="blog-subtitle"><a href="">{{ post.article_razdel }}</a></div>
<div class="blog-title"><a href="{{ post.get_absolute_url }}">{{ post.article_title }}</a></div>
</div>
{% endfor %}

view
class BlogArticles(ListView):  # Главная страница 
    model = BlogNewArticles
    template_name = "blog/blog.html"

    def get_context_data(self, **kwargs):
        context = super(BlogArticles, self).get_context_data(**kwargs)
        context['blockheader'] = BlogPromoHeader.objects.all()  # Реклама шапка
        context['blockcenter'] = BlogPromoCentre.objects.all()  # Реклама центр1
        context['blockfooter'] = BlogPromoFooter.objects.all()  # Реклама подвал
        return context

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gromyko21, 2020-10-23
@gromyko21

If I understand the question correctly, you need to specify in the url the address where you specify the path (not the root). That is, write

path('', views.home, name='home_url'),

And {% url 'home_url' %} will be your link. That is, you need to specify the name tag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question