Answer the question
In order to leave comments, you need to log in
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})
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})
from . import views, models
urlpatterns = [
path('main/', views.main, name="main"),
...
]
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question