0Use the django environment in python scripts
import os
if __name__ == ‘__main__’:
os.environ.setdefault(” DJANGO_SETTINGS_MODULE”, “untitled15.settings”)
import django
django.setup()
from app01 import models
models.Book.objects.filter(name='123')
1 Template import–>Template reuse
1 Write a template
2 Import in another template:{% include ‘template.html’%}
2 Template inheritance (equivalent to __init__)
1 Write a master and leave an expandable area (box). You can leave multiple Boxes (the more boxes you leave, the higher the scalability)
{%block Name%}
Yes Write content
{%endblock%}
{% include 'left.html' %}
{% block c1 %}
wwww
{% endblock c1 %}
2 Used in child template:
{% extend ‘master.html’%}
{%block Name%}
{{block.super}} #The content of the master box can be inherited
{{block.super}} #The content of the master box can be inherited
The content of the child template
{%endblock Name% }
{% extends '1.html' %}{# inheritance#}
{# {{ block.super }} {# invalid#}
{#123321312312412412512 {# invalid#}
{ % block c1 %} {# Equivalent to the subclass definition __init__. If it is not defined, use the parent class. If it is defined, use your own #}
{{ block.super }}
{{ block.super }}
Hehe
Hehe
{% endblock c1 %}
3 Static file related
1 Hard-coded static files: /static/css/mycss.css“>
2 Use the static tag function:
–{%load static%} #load is a static.py file
#static return value will spell the path of the upload parameter
–{% static “css/mycss.css“%}
3 Use the get_static_prefix tag
–{%load static%}
#get_static_prefix The return value is: the address of the static file, equivalent to /static/
–{% get_static_prefix %}css/mycss.css
{# {% load static %} {# static dynamic acquisition method 1 #}
{# #}
{# {% load static %}{# method 2 #}
{# #}
4 Single table operation: Note (1. Distinguish QuerySet object & individual object & value and their methods 2. QuerySet object.query can view sql statement)
Review:
Note: Create orm object mapping relationship
1.settings configuration database
2.import pymysql
# Because django links to the mysql database by default, the MySQLdb module is used. After python3.0, it does not Support MySQLdb, you need to replace MySQLdb with pymysql
pymysql.install_as_MySQLdb()
3.models create table
4.-Data migration command:
-python3 manage.py makemigrations — >Just make a record of the changes and record the file in the app’s migrations
-python3 manage.py migrate —->Submit the changes to the database
–python3 manage.py showmigrations —-> Check which one has not been submitted to the database
-Query api
Contains objects that match the given filtering conditions
get(**kwargs): @ If there are more than one objects that match the filtering conditions or there are none, an error will be thrown.
exclude(**kwargs): Q@exclude
order_by(*field): Q@sort(‘-id’)descending order
reverse(): Q@ Reverse sorting of query results
count(): Value@returns the number of objects matching the query (QuerySet) in the database.
first (): A@<<1 record
Last ():@Return to the last record
exist (): value@If queerySet contains data, Return True, otherwise return False
values(*field): Q@Returns a ValueQuerySet – a special QuerySet. What you get after running is not a series of
model instantiated objects, but An iterable dictionary sequence
values_list(*field): Q@It is very similar to values(). It returns a sequence of tuples, and values returns a sequence of dictionaries.
distinct():
Book.objects.filter(price__lt=100)
Book.objects.filter(price__gte=100)
Book.objects.filter(price__lte=100)
Book.objects.filter(price__range=[ 100,200])
Book.objects.filter(title__cOntains=”python”)
Book.objects.filter(title__icOntains=”python”)
Book.objects.filter(title__startswith=”py”)
Book.objects.filter(pub_date__year=2012)