C:\Users\Administrator\Desktop\p4django\day01>python manage.py makemigrations
No changes detected
from django.db import models
#Create your models here.
class Game(models.Model):
name = models.CharField(max_length=20,verbose_name=‘商品名’)
price = models.IntegerField(verbose_name=‘价格’)
num = models.IntegerField(verbose_name=‘数量’)
1.创建超级用户:python manage.py createsuperuser
2.在子应用的admin.py文件中添加代码,注册表,不然admin控制台没办法管理这个表
from django.contrib import admin
from myapp.models import Game
#Register your models here.
admin.site.register(Game)
导包
from django.shortcuts import render,redirect
from django.views import View
from django.http import HttpResponse
from myapp.models import User
class IndexView(View):
# 返回注册页面
def get(self,request):
return render(request,‘index.html’)
# 提交账号密码并保存到数据库
def post(self,request):
# 获取表单中提交的账号密码
user2 = request.POST.get(‘user’)
pwd2 = request.POST.get(‘pwd’)
# 保存账号密码到数据库中,
#可能要出现异常 进行异常处理
try:
# 新增数据
User.objects.create(user=user2,pwd=pwd2)
except Exception as e:
print(e)
return HttpResponse(‘账号密码注册失败’)
# 重定向到登录页面
return redirect('/login/')
登录视图
class LoginView(View):
# 返回登录页面
def get(self,request):
return render(request,‘login.html’)
# 提交账号密码,验证账号密码是否存在
def post(self,request):
user2 = request.POST.get(‘user’)
pwd2 = request.POST.get(‘pwd’)
# 查看数据库中是否存在账号为user1的数据
try:
# 查询用户名 与 表单中输入的用户名 一致的数据
user_data = User.objects.get(user=user2)
except Exception as e:
print(e)
return HttpResponse(‘账号不存在’)
# 用户存在,判断密码是否一致
if pwd2 == user_data.pwd:
# 重定向到主页面
return redirect(‘/zhuye/’)
else:
return HttpResponse(‘密码错误’)
class ZhuyeView(View):
# 获取主页面
def get(self,request):
return render(request,‘zhuye.html’)
class ZhuyeView2(View):
def get(self,request,id,name):
return HttpResponse(f’匹配到的id为:{id}–{name}')
class ZhuyeView3(View):
def get(self,request,x,y):
return HttpResponse(f’x为{x},y为:{y}')
from django.urls import path,re_path
from myapp import views
urlpatterns = [
# path(‘admin/’, admin.site.urls),
path(‘index/’,views.IndexView.as_view()),
path(‘login/’,views.LoginView.as_view()),
path(‘zhuye/’,views.ZhuyeView.as_view()),
re_path(r’1{5}/$',views.ZhuyeView.as_view()),
# 动态路由
path('zhuye2//',views.ZhuyeView.as_view()),
re_path(r'(?P\d+)/(?P[a-zA-Z]+)',views.ZhuyeView3.as_view()),
]
a-z ↩︎
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)