J
J
Juvel19882021-02-10 23:30:30
Django
Juvel1988, 2021-02-10 23:30:30

Where did I make a mistake in writing the function?

I'm new to programming and I've come across a problem that I can't figure out. When opening the news page by link, the following error is displayed:

entries = el.en_set.order_by('-date_added')
AttributeError: 'function' object has no attribute 'en_set'
. I understand the problem, but I don't understand how to solve it. Confused in my own code.

models.py
class News(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=50,unique=True)
    author = models.ForeignKey(User,on_delete=models.CASCADE,
    related_name='news_posts', null=True, blank=True)
    anons = models.CharField(max_length=350)
    body = RichTextUploadingField(null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)
    # Возврат понятного отображения заголовка в панель администрирования
    def __str__(self):
        return self.title
    class Meta:
        verbose_name = u"Новость"
        verbose_name_plural = u"Новости"


urls.py views.py
path(r'new/<el_id>', views.el, name='el')


def el(request, el_id): #Вывод отдельной новости
    article = News.objects.get(id=el_id)
    entries = el.en_set.order_by('-date_added')
    context = {'el':el, 'entries': entries}
    return render(request, 'prosvet_logs/article.html', context)


article.html
-generated page

{% extends "prosvet_logs/base.html" %}
{% block content %}

<div class="article">
    <h2>{{ el }}</h2>
    {% if new %}
        {% for el in new %}
        <p>{{ new.date_added|date:'M d, Y H:i' }}</p>
        <p>{{ new.body|linebreaks }}</p>
        {% endfor %}
    {% else %}
    <p>Новостей нет</p>
    {% endif %}

</div>
{% endblock content}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-02-10
@Juvel1988

So el is a function def el(request, el_id):
Maybe article should be used instead of el? (the code is not fully shown, I can’t say for sure)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question