How to use Django-3 templates
This article shows you how to use the Django-3 template, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
We learned that templates allow us to reuse code snippets in multiple places, making them ideal for dynamic HTML pages. Use a template to return the more complex HTML to the browser. We will also see how to pass variables as context to the template. Let's start with:
Create a new folder templates\ blog under django_project\ blog\ to store all static html pages, and create a new folder static\ blog to store style files, image files, and so on:
Modify django_project\ settings.py and add the configuration of blog:
Modify django_project\ blog\ views.py:
From django.shortcuts import render# defines a list for foreground data display using posts = [{'author':' Liaobei Emperor', 'title':' blog first post', 'content':' blog first post', 'date_posted':' 04 July 2019'}, {'author':' Love Miracle' 'title':' blog second post', 'content':' blog second content', 'date_posted':' 04 July 2019'}]
# define the home page access method def home (request): context = {'posts':posts} return render (request,' blog/home.html', context) # define about the access method def about (request): return render (request, 'blog/about.html')
Create a new style file main.css under django_project\ blog\ static\ blog:
Create a new base.html under django_project\ blog\ templates\ blog to store general blog page template information:
Create a new home.html under django_project\ blog\ templates\ blog, store the content on the first page of the blog, and display a list of posts:
Create a new about.html under django_project\ blog\ templates\ blog to display information about the page:
We run the python manage.py runserver startup project under the django_project folder and access http://127.0.0.1:8000/:
Next, let's visit the http://127.0.0.1:8000/about page about:
The above content is how to use the Django-3 template, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.