CBV:
定义:
class RegisterView(View):
def get(self, request):
return render(request, 'register.html')
def post(self, request):
username = request.POST.get('username')
password = request.POST.get('password')
repassword = request.POST.get('repassword')
if password == repassword:
user = User.objects.create(username=username, password=password)
if user:
return HttpResponse('注册成功')
return HttpResponse('注册失败')
def delete(self):
pass
def put(self):
pass
url设置:
path('register/', RegisterView.as_view(), name='register')
在用url传递数据时 path(r'user/<int:uid>/', UserView.as_view(), name='user')
方法中的变量也必须和url中声明的一样都是uid
def get(self, request, uid):
user = User.objects.get(pk=uid)