视图函数views.py中
from django.shortcuts import render, redirect from django.http import HttpResponse, JsonResponse from django.core.urlresolvers import reverse # 反响解析 from django.views.generic import View # 导入类试图 import re from user.models import * # Create your views here. class RegisterView(View): '''注册''' def get(self, request): '''显示注册页面''' return render(request, 'register.html') def post(self, request): '''进行注册处理''' # 接受数据 username = request.POST.get('user_name') password = request.POST.get('pwd') email = request.POST.get('email') allow = request.POST.get('allow') # 校验 # 数据完整度 if not all([username, password, email]): return render(request, 'register.html', {'errmsg': '数据不完整'}) # 邮箱验证 if not re.match(r'^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', email): return render(request, 'register.html', {'errmsg': '邮箱格式不正确'}) # 校验协议 if allow != 'on': return render(request, 'register.html', {'errmsg': '请同意协议'}) # 校验用户是否存在 try: user = User.objects.get(username=username) except Exception as e: user = None if user: # 用户已存在 return render(request, 'register.html', {'errmsg': '用户名已存在'}) # 进行用户注册 # create_user() 注册用户 user = User.objects.create_user(username, email, password) user.is_active = 0 # 0为未激活状态 user.save() return redirect(reverse('goods:index'))
路由中一样也要重新配置
urls.py中
from django.conf.urls import url from user.views import RegisterView # 导入视图类 urlpatterns = [ # url(r'^register$', views.register, name='register'), # url(r'^register_handle$', views.register_handle, name='register_handle'), # url(r'^login$', views.login, name='login'), url(r'register', RegisterView.as_view(), name='register'), ]