Django: Building a Personal blog (part 2)

[TOC]

1. Django mode

As a Web framework, Django is responsible for receiving HTTP requests and returning the corresponding HTTP responses. In this process, there are three problems that need to be solved:

  1. How do I receive HTTP requests
  2. How do I handle HTTP requests
  3. How do I generate an HTTP response

2. Construct the view function and bind the URL

Create a view function for the home page in blog/views.py:

from django.shortcuts import render
from .models import Post
# Create your views here.
The view function receives an HTTP request and returns an HTTP response
def index(request) :
    Get the list of articles in the database, in reverse order by the time they were created
    post_list=Post.objects.all().order_by('-createdTime')
    The # render function is used to render the template (which translates to inserting data and static files into HTML) and returns an HTTP response with the following three parameters representing the HTTP request, the template file, and the template variable
    return render(request,'index.html',context={
        'post_list':post_list,
    })
Copy the code

New blog/urls.py file:

from . import views
from django.urls import path
Bind url to view function
urlpatterns = [
    path(' ', views.index, name='index')  Add an empty string to the domain name, i.e. the home page, based on the domain name + port
]
Copy the code

Import your blog app’s urlpatterns with the include() function in tony_blog/urls.py for easy maintenance:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path(' ', include('blog.urls'))  # where the path function concatenates "and", it is still a string
]
Copy the code

3. Modify the. HTML template and add template variables

The template file is usually in BASE_DIR/templates/, where you can use dream Chaser’s blog – the template used in Blogs from Streaks to Skins, or you can download other useful templates to use.

The project directory structure is as follows:

Register the template file path in settings.py:

About template variables and template tags:

  • with{{}}Wrapped around the template variables is where the view function passes values and displays them
  • with{% %}Wrapped around template tags are functions that do some control

Importing template variables into the index. HTML template file involves some HTML syntax:

1. Pass in all posts with {% for post in post_list %}, where post is used to represent an article variable in the body of the loop. Pass the template variables {{post.pk}}, {{post.title}}, {{post.category. Name}}Copy the code

4. Add static files to the template

Static files include CSS and JS files as well as images that are used to style the page. There are three steps:

  1. The path to the static file

    Create a new static/blog/ directory to store the static file of the blog application as follows:

  2. Set the static file URL prefix to the same path in setting.py:

    STATIC_URL = '/static/'
    Copy the code
  3. Reference the static file in the template with the {% static “path” %} template tag, which concatenates the STATIC_URL with path as the address of the static file.

    Note the insertion at the beginning of the file{% load static %}, load the static module<link rel="stylesheet" href={% static "blog/css/bootstrap.min.css" %}>
    Copy the code

CTRL /Shift+F5 Refresh the page or restart the project. If the page style is displayed normally, the static file is successfully imported.

Knowledge summary

  • The view function
  • Url binding
  • HTML template file
  • Static files

The resources

Blog for Dreamers -Django’s Way of Getting Customers, etc

www.cnblogs.com/ouwen-lengy…