-
接着上一课
- 修改app01\views.py:
from django.shortcuts import render,HttpResponse,redirect
USER_DICT = {
'1': {'name': 'root1', 'email': '[email protected]'},
'2': {'name': 'root2', 'email': '[email protected]'},
'3': {'name': 'root3', 'email': '[email protected]'},
'4': {'name': 'root4', 'email': '[email protected]'},
'5': {'name': 'root5', 'email': '[email protected]'},
}
def index(request,nid,uid):
# indexx
print(request.path_info)
# /asdfasdfasdf/13/
# reverse 能反转
from django.urls import reverse
# v = reverse('indexx', args=(90,88,))
v = reverse('indexx', kwargs={'nid':'1','uid':'99'})
print(v)
return render(request, 'index.html', {'user_dict': USER_DICT})
def detail(request, nid):
# print(nid, uid)
# return HttpResponse(nid)
detail_info = USER_DICT[nid]
return render(request, 'detail.html', {'detail_info': detail_info})
def login(request):
# 判断用户获取数据方式是GET,就返回什么数据
if request.method == "GET":
return render(request, 'login.html')
# 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
elif request.method == "POST":
# 数据表中执行 select * from user where username='x' and password='x'
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
from django.views import View
class Home(View):
# 调用父类中的dispatch(相当于助理,)
def dispatch(self, request, *args, **kwargs):
print('before')
result = super(Home,self).dispatch(request, *args, **kwargs)
print('after')
return result
def get(self,request):
print(request.method)
return render(request, 'home.html')
def post(self,request):
print(request.method, 'POST')
return render(request, 'home.html')
- app01\models.py
- app01\models.py 代码如下:
from django.db import models
# Create your models here.
class UserInfo(models.Model):
# Django自动创建id列,自增,主键
# 用户名列,字符串类型,指定长度
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
接着创建表:
-
先在settings中添加app01如下图:
-
然后在Terminal中运行输入
(python manage.py makemigrations)[创建表]
(python manage.py migrate)[生成数据库]
然后产生如下目录:
连接sb.sqlite3的方法如下:
-
打开Navicat,
-
复制sb.sqlite3的路径
-
把路径复制到如下:
-
然后能看到已生成的app01_userinfo 的表(所创建的表)