J
J
Juvel19882022-04-01 00:51:57
Django
Juvel1988, 2022-04-01 00:51:57

How to display the announcement of the latest news in the block on the main page in django?

I am writing a website in Django. This is my first time doing this on my own. I can't create a block with the announcement of the latest news in the block on the main page. And for some reason the news is not displayed on the news page. What am I doing wrong?

The news model itself looks like this:

class Events(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='Автор', null=True, blank=True)
    main_photo = models.ImageField('Постер', null = True, blank = True, upload_to='prosvet/media/images/main')
    anons = models.CharField(max_length=350)
    body = RichTextUploadingField(
                                    null=False,
                                    blank=False,
                                    # config_name='toolbar_Custom',
                                    external_plugin_resources=[(
                                        'youtube', 
                                        '/static/ckeditor/ckeditor/plugins/youtube/',
                                        'plugin.js',
                                      
                                    )],
                                    )
    #file = models.FileField(null = True, blank = True, upload_to='static/media/docs/')
    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 file:

path (r'afisha', views. articles, name = 'afisha'),#Лента новостей
    path('ckeditor/', include('ckeditor_uploader.urls')),#редактор статей
    path(r'new/<el_id>', views.el, name='el'),#Вывод отдельной новости


view.py file:

def articles(request):#Страница новостей
        temp = Events.objects.order_by('-date_added') 
        context = {'afisha': temp}
        return render(request, 'gorozhane/afisha.html', context)



def last_article():
    last_pages = Events.objects.order_by("-id")[0:3]
    return {
        'last_pages': last_pages,
    }



def el(request, el_id): #Вывод отдельной новости
    news = Events.objects.get(id=el_id)
    context = {'news':news}
    return render(request, 'gorozhane/article.html', context)

def date(request):
    now = datetime.datetime.now()
    print("Date: "+ now.strftime("%Y-%m-%d")) #текущая дата


def index_view(request):
    return render(request, 'index.html')


The news page looks like this:

{% extends "gorozhane/base.html" %}

{% block content %}

<div class="content">

    <h2>{{ news.title }}</h2>
    <p>{{ news.date_added|date:'M d, Y H:i' }}</p>
    <div class="content_block">
        <p>{{ news.body|safe }}</p>
        <p>{{ news.file.url }}</p>
    </div>

    <div id="author">
        <p><img src="{{ news.author.avatar.url }}" height="160" align="bottom" /></p>
        <p valign="bottom">{{ news.author }}</p>
    </div>

</div>

{% endblock content %}


Displaying news on the news page:

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

<div class="content">
    {% if afisha %}
    {% for article in afisha %}


    <h2 align="left"><a href="{% url 'el' article.id %}">{{ article }}</a></h2>
    {% if article.main_photo %}<br>
    <p><a href="{% url 'el' article.id %}"><img src="{{ article.main_photo.url }}" width="640"></a></p>
    {% endif %}
    <div id="anons">
        <p align="center">{{ article.anons }}</p>
    </div>

    {% endfor %}
    {% else %}
    <p>Новостей нет</p>
    {% endif %}

</div>
{% endblock content %}


And here is the same block on the main page:

{% last_article as last %}


        {% for article in last.last_pages %}

        {{ article.title }}

        {% endfor %}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question