Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.). (django) $
Creates a Django project directory structure for the given project name in the current directory or optionally in the given directory.
positional arguments: name Name of the application or project. directory Optional destination directory
optional arguments: -h, --help show this help message and exit --template TEMPLATE The path or URL to load the template from. --extension EXTENSIONS, -e EXTENSIONS The file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times. --name FILES, -n FILES The file name(s) to render. Separate multiple file names with commas, or use -n multiple times. --version show program's version number and exit -v {0,1,2,3}, --verbosity {0,1,2,3} Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output --settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Raise on CommandError exceptions --no-color Don't colorize the command output. --force-color Force colorization of the command output.
Creates a Django app directory structure for the given app name in the current directory or optionally in the given directory.
positional arguments: name Name of the application or project. directory Optional destination directory
optional arguments: -h, --help show this help message and exit --template TEMPLATE The path or URL to load the template from. --extension EXTENSIONS, -e EXTENSIONS The file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times. --name FILES, -n FILES The file name(s) to render. Separate multiple file names with commas, or use -n multiple times. --version show program's version number and exit -v {0,1,2,3}, --verbosity {0,1,2,3} Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output --settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Raise on CommandError exceptions --no-color Don't colorize the command output. --force-color Force colorization of the command output. (django) $
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
In your settings file, define STATIC_URL, for example: STATIC_URL = '/static/'
In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE. {% load static %}
class UserInfo(models.Model): # 这个就会和数据库中的表对应上 name = models.CharField(max_length=30) passwrod = models.CharField(max_length=16) age = models.IntegerField()
django定义了好几种 xxxxField。当然有额可以自己继承一个Field类自己定义一个
class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField[_ST, _GT]): ... class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, SmallIntegerField[_ST, _GT]): ... class PositiveBigIntegerField(PositiveIntegerRelDbTypeMixin, BigIntegerField[_ST, _GT]): ... class SmallIntegerField(IntegerField[_ST, _GT]): ... class BigIntegerField(IntegerField[_ST, _GT]): ...
class FloatField(Field[_ST, _GT]): class DecimalField(Field[_ST, _GT]):
class CharField(Field[_ST, _GT]): class CommaSeparatedIntegerField(CharField[_ST, _GT]): ... class SlugField(CharField[_ST, _GT]): class EmailField(CharField[_ST, _GT]): ... class URLField(CharField[_ST, _GT]): class TextField(Field[_ST, _GT]): class BooleanField(Field[_ST, _GT]): class NullBooleanField(BooleanField[_ST, _GT]): class IPAddressField(Field[_ST, _GT]): class GenericIPAddressField(Field[_ST, _GT]): class DateField(DateTimeCheckMixin, Field[_ST, _GT]): class TimeField(DateTimeCheckMixin, Field[_ST, _GT]): class DateTimeField(DateField[_ST, _GT]): class UUIDField(Field[_ST, _GT]): class FilePathField(Field[_ST, _GT]): class BinaryField(Field[_ST, _GT]): class DurationField(Field[_ST, _GT]): class AutoField(AutoFieldMixin, IntegerField[_ST, _GT], metaclass=AutoFieldMeta): class BigAutoField(AutoFieldMixin, BigIntegerField[_ST, _GT]): ... class SmallAutoField(AutoFieldMixin, SmallIntegerField[_ST, _GT]): ...
(django) $ python manage.py makemigrations 先执行这个命令 Migrations for 'myapp01': myapp01/migrations/0001_initial.py - Create model UserInfo
(django) $ python manage.py migrate 再执行这个命令 Operations to perform: Apply all migrations: admin, auth, contenttypes, myapp01, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying myapp01.0001_initial... OK Applying sessions.0001_initial... OK
class UserInfo(models.Model): name = models.CharField(max_length=30) passwrod = models.CharField(max_length=16) age = models.IntegerField()
class OtherInfo(models.Model): char = models.CharField(max_length=300) slug =models.SlugField() email =models.EmailField() url =models.URLField() text =models.TextField() boolean =models.BooleanField() null =models.BooleanField(null=True) generic_ip =models.GenericIPAddressField() date =models.DateField() time =models.TimeField() date_time =models.DateTimeField() uuid =models.UUIDField() file_path=models.FilePathField() binary =models.BinaryField() duration =models.DurationField()
Field Type Null Key Default Extra id bigint(20) NO PRI NULL auto_increment char varchar(300) NO NULL slug varchar(50) NO MUL NULL email varchar(254) NO NULL url varchar(200) NO NULL text longtext NO NULL boolean tinyint(1) NO NULL null tinyint(1) YES NULL generic_ip char(39) NO NULL date date NO NULL time time(6) NO NULL date_time datetime(6) NO NULL uuid char(32) NO NULL file_path varchar(100) NO NULL binary longblob NO NULL duration bigint(20) NO NULL
如果 duration =models.DurationField(db_column="my_col_duration") 数据库表的列名就会对应成下面这个样子 my_col_duration bigint(20) NO NULL
class AutoInfo1(models.Model): auto = models.AutoField( primary_key=True) Field Type Null Key Default Extra auto int(11) NO PRI NULL auto_increment
默认的django 添加的那个住建就是 BigAutoField 类型的。 在 apps.py 文件中 有配置 default_auto_field = 'django.db.models.BigAutoField' class AutoInfo2(models.Model): big_auto =models.BigAutoField( primary_key=True) Field Type Null Key Default Extra big_auto bigint(20) NO PRI NULL auto_increment
class AutoInfo3(models.Model): small_auto =models.SmallAutoField( primary_key=True) Field Type Null Key Default Extra small_auto smallint(6) NO PRI NULL auto_increment
数据库表增删改查
新建一行数据,那就是 调用 对应 model 类对象,然后 create 一个实例就可以了
1 2 3 4 5 6 7 8 9 10 11
class UserInfo(models.Model): name = models.CharField(max_length=30) password = models.CharField(max_length=16) age = models.IntegerField()