I can't figure out what i've done wrong here. In my views.py.
def register(request):
if request.method == 'POST'
form = UserCreationForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'users/register.html', {'form': form})
def register(request):
if request.method == 'POST': #The colon is missing here
form = UserCreationForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'users/register.html', {'form': form})
You are missing a colon on the following line
if request.method == 'POST'
It should look like this
if request.method == 'POST':
Your current second line needs a : statement in the end, so it should be:
if request.method == 'POST':
Related
After a user successfully registers an account, the webpage redirected to some other locations...
I want it to redirect to a specific path, 'products/index' (products is myapp) after successful registration the user logged in automatically. I am here using function based view..
views.py
def register(request):
if request.method == 'GET':
form = RegisterForm()
context = {'form': form}
return render(request, 'register.html', context)
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('index.html')
else:
print('Form is not valid')
messages.error(request, 'Error Processing Your Request')
context = {'form': form}
return render(request, 'register.html', context)
return render(request, 'register.html', {})
#login_required
def index(request):
products = product.objects.all()
return render (request,'index.html',{'products':products})
def register(request):
if request.method == 'GET':
form = RegisterForm()
context = {'form': form}
return render(request, 'register.html', context)
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('index')
else:
print('Form is not valid')
messages.error(request, 'Error Processing Your Request')
context = {'form': form}
return render(request, 'register.html', context)
You can do something like this to redirect user
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('products:index')
I am making a library system with signup pages (admin and user), so when I make an admin user I want to make it in staff, so how can I use (is_staff)?
this is my registration function...
def register(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, 'Account created successfully')
return redirect(loginpage)
context = {'form':form}
return render(request, 'pages/register.html', context)
You can alter the .instance wrapped in the form before saving it to the database:
def register(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.instance.is_staff = True
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, 'Account created successfully')
return redirect(loginpage)
return render(request, 'pages/register.html', {'form':form})
I have a problem in registration with django, here is my views code:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'users/reg_form.html', args)
,but i always get:
ValueError at /user/register/ The view Users.views.register didn't
return an HttpResponse object. It returned None instead. Request
Method: POST Request URL: http://127.0.0.1:3001/user/register/ Django
Version: 2.0.2 Exception Type: ValueError Exception Value: The view
Users.views.register didn't return an HttpResponse object. It returned
None instead. Exception
Location: /home/iah/.local/lib/python3.5/site-packages/django/core/handlers/base.py
in _get_response, line 139
Check you code and ask yourself what happens if you have a POST request and the form doesn't validate - you'll find out that in this case you have no explicit return path, so the function implicitely returns None.
The fix is dead simple : deindent the last two line of your function, so you when the form doesn't validate you return the rendered template too:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
form = RegistrationForm()
# MAKE SURE WE ALWAYS RETURN A RESPONSE:
# we end up here when it's a GET request
# AND when it's a POST request and the form
# did not validate
args = {'form': form}
return render(request, 'users/reg_form.html', args)
You have to return response from the inner else block:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
# here
...
Make sure you return something if the form is not valid and your form.is_valid() fails.
Example:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
return redirect('/') # or render(...)/whatever you need to redirect to
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'users/reg_form.html', args)
Hope this helps!
You are missing the else code. Your if statement says: If the form is valid, save the form. But what if the form is not valid, What if the username is all special characters(ex: !##$%%%^^) or what if the username is only 1 Character long ex: (username:A). Or what if the password is only 3 characters long (ex: pas) In all these scenarios, the user should get the blank form back. You should also consider using class based views for registration This link will help
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
**if form.is_valid():**
# What if the form is not valid you haven't accounted for that
form.save()
return redirect('/')
else:
# Adding a else solves this problem. The user gets a blank form back
form = RegistrationForm()
args = {'form': form}
return render(request, 'users/reg_form.html', args)**
def new_topic(request):
"""add new topic"""
if request.method != 'POST':
form = TopicForm()
else:
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('learning_logs:topics'))
context = {'form': form}
return render(request, 'learning_logs/new_topic.html', context)
The error I got:
The view learning_logs.views.new_topic didn't return an HttpResponse object. It returned None instead.
And I have searched many related problems as well as tested them, yet they did not work.Could you please give me some help,thanks.
def new_topic(request):
"""add new topic"""
if request.method == 'POST':
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('learning_logs:topics'))
else:
form = TopicForm()
return render(request, 'learning_logs/new_topic.html', {'form': form})
The view django_bookmarks.bookmarks.views.register_page didn't return an HttpResponse object. How can I fix this problem?
The code
def register_page(request):
if request.method =='POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.clean_data['username'],
password=form.clean_date['password1'],
email=form.clean_data['email']
)
return HttpResponseRedirect('/')
else:
form = RegistrationForm()
var = RequestContext(request, {
'form': form
})
return render_to_response( 'registration/register.html',var
)
You should add an alternative for your "if" condition. What if request.method is not POST?:
def register_page(request):
if request.method =='POST':
...
else:
form = RegistrationForm()
return render_to_response(...)