Fix a problem with pycharm connecting DB to MySQL
Solve:
In the project's __init__.py file, add
Import Pymysql
Pymysql.install_as_mysqldb ()
When the post submits the form data, 403 Forbidden is prompted by default, and a {% Csrf_token%},render method is added to the form form to render a random string for it to be submitted to the server for validation, which resolves the You can also temporarily comment out the configuration of csrf in Settings. (Not recommended)
Custom Labels and filters
Django provides a limited number of tags and filters, and more is the need to customize the label and filter, the following is the process:
1 . Installed_apps in Settings Configure the current app, or Django cannot find a custom Simple_tag.
2, Create the Templatetags module in the app (module name can only be templatetags)
3 . Create any. py file, such as: my_tags.py
From Django Import Template
From django.utils.safestring import Mark_safe
Register = template. Library ()
# The above is a fixed format
@register. Filter #自定义过滤器
def multi (x, y):
Return X*y
@register. simple_tag# Custom Labels
def mult_tag (x, Y, z):
Return x**y**z
4. Import the previously created my_tags.py in an HTML file that uses custom Simple_tag and filter
{
%
load my_tags
%
}
5. Using Simple_tag and filter (How to call)
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
{#自定义过滤器: Only 2 parameters are accepted when rendering a page through the Render method #}
{% load MyTag%}
{#自定义标签: can accept more than one parameter, no problem when rendering a page by the Render method #}
</body>
Note: Filter can be used after the IF statement, Simple_tag can not
{
%
if
num|filter_multi:
30
>
100
%
}
{{ num|filter_multi:
30
}}
{
%
endif
%
}
Summarize:
Tempalte (template layer): function: To more logically render the data in the database to the template syntax variable {{}} Depth query: A period character. Filter: {{var|Filter_name: Parameter}} tag {% URL%} {% for I in obj%} {% ENDFOR%{% if%} {%endif%} {% with%} {% csrf_token%} custom filters and Tags: define the process: 1. Installed_apps in Settings Configure the current app, or Django cannot find a custom Simple_tag. 2.Create a templatetags module in the app ( Module name can only be templatetags) 3, create any. py file, such as: My_tags.py:from Django import template from Django.utils.safestring import< c8> Mark_safe Register = template. The name of the Library () # Register is fixed and cannot be changed @register. Filter # defines filters def multi (x, y): return x*y @register. Simple_tag # Define label def Multi_tag (x, Y, z): Return x*y*z 4, import previously created my_tags.py {% load My_ in an HTML file that uses custom Simple_tag and filter Tags%} 5, using Simple_tag and filter (how to call) summarizes the difference: 1, custom filter can only accept two parameter 2, custom Simple_tag cannot use inheritance with if: Create base.html {%block content%}{%endblock%}
Template Inheritance (Extend)
The most powerful and complex part of the Django template engine is the template inheritance. Template inheritance lets you create a basic "skeleton" template that contains all the elements in your site, and can define blocks that can be overwritten by a quilt template.
By starting with this example, you can easily understand the template inheritance:
View Code
This template, which we call it base.html
, defines a simple HTML skeleton that can be used for two-column layout pages. The work of the "sub-template" is to fill the empty blocks with their contents.
In this example, the block
tag defines three blocks that can be filled with template content. block
tell the template engine: the sub-template may overwrite these locations in the template template.
The sub-template may look like this:
View Code
extends
Tags are the key here. It tells the template engine that the template "Inherits" another template. When the template system processes the template, first, it locates the parent template-in this case, the "base.html".
At that point, the template engine will take note base.html
of the three tags in and replace the blocks with the contents of the block
sub-template. Depending on blog_entries
the value, the output might look like this:
View Code
Note that the sub-template does not have sidebar
a block defined, so the system uses the values from the parent template. The content in the parent template's {% block %}
label is always used as an alternative (fallback).
This approach maximizes the reuse of code and makes it easier to add content to a shared content area, for example, partial-scope navigation.
Here are some hints for using inheritance:
-
If you use in the template, {% extends%}
label, it must be the first label in the template. In any other case, template inheritance will not work.
-
Sets the more in the base template; {% block%}
label the better. Keep in mind that the sub-template does not have to define all the blocks in the parent template, so you can populate the most blocks with the appropriate default content, and then define only the one you need. A little more hooks is better than a little bit less.
-
If you find yourself copying content in a large number of templates, it might mean that you should move the content to a in the parent template; {% block%}
.
-
If You need to get the content of the block from the parent template, the {{Block.super}} variable'll do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{block.super}}
will not being automatically escaped (see The next section ), since it was already escaped, if necessary, and in the parent template.
-
For better readability, you can also give your , {% endblock%}
tag a name . For example:
{% block content%} ... {% Endblock content%}
Finally, note that you cannot define multiple labels of the same name in one template block
. This limitation exists because the block tag acts as a "two-way". This means that the block tag not only provides a pit to fill, it also defines the contents of the pits in the _ Parent template _. If you have two names in a template block
, the parent template of the template will not know which block to use.