def post(self, request, *args, **kwargs): form = MyUserCreationForm(request.POST) if form.is_valid(): form.save() return redirect('accounts:login') context = { 'form': form, } return render(request, self.template_name, context)
def register_view(request, *args, **kwargs): if request.method == 'POST': form = MyUserCreationForm(data=request.POST) if form.is_valid(): user = form.save() login(request, user) return redirect('product-list') else: form = MyUserCreationForm() return render(request, 'user_create.html', context={'form': form})
def register_view(request, *args, **kwargs): if request.method == 'POST': form = MyUserCreationForm(data=request.POST) if form.is_valid(): user = form.save() login(request, user) user.save() Profile.objects.create(user=user) return redirect('webapp:home') else: form = MyUserCreationForm() return render(request, 'user_create.html', context={'form': form})
def signup(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password1'] form = MyUserCreationForm(request.POST) if form.is_valid(): form.save() user = authenticate(username=username, password=password) auth_login(request, user) messages.success(request, 'Profile successfully created.') return redirect(reverse('home')) else: form = MyUserCreationForm() return render(request, 'accounts/signup.html', {'form': form})
def signup(request): """ 该函数用于用户的注册 进入到注册页面 """ if request.method == 'POST': form = MyUserCreationForm(request.POST) if form.is_valid(): new_user = form.save() new_user.backend="%s.%s" %('django.contrib.auth.backends','ModelBackend') #以新用户登陆 auth.login(request, new_user) #进入主页面 return HttpResponseRedirect("/") else: form = MyUserCreationForm() return render_to_response("accounts/signup.html", { 'form': form, },context_instance=RequestContext(request))
def post(self, request): form = MyUserCreationForm(request.POST) form2 = CustomerCreateForm(request.POST) if form.is_valid() and form2.is_valid: user = form.save() customer = form2.save(commit=False) customer.user = user customer.save() user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password1']) if user is not None: login(request, user) return redirect('home') return render(request, 'registration/register.html', { "form": form, "form2": form2 })
def create(request): if 'next' in request.GET: next_page = request.GET['next'] elif 'next' in request.POST: next_page = request.POST['next'] else: next_page = '' if request.method == 'POST': form = MyUserCreationForm(request.POST) if form.is_valid(): new_user = form.save() user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request,user) if next_page: return redirect(next_page) else: return redirect('home') else: form = MyUserCreationForm() return render_to_response('accounts/create.html', {'form': form, 'next': next_page, 'user': request.user}, context_instance=RequestContext(request))
def new_account(request): if request.method == 'POST': form = MyUserCreationForm(request.POST) if form.is_valid(): form.save() utilities.set_message( request, "User '{}' created!".format(form.cleaned_data['username'])) return HttpResponseRedirect(reverse('accounts:login')) else: form = MyUserCreationForm() context = {'form': form} return render(request, 'accounts/new_account.html', context)
def test_form_success(self): form = MyUserCreationForm(data={'username': '******', 'password1': 'user', 'password2': 'user'}) self.assertTrue(form.is_valid())
def test_form_error_passwords_do_not_match(self): form = MyUserCreationForm(data={'username': '******', 'password1': 'user', 'password2': 'other password'}) self.assertFalse(form.is_valid())
def test_form_error_not_required_fields(self): form = MyUserCreationForm(data={'username': '******', 'password1': 'user'}) self.assertFalse(form.is_valid())