In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of static files and templates in Django, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Template
Previously, our view function test used HttpResponse to return a string
As our first Django program
But this is far from enough. You say that if other people look at their own web page, there are only a few lines of text there, which is neither beautiful nor meaningful.
Storage catalogue
The web pages we usually see are presented to us through HTML, and so is Django. A complete system must have a large number of web pages for different functions, and those HTML files also have a special directory called templates templates. This directory is generally created under the APP directory, and each APP can create a templates to store HTML files. When we call the HTML file, the system will find the desired HTML file in the templates under each APP according to the order in which the APP is registered.
Template call
So now let's create a templates directory under the APP file.
And create a HTML file named demo in this directory
After typing something, you can call this template in the view function.
Go to the view.py file to define a function to call the demo.html template.
From django.shortcuts import render, HttpResponse # Create your views here. Def test (request): return render (request, "demo.html")
Note: the function must add the request parameter, and the render used when referencing the html template also requires the request parameter.
Next, in the url.py file, it is OK to determine the relationship. (the web page path takes hd/ as an example)
After clicking run, enter the URL http://127.0.0.1:8000/hd/ to enter.
You can see a run result similar to the one above.
Template syntax
Django is not a markup language, it can process data. The Django statement is executed before rendering the web page, and then the logical result that can be presented to the user is replaced by the Django statement in the corresponding position.
Data transmission
When making a web page, if we can't know the value of the element at once, for example, we need to get the data operation through the database, then we have to pass the needed data to HTML in the view function in the form of a dictionary. Happily, all common data types can be passed.
Passing method: the variable is passed in dictionary form through the render function, and when referenced by HTML, it is referenced by double curly braces + key name.
Try it now!
Write some functions in the view function, and then pass them
S = "this is a string" d = {"this is a": "dictionary"} return render (request, "demo.html", {"str": s, "dict": d})
Then reference it in the corresponding HTML page
{{str}} {{dict}}
Run at the last point.
Element reference
Django also supports referencing values in data types such as dictionaries, lists, tuples, etc., except for Django. Replaced [].
For example, if you want to reference the values in a dictionary, you need to use {{dictionary name. The key name}} refers to the value (note that the key name does not need to use quotation marks. If you want to reference the list, then {{list name. Index value}} or multidimensional list {{list name. Index 1. Index 2.}} to reference.
View.py
Def test (request): d = {"this is a": "dictionary"} L1 = [1,2,3] L2 = [[1,2], [3,4]] return render (request, "demo.html", {"list1": L1, "dict": d, "list2": L2})
Demo.html
Look, is the call successful? {{list1.0}} {{list2.0.1}} {{dict. This is a}}.
Result
For statement
Django also supports loop statements, but you need to add a closing statement after using the loop. These commands are all between the {% command%} angle bracket percent sign. {there is no space between and%
When cycling the dictionary, you don't need to add parentheses after the key (keys), values (value), and key-value pair (items).
Look at the dictionary first.
D = {"Today": "Weather", "really nice": "right?" } L1 = [1,2,3] return render (request, "demo.html", {"list1": L1, "dict": d}) see if the call is successful? {% for k, v in dict.items%} {{k}}-{{v}} {% endfor%}
If you look at the list, Django does not support range, so you can only iterate through the list.
Same string of code.
Def test (request): d = {"Today": "Weather", "really nice": "right?" } L1 = [1,2,3,4] return render (request, "demo.html", {"list1": L1, "dict": d})
Just changed the HTML.
Look, is the call successful? {% for i in list1%} {{list1}} {{I}} {{list1.i}} there seems to be no output {% endfor%} before.
The loop ends with endfor. In addition, list elements do not seem to be accessible through list values, resulting in {{list1.i}} having no value
If statement
Django also supports conditional statements.
{% if list.1 = = 0%} 11111111111111111 {% elif list.1 = = 1%} 2222222222222222222 {% else%} 33333333333333333 {% endif%}
Static file
In order to present more beautiful content or improve the efficiency of coding, we often use some static files.
In the development process, pictures, plug-ins, css,js and so on are generally treated as static files.
Storage catalogue
Static files are so important, of course, they have their own destination, and they are all stored in the static directory. The same static directory is also created under APP, and each APP can have a directory static dedicated to static files. We'd better be able to classify and store different types of files under static. For example, pictures are stored in the img folder, plug-ins in the plugins folder, css files in the css folder, and js in the js folder.
File call
Let me take a picture as an example to explain how it is called.
Let's put the picture in the img folder of static first.
The body in the demo.html template uses the img element to call the picture. The general call goes like this
If the picture is not displayed, rerun it should be able to load.
However, there is a syntax for calling static files in Django, and we also recommend using methods that specifically invoke static files using Django. Enter the setting and slide to the bottom with this variable.
This is where our static files are stored. Using the Django syntax, if the static file is renamed or moved to another location, just modify it here, instead of modifying the path one by one when html references the static file.
Let's take a look at how to use it now. Enter at the top of html
{% load static%}
Declare to import a static file
I don't know why I have a red line on the second line. Maybe pycharm doesn't know that this is a Django statement, regardless of it, and then enter the import path of the img image.
Click to run
Enter the URL http://127.0.0.1:8000/hd/ to complete the picture call.
The method of importing files such as css,js is the same, as long as we know the syntax of Django importing static files, rendering exquisite html pages will not be a problem! Now go and try it.
Thank you for reading this article carefully. I hope the article "sample Analysis of static Files and templates in Django" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.