Django入门学习

1、安装django: import django
2、创建项目:
django-admin.py startproject HelloWorld
3、启动服务:python manage.py runserver 0.0.0.0:8000
4、打开,浏览器中输入http://127.0.0.1:8000/
5、更改端口:
manage.py help runserver
manage.py runserver 8081
HelloWorld 8082
6、数据库配置
下载 pip install pymysql
在_init.py_中配置添加:
import pymysql
pymysql.install_as_MySQLdb()

在setting.py中设置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 或者使用 mysql.connector.django 'NAME': 'test', 'USER': 'test', 'PASSWORD': 'test123', 'HOST':'localhost', 'PORT':'3306', } }

7、Django模板
1、目录底下创建 templates 目录并建立 hello.html文件
2、setting.py修改文件
...TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR+"/templates",], # 修改位置 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]


8、定义模型
django-admin.py startapp TestModel

manage.py makemigrations
$ python manage.py migrate # 创建表结构

$ python manage.py makemigrations TestModel # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate TestModel # 创建表结构

9、数据库操作

10、admin 操作
登录:http://127.0.0.1:8082/admin/login/?next=/admin/
建立超级用户: python manage.py createsuperuse

猜你喜欢

转载自www.cnblogs.com/LinxiHuang/p/9247968.html
今日推荐