一、创建项目
django-admin startproject (project)
二、设计表节构
- 数据库名(vipurls)
- 表名(vipurl)
- 字段(urlname,createtime,isuseful)
三、数据库配置
- 使用数据库
mysql -uroot -p回车
create database vipurls - 配置数据库
'ENGINE': 'django.db.backends.mysql',
'NAME': "vipurls",
'USER':"root",
'PASSWORD':"123456",
'HOST':"localhost",
'PORT':"3306", -
init.py中的配置
import pymysql
pymysql.install_as_MySQLdb()
四、创建应用
python manage.py startapp vipmovie
五、配置模型类
- 在INSTALLED_APPS
- 创建类vipurl(数据表)
class Vipurl(models.Model):
urlname=models.CharField(max_length=20)
createtime=models.DateField()
isuseful=models.BooleanField
关联性表 lion=models.ForeignKey("Vipurl")
(default=False)def str(self):
return "%s【%s】"%(self.urlname,self.createtime)`
六、迁移文件
python manage.py makemigrations
python manage.py migrate
use vipurl->show tables->desc vipmovie_vipurl
七、测试数据操作
- 进入到python shell
python manage.py shell
- 引入包
from vipmovie.models import Vipurl
from django.utils import timezone
from datetime import * - 查询数据
Vipurl.objects.all()
vipurl.objects.get(2) - 添加数据
vipurl=Vipurl()
vipurl.urlname="http:\www.baidu.com"
vipurl.createtime=datetime(year=2018,month=1,day=1)
vipurl.isuseful=False
vipurl.save() - 修改数据
o=Vipurl.objects.get(pk=2)
o.isuseful=True
o.save() - 删除数据
d=Vipurl.objects.get(pk=)
d.delete() - 增加数据
net start mysql
关联性表(类名.关联的类名小写_set.all())
八、admin
from vipmovie.models import Vipurl
执行python manage.py createsuperuser
帐号:zlwy001
邮箱:948586883@qq.com
密码:happy2018
python manage.py runserver 6666默认端口8000
九、配置admin(汉化)
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
lass Myadmin(admin.ModelAdmin):
list_display=["pk","urlname","createtime","isuseful"]
list_filter=["createtime"]
search_fields=["createtime"]
list_per_page=10
# fields=[]
十、关联
class StudentInfo(admin.TabularInline):
model=Vipurl
extra=2
inlines=[StudentInfo]
十一、布尔值显示男和女,执行动作位置
@admin.register(Vipurl)
class Myadmin(admin.ModelAdmin):
# inlines=[StudentInfo]
def isuse(self):
if self.isuseful:
return "能用"
else:
return "不能用"
isuse.short_description="是否可用"
list_display=["pk","urlname","createtime",isuse] list_filter=["createtime"]
search_fields=["createtime"]
list_per_page=5
actions_on_top=False
actions_on_bottom=True
十二、视图
在vipmovie下创建urls.py
path(r'^',include('vipmovie.urls')),
from django.contrib import admin
from django.urls import path,include
from vipmovie import views
urlpatterns = [
path('admin/', admin.site.urls),
# path(r'',views.index),
path(r'',include("vipmovie.urls")),
# path('articles/<int:year>/<int:month>/<slug>/', views.article_detail),
]
app内的urls.py内的变化
from django.urls import path
from . import views
urlpatterns = [
path(r'',views.index),
]
from django.http import HttpResponse
def index(request):
return HttpResponse("我草")
十三、视图编写
- url编写
urlpatterns = [
path(r'',views.index),
path(r'<int:num>',views.page1),
] - 在项目中建立templates文件夹 ,视图下在创建哪个应用的视图
- 创建模板目录
'DIRS': [os.path.join(BASE_DIR,"templates")],
十四、模板写法
写母板
{%for vipurl in vipurls%}
<option value="{{vipurl.urlname}}">通道{{forloop.counter(counter0)}}</option>
{%endfor%}
十五、配置模板
写页面,写视图,配置url
十六、引入bootstrap
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,"static"),
)
创建static文件夹
引入<link rel="stylesheet" href="/static/css/bootstrap.css">
跨域表单:{% csrf_token %}