简体   繁体   中英

Custom django user registration form

I am learning Django and I am trying to make my own custom registration forms. I currently ran into this error:

Unknown field(s) (user) specified for User

Here is the traceback:

Traceback:
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/handlers/base.py" in get_response
  101.                 resolver_match = resolver.resolve(request.path_info)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in resolve
  320.                     sub_match = pattern.resolve(new_path)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in resolve
  222.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in callback
  229.         self._callback = get_callable(self._callback_str)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/utils/functional.py" in wrapper
  32.         result = func(*args)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in get_callable
  96.             mod = import_module(mod_name)
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/username/Development/django_tutorial/bin/django_test/django_test/views.py" in <module>
  10. from forms import MyRegistrationForm
File "/Users/username/Development/django_tutorial/bin/django_test/django_test/forms.py" in <module>
  7. class MyRegistrationForm(UserCreationForm):
File "/Users/username/Development/django_tutorial/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/forms/models.py" in __new__
  292.                 raise FieldError(message)

Exception Type: FieldError at /accounts/register/
Exception Value: Unknown field(s) (user) specified for User

I think the problem resides within this piece of code:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required = True)

    # hold anything that isnt a form field (meta data)
    class Meta:
        model = User
        fields = ('user', 'email', 'password1', 'password2')    

    def save(self, commit = True):
        # save(commit = T/F) means save or dont save yet
        # calls the super method of this class (UserCreationForm) and calling it's save method with input as false
        user = super(MyRegistrationForm, self).save(commit = False)
        # cleaned_data makes things save to put in data base
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user

I think you mean field "username". Model User haven't field "user". Right:

class Meta:
    model = User
    fields = ('username', 'email', 'password1', 'password2')

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