Django: Building a Personal blog (part 4)

[TOC]

Develop the article details page

1. The URL binding

Add the matching rule for the detail view function to blog/url.py:

urlpatterns = [
    path(' ', views.index, name='index'),
    Pk is passed to the detail view function as the second argument, the first of which is request
    # 
      
       ,
       
        , etc
       
      
    path('post/<int:pk>',views.detail,name='detail')]Copy the code

2. Detail view function

Add the following to views.py, where the get_object_or_404 method can query the data in the database by pressing the primary key, or return a 404 response if it does not exist.

from django.shortcuts import render,get_object_or_404
def detail(request,pk) :
    post=get_object_or_404(Post,pk=pk)
    return render(request,'blog/detail.html',context={
        'post':post
    })
Copy the code

Define the get_absolute_URL method for Post model:

def get_absolute_url(self) :
    The # reverse function finds the URL named detail in the Blog application's UrlPatterns, passes in the argument in kwargs, and returns the URL
    # Through this way of url parsing, the binding between URL and view function is more flexible, and the association between URL and model instance is realized
    return reverse('blog:detail', kwargs={'pk': self.pk})
Copy the code

3. How to use template inheritance

Since the article details page and the article contents page are mostly the same, template inheritance is adopted.

Create a new base.html in the templates/ directory and embed the following structure:

<main class="col-md- 8 "> {%block main %}
{% endblock%} < /main>

<aside class="col-md- 4 "> {%block aside %}
{% endblock%} < /aside>
Copy the code

In the templates/blog/detail. In HTML, first by {% extends’ base. HTML ‘%} statement shows that inheritance, to write details within the block

{%extends 'base.html'%}
{%block main%}
Fill in the details of the main TAB here, for example:
	<body>
		<h1 class="entry-title"> {{post.title}} < /h1>  # postbydetailThe view function is passed to the template </body>
{%end block%}

{%block aside%} # written hereasideDetails of the tag (sidebar related content) {%end block%}
Copy the code

In this way, template inheritance can be implemented, similar to the way the child template detail.html overwrites the block wrapped part of the parent template base.html.

4. Jump from the contents page to the details page

Call the {{post.get_absolute_URL}} template variable just defined in the index. HTML article title and “Read On” to get the URL of the clicked article (template variable can be a property of the model as well as a callable method).

<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
<a href="{{ post.get_absolute_url }}" class="more-link"> < p style = "max-width: 100%; clear: both;span class="meta-nav"> - < /span></a>
Copy the code

5. Fill in the details page

Fill the template with template variables, using body as an example:

<div class="entry-content clearfix">
    {{ post.body }}
</div>
Copy the code

Knowledge summary

  • The view function
  • Template inheritance
  • Reverse parse the url of the article

Next, develop the contents of the sidebar.