简体   繁体   中英

'UserCreationForm' object has no attribute 'FormWrapper' in Django

this is my views.py

why i am facing this problem wether oldform is FormWrapper has been removed from the new Django version.

 from django.shortcuts import render
 from django.http import HttpResponse
 from books.models import Author
from models import ContactForm 
from django.db.models import Q
#from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.core.mail import send_mail
from django.template import RequestContext, loader
from django.template.engine import (
                         _context_instance_undefined,                     _dictionary_undefined, _dirs_undefined,
                           )
#from django oldforms as forms
 from django.contrib.auth.forms import UserCreatForm

def register(request):
  form = UserCreationForm()
   if request.method == 'POST':
       data = request.POST.copy()
       errors = form.get_validation_errors(data)
       if not errors:
          new_user = form.save(data)
          return HttpResponseRedirect("/books/")
   else:
       data, errors = {}, {}
   return render_to_response("registration/register.html", {
         'form' : form.FormWrapper(form, data, errors)
          })

Yikes - that is some ugly code. You've really answered your own question - that import is failing because the class isn't there anymore.

Why not just:

from django.contrib.auth.forms import UserCreationForm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render


def register(request):
    form = UserCreationForm(data=request.POST or None)
    if request.method == 'POST' and form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('name-of-books-url'))
    return render(request, 'registration/register.html', {'form': form}

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