C
C
cromvwell2020-03-09 15:59:45
Django
cromvwell, 2020-03-09 15:59:45

Why is the data not coming to the template?

Can you please tell me why the context is not passed to the template?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ article.title }}</title>
</head>
    <h1>{{ article.title }}</h1>
    <p>
        {{ article.text }}
    </p>
    <p>
        {{ article.published_date }}
    </p>
    {% if user.is_authenticated %}
        <a href="{% url 'delete' pk=article.id %}" class="btn btn-primary">Удалить</a>
        <a href="{% url 'edit' pk=article.id %}" class="btn btn-primary">Изменить</a>
    {% endif %}
</body>
</html>


URLs
path('article/<int:pk>', views.view_article, name='article'),


views
def view_article(request, pk):
    article = Article.objects.filter(id=pk)
    return render(request, 'view_article.html', context={'article': article})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-03-09
@cromvwell

You are passing a Queryset to the template. It doesn't have title and other attributes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question