Detailed explanation of Django model data types and template tags

One: Django model data type

Django follows the principle of Code Frist, that is: automatically generate database tables according to the classes defined in the code

Here is the data I have written in the model:

class Cart(models.Model):
    sort = models.CharField(max_length=20, null=False)
    name = models.CharField(max_length=20, null=False)
    price = models.FloatField(max_length=20)
    unit = models.CharField(max_length=20)
    count = models.IntegerField(null=False)
    money = models.FloatField(default=0, null=False)
    tot_money = models.FloatField(default=0, null=False)
    def __str__(self):
        return self.name
  def publish(self):
        self.save()

 Many data types are used here, the following is a summary of the common types:

models.CharField String field (must be parameter max_length)
models.BooleanField Boolean type, cannot be empty, Blank=True
models.DateField Date type date, for parameter: auto_now=True, this time will be updated every time; auto_now_add is only the first Created and added for the first time, and subsequent updates will not change models.DateTimeField
date type datetime is the same as DateField parameters regular expression on string models.FloatField float type models.IntegerField int models.BigIntegerField long int models.TextField string=longtext models.TimeField time HH:MM[:ss[.uuuuuu]] models.URLField string, address regex models.BinaryField binary models.ImageField image models.FilePathField file










 

Parameters are required in parentheses, so here is some summary of the parameters:

null=True (whether the fields in the database can be empty)
blank=True (whether null values ​​are allowed when adding data in django's Admin)
primary_key = False (primary key, after setting the primary key for AutoField, it will replace the original auto-increment id column )
auto_now and auto_now_add (auto_now is automatically created--whether it is added or modified, it is the time of the current operation; auto_now_add is automatically created--always the time of creation)
max_length (maximum length)
default (default value)
verbose_name (the field in Admin Display name)
name|db_column (field name in the database)
unique=True (duplication is not allowed)
db_index = True (database index)
editable=True (editable in Admin)
error_messages=None (error message)
auto_created=False ( Automatically created)
help_text (prompt help information in Admin)
upload-to (where to upload to, more used in conjunction with image and filepath)

 

2: Tag details (1) The form of the tag: {% tag %}

(2) The role of the label:

① Create some text on output
② Implement control flow by executing loops and some logic
③ Load some external information into the template

(3) Some commonly used built-in tags:

①block defines a block that can be overridden by descendant templates that inherit from him

{% block %}
//(Define block content)
{% endblock %}

②The comment template system will ignore everything inside the tag

{% comment %}
   //(content)
{% endcomment %}

 ③cycle uses the values ​​in the given string list in turn while looping

<tr class="{% cycle list%}">
   ...
</tr>

 ④For each value in the loop list, make the corresponding output

{% for variable in list/dict %}
   //(using variable)
{% endfor%}

 ⑤If variable can use and,or,not, but not allowed to use and and or together

{% if variable %}
  //(Content 1)
     {% elif %}
  //(Content 2)
{% endif %}

 ⑥ifequal judges whether two variables are equal

{% ifequal variable1 variable2 %}
     ...
{% endifequal%}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693973&siteId=291194637