This is Qing'an. I study a little every day. Today is no exception. V: qing_an_an
The template in Django controls the static data of the front end. A complete web should have the static data of the front end.
First, you have to prepare your own application. The preparation process is no longer mentioned here.
Create a template file for managing templates.
templates:
It can be placed inside the application or outside the application, as long as it is in the project.
The file name is fixed and cannot be changed arbitrarily. In this file, we create a template in HTML5 format.
Here, because I wrote it in advance, there are five HTML templates. But the body content on the right can be added.
Next, we write the code content in the application view:
# Whether you create a model or a template, you return resources to the route through the view def hello(request): return render(request, "hello.html")
If you are more careful here, you will ask. In front, you use HttpResponse and here you use render, because there is a template here.
After reading my notes carefully, both the model and template return resources to the route through the view. Next, we need to add a path to the route.
from testapp.views import * urlpatterns = [ path('hello/',hello), ]
The import * here is all in the default import view. This is relatively simple. There is no need to write one and import one.
Next, run the Django code, python manage.py runserver:
Here you can see that the content in the view is presented in the browser.
Dictionary value
The default value specified in the template is directly passed here. If the dictionary is passed in, how to write the above words if you want to present the form of key value pairs.
View writing:
def hello1(request): data = {'key1': 'Hello', 'key2': 'QingAn', 'key3': '!'} return render(request, "hello1.html", data)
Slightly change the route:
from testapp.views import * urlpatterns = [ path('hello1/',hello1), ]
Template writing method:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p> {{key1}} {{key2}} {{key3}} </p> </body> </html>
Use two curly braces to receive the transmitted value, so that you can get the above word effect.
Dictionary list transfer value
View writing:
def hello2(request): data = {'key':['Hello', 'Qing', 'An', '!']} return render(request, "hello2.html", data)
Slightly change the route:
from testapp.views import * urlpatterns = [ path('hello2/',hello2), ]
Template writing method:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p> {{ key.0 }} {{ key.1 }} {{ key.2 }} {{ key.3 }} </p> </body> </html>
The value here is similar to the subscript value in the list. Run by yourself to see the effect. The comparison picture is the same as the picture pasted at the beginning.
Dictionary embedding dictionary value transfer
View writing:
def hello3(request): data = {'key': {'key1': 'Hello !', 'key2': 'Qing', 'key3': 'An'}} return render(request, "hello3.html", data)
Slightly change the route:
from testapp.views import * urlpatterns = [ path('hello3/',hello3), ]
Template writing method:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p> {{ key.key1 }} {{ key.key2 }} {{ key.key3 }} </p> </body> </html>
This is similar to point-to-point value transfer, point-to-point matching.
Yes or no
When testing the front end, we occasionally see such a scenario. Clicking yes will change to no, and clicking again will change from no to yes. How can this be achieved. Let's simply implement it today:
View code:
def hello4_if(request): num = random.randint(0, 1) # Generates a random integer from a given return render(request, "hello4.html", {'num': num})
Routing code:
from testapp.views import * urlpatterns = [ path('hello4/',hello4), ]
Template code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p> {% if num > 0 %} yes {% else %} no {% endif %} </p> </body> </html>
Here, we generate random numbers for judgment. A view returns the results to the template, and the template makes its own judgment and outputs yes or no. Effect browse by yourself.
for loop
View code:
def hello5_for(request): list = ['Hello', 'World'] # Receive dictionary only return render(request, "hello5.html", {'key': list})
Routing code:
from testapp.views import * urlpatterns = [ path('hello5/',hello5), ]
Template code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p> {% for foo in key %} {{ foo }} {% endfor %} </p> </body> </html>
This will cycle through the values in the list. The code in the template is similar to the for loop in python, but the format of writing code is different. So that's what you need to pay attention to.
In addition, every time I pass the following values in the form of a dictionary, so this is also a point of attention!