Friday, November 24, 2017

Re: How do I make Django show the "user already exists" message when someone tries to register with an existing username?

Thanks for replying. Looks like it'll be easier for me to combine these into one view.

On Thursday, November 23, 2017 at 7:13:16 PM UTC-5, Tom Tanner wrote:
My `urls.py` has this:
url("^login_register/$", views.login_register, name="login_register"),
url("^register/$", views.register, name="register"),

`views.py` has this:
def login_register(request, template="pages/login_register.html"):
 
'''
 Display registration and login forms
 '''

 registration_form
= RegisterForm()
 
return render(request, template, {"registration_form": registration_form})




def register(request):
 
'''
 Process user registration
 '''

 
if request.method=="POST":
  form
= RegisterForm(request.POST)
  
print form.is_valid()
  
if form.is_valid():
   form
.save()
   email
= form.cleaned_data.get("email")
   raw_password
= form.cleaned_data.get("password1")
   user
= authenticate(username=email, password=raw_password)
   login
(request, user)
   
return redirect(home_slug())
 
else:
  
return redirect(reverse("login_register"))


`login_register.html` form HTML looks like this:
 <h2>Register</h2>
 
<form method="post" action="{% url 'register' %}">
   {% csrf_token %}
   {{ registration_form.as_p }}
   
<button type="submit">Register</button>
 
</form>


When I fill out registration with an email that already exists, I get this error: `ValueError: The view theme.views.register didn't return an HttpResponse object. It returned None instead.`. That's because when `form.is_valid()` is False, there is no return value. What do I need to return so that the message on the Registration form states the email already exists?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/acd3f42f-9859-4c0c-8f00-e8147c0bbc2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment