def login(request): message = "" if request.session.get('is_login', None): return redirect("/") if request.method == "POST": login_form = forms.UserForm(request.POST) message = "请检查填写的内容!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.User.objects.get(name=username) if not user.has_confirmed: message = "该用户还未通过邮件确认!" return render(request, 'login.html', {'message': message}) if user.password == hash_code(password): # 哈希值和数据库内的值进行比对 request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('index.html') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login.html', {'message': message}) login_form = forms.UserForm() return render(request, 'login.html', { 'message': message, 'login_form': login_form })
def login(request): # No duplicate enter if request.session.get('is_login', None): return redirect('/index/') if request.method == 'POST': login_form = forms.UserForm(request.POST) message = 'Please enter correct information!' if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.User.objects.get(name=username) if user.password == password: request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('/index/') else: message = 'password invalid!' except: message = "user doesn't exit!" return render(request, 'login/login.html', locals()) login_form = forms.UserForm() return render(request, 'login/login.html', locals())
def login(request): if request.session.get('is_login', None): return redirect("/index/") if request.method == "POST": login_form = forms.UserForm(request.POST) message = "请检查填写的内容!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.User.objects.get(name=username) if user.password == hash_code(password): # 哈希值和数据库内的值进行比对 request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name # messages = models.Message.objects.all() return redirect('/index') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'loginapp/login.html', locals()) login_form = forms.UserForm() return render(request, 'loginapp/login.html', locals())
def login(request): if request.session.get('is_login', None): return redirect('/index/') if request.method == "POST": login_form = forms.UserForm(request.POST) message = '请检查填写的内容!' if login_form.is_valid(): username = login_form.cleaned_data.get('username') password = login_form.cleaned_data.get('password') try: user = models.User.objects.get(name=username) except: message = '用户不存在!' return render(request, 'login/login.html', locals()) if user.password == hash_code(password): request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('/index/') else: message = '密码不正确!' return render(request, 'login/login.html', locals()) else: return render(request, 'login/login.html', locals()) login_form = forms.UserForm() return render(request, 'login/login.html', locals())
def get(self, request): code = request.GET.get('code', None) try: confirm = models.ConfirmString.objects.get(code=code) except: message = '无效的确认请求!' return render(request, 'login/confirm.html', {'message': message}) c_time = confirm.c_time now = datetime.datetime.now() if now > c_time + datetime.timedelta(settings.CONFIRM_DAYS): confirm.user.delete() message = '您的邮件已经过期!请重新注册!' return render(request, 'login/confirm.html', {'message': message}) else: confirm.user.has_confirmed = True confirm.user.save() confirm.delete() message = '感谢确认,请使用账户登录!' login_form = forms.UserForm(request.POST) return render(request, 'login/login.html', { 'message': message, 'login_form': login_form })
def post(self, request): login_form = forms.UserForm(request.POST) message = "请检查填写的内容!" if login_form.is_valid(): # username = login_form.cleaned_data['username'] # password = login_form.cleaned_data['password'] username = request.POST.get('username', '') password = request.POST.get('password', '') try: user = models.User.objects.get(name=username) if not user.has_confirmed: message = "该用户还未通过邮件确认!" return render(request, 'login/login.html', { 'message': message, 'login_form': login_form }) if user.password == hash_code(password): # 哈希值和数据库内的值进行比对 # 记录会话状态 request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('/index/') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login/login.html', { 'message': message, 'login_form': login_form })
def login(request): if request.method == "POST": login_form = forms.UserForm(request.POST) message = "请检查填写的内容!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.User.objects.get(name=username) if user.password == password: return redirect('/index/') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login.html', locals()) login_form = forms.UserForm() return render(request, 'login.html', locals())
def login(request): # 判断用户是否已经登录 if request.session.get('is_login', None): # 这里get(is_login)的默认值是None # 当会话中间件启用后,传递给视图request参数的HttpRequest对象将包含一个session属性 # 这个属性的值是一个类似字典的对象。 return redirect("/index/") # 判断用户的请求方法是否为"POST" if request.method == "POST": # login.html -- form表单的属性 -- method = 'POST' # (以下被注释的代码是为在html手动写的表单服务的) # username = request.POST.get('username', None) # password = request.POST.get('password', None) login_form = forms.UserForm(request.POST) message = "所有字段都必须填写!" # 确保用户名和密码都不为空 if login_form.is_valid(): # cleaned_data是一个字典类型数据: 根据key获得字典中的具体值 username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] # 用户名字符合法性验证 # 密码长度验证 # 更多的其它验证..... try: user = models.User.objects.get(name=username) # 如果密码正确,则把key为is_login的值设为True # 并往session字典内写入用户状态和数据 if user.password == password: request.session['is_login'] = True request.session['user_id'] = user.id # 每一个表都默认有一个id吗 ?? request.session[ 'user_name'] = user.name # user.name是 user表里的name属性的值 return redirect('/index/') else: message = "密码不正确" except: message = "用户名不存在" # 如果验证不通过,则返回一个包含先前数据的表单给前端页面,方便用户修改 return render(request, 'login/login.html', locals()) # locals(): 返回当前所有的本地变量字典 # message为什么会是字典?? # 对于非POST方法发送数据时,比如GET方法请求页面,返回空的表单,让用户可以填入数据 login_form = forms.UserForm() return render(request, 'login/login.html', locals())
def register(request): isRegistered = False if request.method == "POST": user_form = forms.UserForm(data = request.POST) profile_info_form = forms.ProfileInfoForm(data = request.POST) if user_form.is_valid() and profile_info_form.is_valid(): user = user_form.save() # save hash for password user.set_password(user.password) user.save() profile = profile_info_form.save(commit = False) profile.user = user if 'profile_pik' in request.FILES: profile.profile_pik = request.FILES['profile_pik'] profile.save() # when everything is done then isRegistered = True return index(request) else : print(user_form.errors, profile_info_form.errors) else : user_form = forms.UserForm() profile_info_form = forms.ProfileInfoForm() return render(request, 'app_2/register.html', { 'user_form': user_form, 'profile_info_form': profile_info_form, 'registered': isRegistered })
def get(self, request): login_form = forms.UserForm(request.POST) return render(request, 'login/login.html', {'login_form': login_form})