def test_userForm(self): # test that empty form is rejected data = {'username': '', 'password': '', 'email': ''} form = userForm(data) self.assertFalse(form.is_valid()) # test that email is optional data = {'username': '******', 'password': '******', 'email': ''} form = userForm(data) self.assertTrue(form.is_valid())
def index(request): form = userForm() if request.method == "POST": data = userForm(request.POST) if data.is_valid(): user = BlogPost(user=request.user, content=request.POST['content'], image=request.POST['image']) user.save() else: form = userForm() data = BlogPost.objects.all() return render(request, 'pages/home.html', {'data': data, 'form': form})
def register(request): context_dict = {} if request.method == "POST": form = userForm(request.POST) if form.is_valid(): user = form.save() user.set_password(user.password) user.save() return index(request) else: form = userForm() context_dict['form'] = form return render(request, 'blog/register.html', context_dict)
def register(request): registered = False if request.method == 'POST': user_form = userForm(data=request.POST) profile_form = profileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = userForm() profile_form = profileForm() return render( request, 'registration/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })