Create a home page that loops through the posts in the model and shows the title as a link
Create an article detail page that shows the post that was clicked on the home page.
Blog post links on home page
In the app's views.py file (ablog/theblog/views.py) import ListView and DetailView - generic list and detail forms included with Django.
In the app's views.py file (ablog/theblog/views.py) we have to import our Post model.
Change home view in ablog/theblog/views.py to a class based view.
Pass the ListView to the HomeView class
Then tell the class that the model we are using is Post (our only model)
Then template_name = 'home.html' tells the class what template to use.
Pretty much same process for the Article's Detail View class.
fromdjango.shortcutsimportrender# Import Django's generic listview and detailviewfromdjango.views.genericimportListView,DetailView# Have to import our model so the views can find itfrom.modelsimportPost# Create your views here.# def home(request):# return render(request, 'home.html', {})classHomeView(ListView):model=Posttemplate_name='home.html'classArticleDetailView(DetailView):model=Posttemplate_name='article_details.html'
In template home.html a for loop to loop through the object_list which seems to get passed automatically.
Show the items in an unordered list.
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>All Articles</title></head><body><h1>Post</h1><!-- for loop to get all the posts --><!-- object_list is the query set from ListView --><ul>
{% for post in object_list %}
<li><ahref="{% url 'article-detail' post.pk %}">{{ post.title }}</a> - {{ post.author.first_name }} {{ post.author.last_name }} <br>
{{ post.body }}</li>
{% endfor %}
</ul></body></html>
Anything in the model can be used in the template. e.g. email.
The url patterns also changes to a class, have to import HomeView from .views