配置數(shù)據(jù)庫
項目settings.py文件中,對sqlite3配置。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
創(chuàng)建數(shù)據(jù)庫
- 首先,在models模型中創(chuàng)建數(shù)據(jù),其格式如下:
定義一個類,通過類操作
from django.db import models
class index(models.Model): #index是Class的名稱與views的相同,也是創(chuàng)建的表名
u = models.CharField(max_length=64) # 創(chuàng)建Char 類型字段,字段長度為64,根據(jù)自己實際需求進(jìn)行更改。
s = models.CharField(max_length=64)
e = models.CharField(max_length=64)
- 在命令行中輸入以下命令
python manage.py makemigrations
需要指向項目目錄中
python manage.py makemigrations
以下為返回內(nèi)容
index\migrations\0001_initial.py
- Create model index
- 接著輸入
python manage.py migrate
python manage.py migrate
以下返回結(jié)果為:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, index, 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 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 index.0001_initial... OK
Applying sessions.0001_initial... OK
查看數(shù)據(jù)庫
通過Pycharm 工具進(jìn)行對 db.sqlite3 進(jìn)行查看,如果無法查看,通過右鍵屬性安裝其插件后,便可查看。
里面會有很多關(guān)于Django創(chuàng)建的表,暫時無需理會,找到你項目表,進(jìn)入查看,是否是自己創(chuàng)建的即可。
views層次更改
見代碼注釋段。
from django.shortcuts import render
from index import models # 引入創(chuàng)建的模型
def index(request):
if request.method == "POST":
username = request.POST.get("username",None)
sex = request.POST.get("sex",None)
email = request.POST.get("email",None)
models.index.objects.create( #數(shù)據(jù)庫插入語句
u = username, #設(shè)定字段與傳入值進(jìn)行對應(yīng)(將會什么內(nèi)容將會保存在什么字段下。)。
s = sex,
e = email,
)
user_list = models.index.objects.all() #將數(shù)據(jù)全部展示至html中。
return render(request,"index.html",{"user_list":user_list})
html 代碼
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="/index/" method="POST">
<lable>姓名</lable><input type="text" name="username">
<lable>性別</lable><input type="text" name="sex">
<lable>郵箱</lable><input type="text" name="email">
<input type="submit" value="提交">
</form>
<h1 id="h1">test staticfiles_dirs</h1>
<table border="1px">
<tr>
<td>姓名</td>
<td>性別</td>
<td>郵箱</td>
</tr>
{% for i in user_list %}
<tr>
<td>{{ i.u }}</td>
<td>{{ i.s }}</td>
<td>{{ i.e }}</td>
</tr>
{% endfor %}
</table>
<script src="/abc/jquery-2.2.4.min.js"></script>
<script>
$("#h1").css("color","red")
</script>
</body>
</html>