-
接著上一課
- 修改app01\views.py:
from django.shortcuts import render,HttpResponse,redirect
USER_DICT = {
'1': {'name': 'root1', 'email': 'root@live.com'},
'2': {'name': 'root2', 'email': 'root@live.com'},
'3': {'name': 'root3', 'email': 'root@live.com'},
'4': {'name': 'root4', 'email': 'root@live.com'},
'5': {'name': 'root5', 'email': 'root@live.com'},
}
def index(request,nid,uid):
# indexx
print(request.path_info)
# /asdfasdfasdf/13/
# reverse 能反轉(zhuǎn)
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):
# 判斷用戶獲取數(shù)據(jù)方式是GET,就返回什么數(shù)據(jù)
if request.method == "GET":
return render(request, 'login.html')
# 判斷用戶獲取數(shù)據(jù)方式是POST,就判斷用戶提交的數(shù)據(jù)是否正確
elif request.method == "POST":
# 數(shù)據(jù)表中執(zhí)行 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):
# 調(diào)用父類中的dispatch(相當(dāng)于助理,)
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')
image.png
- app01\models.py
image.png
- app01\models.py 代碼如下:
from django.db import models
# Create your models here.
class UserInfo(models.Model):
# Django自動(dòng)創(chuàng)建id列,自增,主鍵
# 用戶名列,字符串類型,指定長(zhǎng)度
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
接著創(chuàng)建表:
-
先在settings中添加app01如下圖:
image.png -
然后在Terminal中運(yùn)行輸入
(python manage.py makemigrations)[創(chuàng)建表]
(python manage.py migrate)[生成數(shù)據(jù)庫]
image.png 然后產(chǎn)生如下目錄:
image.png
連接sb.sqlite3的方法如下:
-
打開Navicat,
image.png -
復(fù)制sb.sqlite3的路徑
image.png -
把路徑復(fù)制到如下:
image.png
-
然后能看到已生成的app01_userinfo 的表(所創(chuàng)建的表)
image.png