1. Another way to configure the URL . Introduce the include method, and then create a new urls.py file under the blog. If you follow the method in the previous section, the code in the urls file in the root directory will become difficult to maintain.
ccblog\urls.py
from django.urls import path, include from django.contrib import admin urlpatterns = [ path('admin/', admin.site.urls), path('blog/', include('blog.urls')), ]
blog\urls.py
from django.contrib import admin from django.urls import path, include from . import views urlpatterns = [ path('index/',views.index), ]
2. Develop a Template
1.What are Templates
In fact, it is an HTML file one by one. The difference from the traditional html file is that Templates uses the Django template language (django templates language, DTL).#At the same time, django also allows us to use third-party templates (such as Jinja2). Template engine, just change the content in backend under TEMPLATES in settings.
2. Create steps
Create a directory named template in the root directory of the app, create an html file in that directory, and return render() in views.py
from django.shortcuts import render #Respond to the html page to the browser from django.http import HttpResponse #Create your views here.def index (request): #Note that because the request is to be processed here, the request must be received first. return render(request , 'blogindex.html' )
Initial use of DLT: The render() function supports a dict type parameter (dictionary type, that is, a key-value pair, the key is the parameter name obtained from the front end, and the value is the data we want to pass).
Use {{parameter name}} in the template to use it directly.
from django.shortcuts import render #Respond to the html page to the browser from django.http import HttpResponse #Create your views here.def index (request): #Note that because the request is to be processed here, the request must be received first. return render(request , 'blogindex.html' , { 'inner' : 'hello cc,hello bolg' })
The inner part in the html page is
<body> <h1>{{inner}}</h1> </body>
3.Notes on templates
django find templates
django looks for templates in the order in which they are added in installed_apps in settings
.html files with the same name in the templates directory under different apps will cause conflicts.