Django框架创建
Posted wust_ouyangli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django框架创建相关的知识,希望对你有一定的参考价值。
python搭建虚拟环境
Virtualenv方式
安装virtualenv
pip install virtualenv
进入自己想要创建虚拟环境的目录(以E盘MyDjangoProject为例)
cd E:/MyDjangoProject
创建虚拟环境(以创建Envs为例)
virtualenv Envs # 默认
virtualenv -p /user/administrator/python35 Envs # 指定python解释器
启动/退出 虚拟环境
cd Envs/Scripts
activate.bat # 启动
deactivate.bat # 退出
删除虚拟环境(实际上是删除目录)
rd /s Envs
也可以用mkvirtualenv(可能会默认创建在C盘某个地方)
mkvirtualenv Envs # 创建
rmvirtualenv Envs # 删除
workon Envs # 启动虚拟环境
deactivate # 退出虚拟环境
pip list # 显示虚拟环境下所有的包
安装django
pip install django # pip安装(要联网)
创建项目
django-admin startproject test2
cd test2
Django项目结构图
创建应用(以booktest为例)
python manage.py startapp booktest
应用结构图
创建模型
# booktest/models.py from django.db import models #数据库模块 # Create your models here. class BookInfo(models.Model): #继承Model类 title = models.CharField(max_length=20) #字符串类型 pub_date = models.DateField() #日期类型 def __str__(self): return self.title class HeroInfo(models.Model): name = models.CharField(max_length=10) content = models.CharField(max_length=100) gender = models.BooleanField(default=True) book = models.ForeignKey(BookInfo, on_delete=models.CASCADE) #外键 def __str__(self): return self.name
settings.py添加app
生成迁移文件
python manage.py makemigrations
迁移在数据库中创建表
python manage.py migrate
附: 用shell操作(相当于用cmd运行代码)
python manage.py shell
数据操作命令
# 创建 book = BookInfo() book.*** = *** book.save() # 修改 book = BookInfo.objects.get(id=***) book.*** = *** book.save() # 删除 book.delete() # 查询 BookInfo.objects.all() BOokInfo.objects.get(id=***)
创建超级管理员
python manage.py createsuperuser
运行
python manage.py runserver
在浏览器地址栏输入 127.0.0.1:8000/admin
改变语言和时区
#LANGUAGE_CODE = \'en-us\' LANGUAGE_CODE = \'zh-Hans\' #TIME_ZONE = \'UTC\' TIME_ZONE = \'Asia/Shanghai\'
admin注册模型类
# booktest/admin.py
class BookInfoAdmin(admin.ModelAdmin): list_display = (\'id\', \'title\', \'pub_date\') class HeroInfoAdmin(admin.ModelAdmin): list_display = (\'id\', \'name\', \'content\', \'gender\', \'book\') admin.site.register(BookInfo, BookInfoAdmin) admin.site.register(HeroInfo, HeroInfoAdmin)
views创建视图类
from django.shortcuts import render from django.http import HttpResponse from booktest.models import * # Create your views here. def index(request): # return HttpResponse(\'<h1>hello world</h1>\') list = BookInfo.objects.all() context = {\'booklist\':list} return render(request, \'booktest/index.html\', context=context) def detail(request, id): list = BookInfo.objects.get(id=id).heroinfo_set.all() context = {\'herolist\':list} return render(request, \'booktest/detail.html\', context=context)
test2/urls.py配置urlpatterns
from django.contrib import admin from django.urls import path, include # from django.conf.urls import url, include urlpatterns = ( # url(r\'^admin/\', include(admin.site.urls)) 旧版本 path(\'admin/\', admin.site.urls), # 新版本 # url(r\'^\', include(\'booktest.urls\')) path(\'\', include(\'booktest.urls\')), )
booktest/urls.py配置urlpatterns
from booktest.views import * from django.urls import path, re_path urlpatterns = [ # url(\'^index/$\', index) path(\'index/\', index), # url(\'^(\\d+)$\', detail) re_path(\'(?P<id>\\d+)/\', detail), ]
配置模版目录
templates目录下创建html文件
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {#<h3>{{ content }}</h3>#} <ul> {% for book in booklist %} <li><a href="/{{ book.id }}/">{{ book.title}}</a></li> {% endfor %} </ul> </body> </html>
detail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>英雄列表</title> </head> <body> <ul> {% for hero in herolist %} <li>{{ hero.name }}</li> {% endfor %} </ul> </body> </html>
以上是关于Django框架创建的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Django Summernote 中显示编程片段的代码块?
一 Django框架介绍——用pycharm创建Django项目
Django基于PythonWeb的Django框架设计实现天天生鲜系统-3模型创建
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段