views.py

from django.shortcuts import render from datetime import datetime from .models import Article def index(request): context={ 'current_date':datetime.now(), 'title':'Home' } #returning a render having a request, go to index.html an pass that context return render(request,'index.html',context) def about(request): #navigates to about page context = { 'current_date':datetime.now(), 'title':'About' } #returning a render having a request, go to about.html an pass that context return render(request,'about.html',context) def news(request): #navigates to news page populate_db() articles = get_articles() context = { 'articles':articles, 'current_date':datetime.now(), 'title':'News' } #returning a render having a request, go to news.html an pass that context return render(request,'news.html',context) def get_articles(): result = Article.objects.all() return result def populate_db(): if Article.objects.count()==0: Article(title = 'First item',content = 'This is the first db item').save() Article(title = 'Second item',content = 'This is the second db item').save() Article(title = 'Third item',content = 'This is the third db item').save()
In the function def get_articles(): and populate_db():
Here, in Article.objects.all() and Article.objects.count()==0: Article is red underlined
When I put the cursor on the Article, it says ," E1101:Class 'Article' has no 'objects' member ".

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.