菜鸟教程django
pycharm安装django包
file ---> settings ----> project 解释器 ---> 点击+ ----> 输入Django ---> install(如果选择版本时,选择 CheckBox ,选择指定版本) |
命令行创建项目提示包错误
C:\Users\Dell\Desktop\python\venv\Lib\site-packages\django\bin>python django-admin.py startproject djangotest Traceback (most recent call last): File "django-admin.py", line 2, in <module> from django.core import management ModuleNotFoundError: No module named 'django' |
安装django包
C:\Users\Dell\Desktop\python\venv\Lib\site-packages\django\bin>pip install Django==3.0.9 |
查看python安装路径
>>> import sys >>> sys.path |
退出python命令行模式
Use exit() or Ctrl-Z plus Return to exit |
添加环境变量PATH
C:\Users\Dell\AppData\Local\Programs\Python\Python36\Lib\site-packages\django\bin |
注意项目名可以有下划线_,但是不可以有中划线-。
(venv) C:\Users\Dell\Desktop\python>django-admin startproject django-test CommandError: 'django-test' is not a valid project name. Please make sure the name is a valid identifier. |
成功创建项目后的目录
目录介绍
django_test: 项目的容器。
manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
django_test/__init__.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
django_test/asgi.py: 一个 ASGI 兼容的 Web 服务器的入口,以便运行你的项目。
django_test/settings.py: 该 Django 项目的设置/配置。
django_test/urls.py: 该 Django 项目的 URL 声明; 一份由 Django 驱动的网站”目录”。
django_test/wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
启动web程序
(venv) C:\Users\Dell\Desktop\python\django_test>python manage.py runserver Watching for file changes with StatReloader Performing system checks... |
也可以指定端口manage.py runserver 80
视图和URL配置
修改views.py
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world!") |
urls.py
from django.conf.urls import url from .import views urlpatterns = [ url(r'^$', views.hello), ] |
Django模板
创建django_test\templates\hello.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>{{ hello }}</h1> </body> </html> |
setting.py中的TEMPLATES的DIRS修改 ‘DIRS’: [BASE_DIR+”\\django_test\\templates”],,表明了模板文件所在路径,注意windows和linux下路径区别,可以在setting.py打印出BASE_DIR输出项目路径。
提示
ModuleNotFoundError: No module named ‘pymysql’,从pycharm命令行里又重新执行了一次就可以了。
django_test\django_test\settings.py配置DATABASES
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'NAME': 'weixinmofang', # 数据库名称 'HOST': '49.233.138.90', # 数据库地址,本机 ip 地址 127.0.0.1 'PORT': 3306, # 端口 'USER': 'weixinmofang', # 数据库用户名 'PASSWORD': 'pzabdwzWdRetydYX', # 数据库密码 'OPTIONS':{'init_command':'SET sql_mode="STRICT_TRANS_TABLES"'} #设置数据库为INNODB,为第三方数据库登录用 } } |
django_test\django_test\__init__.py(setting.py同级目录下)配置
import pymysql pymysql.install_as_MySQLdb() |
定义模型
django-admin.py startapp TestModel
django_test\TestModel\models.py配置,类名代表了数据库表名,且继承了models.Model
# models.py
from django.db import models class Test(models.Model): name = models.CharField(max_length=20) |
django_test\django_test\settings.py配置TestModel
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'TestModel' #添加此项 ] |
命令行执行以下三个命令创建数据表
python manage.py migrate
(venv) C:\Users\Dell\Desktop\python\django_test>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 sessions.0001_initial... OK |
python manage.py migrate报如下错误:
raise MigrationSchemaMissing(“Unable to create the django_migrations table (%s)” % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, “You have an error in your SQL syntax; check the manual t
hat corresponds to your MySQL server version for the right syntax to use near ‘(6) NOT NULL)’ at line 1”))
Django2.1不再支持MySQL5.5,必须5.6版本以上
python manage.py makemigrations TestModel
(venv) C:\Users\Dell\Desktop\python\django_test>python manage.py makemigrations TestModel
Migrations for ‘TestModel’:
TestModel\migrations\0001_initial.py
– Create model Test
python manage.py migrate TestModel
(venv) C:\Users\Dell\Desktop\python\django_test>python manage.py migrate TestModel
Operations to perform:
Apply all migrations: TestModel
Running migrations:
Applying TestModel.0001_initial… OK
表名组成结构为:应用名_类名(如:TestModel_test)