db.html(模板)

<!DOCTYPE html><html lang="ja">
<head>
<title>{{context.title}}</title>
<link rel="icon" href="/static/pic/1.jpg" type="image/x-icon" />
<link rel="shortcut icon" href="/static/pic/1.jpg" type="image/x-icon" />
<script>

</script>
<style type="text/css">

<!--
.box{
float: left;
color:black;
font-size:12px;
text-align:leftwards;
}
-->
</style>
</head>
<body>
{% if context.person.1 < 18 %}
<h3 align="center">时间:{{context.time | date:"Y-m-d H:i:s" }}</h3>
{% else %}
<h3 align="center">时间:{{context.time | date:"Y-m-d H:i:s" }}</h3>
{% endif %}
<div>
{% if context.member_list %}
{% for li in context.member_list %}
<div style="float:left;width:22.5%;">
{% load static %}
<div class="box" >
<video muted width="400" height="270" controls preload="none">
<source src="/static/{{li.address}}" type="video/mp4">
</video>
</div>
<div class="box" >
<p>
名前:{{li.name}}
<br>
年目:{{li.age}}
<br>
メール:{{li.address}}
<br>
時間:{{li.inttime}}
<br>
地点:{{li.area}}
<br>
</p>
</div>
</div>
{% empty %}
<li>no!</li>
{% endfor %}
{% endif %}
</div>
{% load mydigit %}
<br>
<br>
<p align="right">数量:{{context.num | mydigit:4}} </p>
</body>
</html>

models.py

from __future__ import unicode_literals
from django.db import models


class Member(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField( max_length=40) # Field name made lowercase.
age = models.IntegerField() # Field name made lowercase.
address = models.CharField(max_length=100) # Field name made lowercase.
inttime = models.DateTimeField()
area = models.CharField(max_length=40)

class Meta:
managed = False
db_table = 'member'

views.py#向数据库member里进行增删改查操作

def data(request):
template=loader.get_template("db.html")
pic_root="J:/flod/pic_image/FC-2/fc2/vedio/"
member_data=["相澤南","波多野ゆい","あやみ旬果","小沢まりあ","鈴村愛理","蒼い空","明日花キラ","蒼い空","七海奈々","葵司","天海翼","asuka","......"]
root = os.listdir(pic_root)
member_list=[]

#增添
#在数据库表批量加入数据
#for i in range(len(member_data)):
#member_list.append( [member_data[i],random.randint(10,45),root[i],"2020-09-04","Japan"])
#db=Member(name=member_data[i],age=21,address=str(root[i]),inttime="2020-09-04",area="Japan")
#db.save()

#查找
#获取数据库表所有信息
member_list=Member.objects.all()
#信息排序
#member_list=Member.objects.all().order_by("age")#升序member_list=Member.objects.order_by("age")
#member_list=Member.objects.all().order_by("-age")#降序
#过滤
#member_list=Member.objects.filter(age = 16) #age=16
#member_list=Member.objects.filter(age__gt = 16)#age>16
#member_list=Member.objects.filter(age__gte 16)#age>=16
#member_list=Member.objects.filter(name__contains = "张")#筛选姓张的人

#修改
#member_data=Member.objects.get(id=1)
#member_data.age=25
#member_data.area="中国"
#member_data.save()

#删除
#Member.objects.all().delete()
#Member.objects.filter(id=1)

context={"title":"日本語の世界に入ってくだい!","time":datetime.datetime.now(),"member_list":member_list,"num":len(member_list)}
request_context = RequestContext(request, context)
request_context.push(locals())
html=template.render(context=locals(), request=request)
return HttpResponse(html)

urls.py#访问网址

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^data/$", views.data),
]

setting.py#数据库配置

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'data.sqlite3'),
}
}

出现编码问题

views.py加入
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

Django sqlite3的实际应用_html


名前、年目、メール、時間、地点从数据库获取。

命令操作

cd /d root 切换目录

manage.py makemigrations appname 记录我们对appname/models.py的所有改动
manage.py makemigrations 记录我们对所有models.py的所有改动
manage.py migrate 同步数据库
manage.py inspectdb 检测数据库,自动生成 Django 模型
manage.py inspectdb > blog_/models.py 自动生成 Django 模型与本地对比,生成缺少的模型信息

遇到问题
models.py增加字段后,需要重新生成数据表时,删除migrations目录,以及数据库里有关的数据表, 重新执行命令生成。(有时执行命令报错,参照删除即可。但有时删除信息后,不会报错,重新生成却不包含定义的数据表。有时生成的包含数据表,原因不知。
方法:修改配置文件数据库名称,重新生成)