跟着视频学一学,记录一下。
照着步骤创建虚拟环境,安装Django等依赖包,创建项目:【Django学习】01 项目创建、结构及命令
> django-admin startproject pro_recruitment
> cd pro_recruitment
> python manage.py runserver # 运行项目,默认以0.0.0.0:8000
> python manage.py runserver 0.0.0.0:8000 # 也可指定端口
访问网页(127.0.0.1:8000或指定IP:8000),可以看到Django的初始页面:
项目运行之后,Django使用默认的SQLite数据库,会在项目的根目录下创建数据库文件db.sqlite3,
可在项目settings.py文件中指定sqlite3文件的路径或更改为其他的数据库引擎。
数据库访问层和Django是松耦合的:数据库的配置也可随时替换;同一套代码,既可以使用sqlite数据库,也可使用MySQL数据库或Oracle数据库。
访问链接:http://127.0.0.1:8000/admin
后台登录是需要账号及密码的,但现在尝试输入账号密码却会报错,报错原因是此时尚未迁移数据库以及未创建管理员账号。
先迁移一下数据库:
PS E:\05_Python\Django_Codes\pro_recruitment> python .\manage.py makemigrations
No changes detected
PS E:\05_Python\Django_Codes\pro_recruitment> python .\manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, 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 sessions.0001_initial... OK
PS E:\05_Python\Django_Codes\pro_recruitment>
后创建管理员账号:
PS E:\05_Python\Django_Codes\pro_recruitment> python .\manage.py createsuperuser
Username (leave blank to use 'asdfv'): admin
Email address: admin@123.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
账号创建完成,再重新运行项目后去后台管理界面上进行登录,此时就能正常进入:
settings.py是项目的配置文件,其中包含了语言设置、数据库设置、模板文件设置等等。
设置中文
LANGUAGE_CODE = 'zh-hans' # 中文
项目的创建与运行,没啥问题,掌握常用的命令即可;同时需要加强排错能力,在遇到报错时能快速定位并解决。