def registration(request): years = [y for y in range(date.today().year-80,date.today().year-10)] if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): new_user = form.save() new_user = authenticate(email=form.cleaned_data['email'], password=form.cleaned_data['password1']) login(request, new_user) current_question = Question.objects.all().order_by('?')[:1].get() return redirect(current_question) else: form = UserCreationForm() return render(request, "registration.html", {'form': form, 'years':years})
def user_register(request): if request.method == 'POST': # 如果有post提交的动作,就将post中的数据赋值给uf,供该函数使用 uf = UserCreationForm(request.POST) if uf.is_valid(): # 读取表单值 username = uf.cleaned_data['a_username'] tel = request.POST["tel"] a_password = request.POST["a_password"] right = None try: right = request.POST["type"] except Exception as e: pass if right is None: right = 'customer' # 注册用户表 user = MyBaseUser.objects.create_user(username=username, password=a_password, right=right) # userID = MyBaseUser.objects.get(username=username) if right == 'customer': Customer.objects.create(CID=user, name=username, tel=tel) else: Staff.objects.create(StaNO=user, StaName=username, tel=tel, Position=right) auth.login(request, user) request.__setattr__('user', user) return HttpResponseRedirect('/home') else: return render(request, 'register.html', { 'form': uf, 'error': uf.errors, 'user': None }) else: form = UserCreationForm() return render(request, 'register.html', {'form': form, 'user': None})
def get(self, request, *args): form = UserCreationForm() profile = ProfileForm() context = { "form": form, "profile": profile, } return render(request, self.template_name, context)
def confirm_login_allowed(self, user): """ Controls whether the given User may log in. This is a policy setting, independent of end-user authentication. This default behavior is to allow login by active users, and reject login by inactive users. If the given user cannot log in, this method should raise a ``forms.ValidationError``. If the given user may log in, this method should return None. """ if not user.is_active: UserCreationForm.create_activation_key(user) UserCreationForm.send_activation_email(user) raise forms.ValidationError( self.error_messages['inactive'], code='inactive', )
def agree_to_terms(request): # Agree to Terms # Register for account if settings.DEBUG: print(settings.APPLICATION_TITLE, "in accounts.views.agree_to_terms") if request.method == 'POST': form = UserCreationForm(data=request.POST) if form.is_valid(): user = form.save() return redirect(reverse_lazy('home')) else: form = UserCreationForm() context = {'form': form, } # return render_to_response('developer/agree_to_terms.html', RequestContext(request, context,)) return render_to_response(reverse_lazy('accounts:register'), RequestContext(request, context, ))
def agree_to_terms(request): # Agree to Terms # Register for account application_title = settings.APPLICATION_TITLE DEBUG = settings.DEBUG_SETTINGS if DEBUG: print(application_title, "in accounts.views.agree_to_terms") if request.method == 'POST': form = UserCreationForm(data=request.POST) if form.is_valid(): user = form.save() return redirect(reverse_lazy('home')) else: form = UserCreationForm() context = { "APPLICATION_TITLE": application_title, 'form': form, } # return render_to_response('developer/agree_to_terms.html', RequestContext(request, context,)) return render_to_response(reverse_lazy('accounts:register'), RequestContext( request, context, ))
def post(self, request, *args): form = UserCreationForm(request.POST) profile = ProfileForm(request.POST, request.FILES) context = { "form": form, "profile": profile, } if form.is_valid() and profile.is_valid(): user = form.save() pro = profile.save(commit=False) pro.user = user if pro.status != 'ST': requests.objects.create(user=user) pro.slug = slugify(pro.name) pro.save() if pro is not None: login(request, user) return redirect('/home/') return render(request, self.template_name, context)
def clean(self): email = self.cleaned_data.get('email') activation_key = self.cleaned_data.get('activation_key') try: valid_user = User.objects.get(email=email) except User.DoesNotExist: raise forms.ValidationError( self.error_messages['invalid_key'], code='invalid_key', ) try: valid_key = valid_user.activationkey except ActivationKey.DoesNotExist: raise forms.ValidationError( self.error_messages['invalid_key'], code='invalid_key', ) if not valid_key.value == activation_key: raise forms.ValidationError( self.error_messages['invalid_key'], code='invalid_key', ) self.user = valid_user if valid_key.expiration < timezone.now(): UserCreationForm.create_activation_key(self.user) UserCreationForm.send_activation_email(self.user) raise forms.ValidationError( self.error_messages['expired_key'], code='expired_key', ) return activation_key
def registration(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): new_user = form.save() return HttpResponseRedirect(reverse('home')) else: return render(request, "signup.html", {"form": form}) else: form = UserCreationForm() context = {"form": form} return render(request, "signup.html", context)