简体   繁体   中英

Django UserCreationForm neither gives error message nor registering user

I am beginner to python Django. And trying build an posting article website with the help of tutorials. I got stuck at UserCreationForm. I have created a form using UserCreationForm, but when I am submitting the form I am not able to neither submit the form nor getting any error message on the page.

My views.py code

 from django.shortcuts import render_to_response
 from django.contrib.auth import authenticate
 from django.http import HttpResponseRedirect
 from django.contrib import auth
 from django.template.context_processors import csrf
 from django.contrib.auth.forms import UserCreationForm

 def register_user(request):
     if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
           form.save()
           return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))

    args['form'] = UserCreationForm()
    print args
    return render_to_response('register.html', args)


 def register_success(request):
    return render_to_response('register_success.html')

register.html

 {% extends "base.html" %}

 {% block content %}

    <h2>Register</h2>
    <form action="/accounts/register/" method="post"> {% csrf_token %}

      {{form}}

      <input type="submit" value="Register"/>

   </form>

  {% endblock %}

register_success.html

  {% extends "base.hml" %}

  {% block content %}

   <h2>You have registered!</h2>
   <p>Click <a href="/accounts/login/">Here</a> to login again</p>

 {% endblock %}

The problem is that you are always creating a blank form.

args['form'] = UserCreationForm()

This means that you do not see any errors for POST requests when the form is invalid.

Instead, you should only create the blank form for GET requests.

from django.shortcuts import render

def register_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
           form.save()
           return HttpResponseRedirect('/accounts/register_success')
    else:
        form = UserCreationForm()
    args = {'form': form}
    return render(request, 'register.html', args)

Note that I have simplified the view by using render instead of the obsolete render_to_response . That means you don't need to handle csrf manually.

You can use Django Generic Views , specifically CreateView, it will make your life a lot easier. You can use it like so:

from django.views.generic import CreateView

class CreateUserView(CreateView):
    template_name = 'register.html'
    form_class = UserCreationForm
    success_url = '/accounts/register_success'

Add this to your urls.py and you are good to go:

from mysite.views import CreateUserView

# add this url pattern
url(r'^sign_up/$', CreateUserView.as_view(), name='signup'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM