V
V
Vladimir Timoshko2019-07-13 01:08:57
Django
Vladimir Timoshko, 2019-07-13 01:08:57

Why are database records not showing up?

I need to integrate pagination into my site. Using the method provided in the documentation , I get an error: local variable 'news' referenced before assignment
Views.py code:

from django.core.paginator import Paginator

def main(request):
    news_list = news.objects.all()
    paginator = Paginator(news_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    news = paginator.get_page(page)
    return render(request, 'school/main.html', {'main': main})

But by importing models , and the page is already rendered. True, the news is not displayed.
The code:
from django.core.paginator import Paginator
from . import models

def main(request):
    news_list = MODELS.news.objects.all()
    paginator = Paginator(news_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    news = paginator.get_page(page)
    return render(request, 'school/main.html', {'main': main})

-------------------------------------------------- -------------------------
Maybe someone needs it - here is the urls code:
from . import views, models

urlpatterns = [
    path('main/', views.main, name="main"),
    ...
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2019-07-13
@IIIaTaTeJIb

The main function object is passed to the template, which definitely does not have news somewhere in the dictionary. That's why the list is empty.

A
Alexey Guest007, 2019-07-13
@Guest007

Just the same, following the example, you need to write instead of{'main': news}{'main': main}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question