常用命令
django-admin startproject your_project_name
django-admin startapp your_app_name
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py changepassword username
python manage.py runserver
python manage.py runserver 8080
python manage.py runserver 127.0.0.1:9000
python manage.py flush
python manage.py shell
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
optimizemigration
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
修改时区 语言
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
静态文件目录 文件上传路径
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
连接SQL数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blogs',
'USER': 'root',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
配置模板路径
TEMPLATE_DIRS = (
os.path.join(BASE_DIR,'templates'),
)
注册APP
INSTALLED_APPS = [
'blog.apps.BlogConfig',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
设置打印日志到屏幕
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level': 'DEBUG',
},
}
}
在浏览器里访问自己上传的文件,在urls.py中:
from django.views.static import serve
from django.conf import settings
urlpatterns = [
...
re_path('^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]
actions_on_top=True
actions_on_bottom=False
admin.site.site_header = '吴腾的管理后台'
admin.site.site_title = '吴腾'
all()
filter(**kwargs)
get(**kwargs)
exclude(**kwargs)
values(*field)
values_list(*field)
order_by(*field)
reverse()
distinct()
count()
first()
last()
exists()
annotate()
dates()
datetimes()
none()
union()
intersection()
difference()
select_related()
prefetch_related()
extra()
defer()
only()
using()
select_for_update()
raw()