1.django两种关系
1.1FBV(function base views)
1.2CBV(class base views)
1.3name:多url路由关系进行命名,以后可以根据此名称生成自己想要的url
url(r'^index/',views.index,name='i1'),
url(r'^index/(\d+)/(\d+)/',views.index,name='i2'),
url(r'^index/(?P\d+)/(?P\d+)/',views.index,name='i3'),
def func(request,*args,**kwargs):
from django.urls import reverse
url1 = reverse('i1')
url2 = reverse('i2',args=(1,2,))
url3 = reverse('i3',kwargs={'pid':1,"nid":9})
xxx.html 模板语言
{% url 'i1' %}
{% url 'i2' 1 2 %}
{% url 'i3' pid=1 nid=9 %}
1.4 创建数据库(使用sqlite3)
PS C:\Users\11025\PycharmProjects\day19> python.exe .\manage.py makemigrations
PS C:\Users\11025\PycharmProjects\day19> python.exe .\manage.py migrate
1.5 orm数据库修改表结构
class UserInfo(models.Model):
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
email = models.CharField(max_length=60)
gender = models.CharField(max_length=60,null=True)
PS C:\Users\11025\PycharmProjects\day19> python.exe .\manage.py makemigrations
You are trying to add a non-nullable field 'email' to userinfo without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>> 'alex'
Migrations for 'app01':
app01\migrations\0002_auto_20180119_1356.py
- Add field email to userinfo
- Alter field password on userinfo
PS C:\Users\11025\PycharmProjects\day19> python.exe .\manage.py migrate
Operations to perform:
Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
Applying app01.0002_auto_20180119_1356... OK
PS C:\Users\11025\PycharmProjects\day19>
1.5 创建超级管理员账号
PS C:\Users\11025\PycharmProjects\day19> python.exe .\manage.py createsuperuser
Username (leave blank to use
Email address:
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Error: Your passwords didn
Password:
Password (again):
Superuser created successfully.
1.6 ORM创建数据库参数介绍
null 创建数据库是否为空
default 默认值
primary key 主键
db_column 列名修改
db_index 是否建立索引(非唯一),unique(建立唯一索引)
unique_for_date 只对时间做索引
unique_for_month 只对月份做索引
unique_for_year 只对年份做索引
auto_now 创建时,自动
auto_now_add 创建时,自动更新为当前时间
choices django admin显示下拉框,避免连表查询
blank django admin是否可以为空
verbose name django admin显示中文
editable 可否被编辑
help_text 提示信息
validators django admin