Django custom registration form

Hiroyuki Nuri

I'm trying to create a custom form where the user can also enter his name for instance but I am facing an issue, when the registration is done the name is not saved and I can't show it on the template.

here is the code

views.py

def register_user(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/user/')
    else:
        if request.method == 'POST':
            form = MyRegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/user/')

        context = {}
        context.update(csrf(request))
        context['form'] = MyRegistrationForm()

        return render(request, 'register.html', context)

forms.py

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

    class Meta:
        model = User
        fields = {'name', 'username', 'password1', 'password2', 'email'}

    def save(self, commit=True):
        user = super(MyRegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.name = self.cleaned_data['name']

        if commit:
            user.save()

        return user

register.html

<form action="/user/register/" method="post" id="register" autocomplete="off">
{% csrf_token %}
<div class="fieldWrapper">
  {{ form.name.errors }}
  {{ form.name.label_tag }}
  {{ form.name }}
</div>
[... other form fields ...]
</div>
<input type="submit" value="Register"/>
</form>

So when I submit the form, everything works but when I try to show {{ user.name }} in the template, nothing is shown, why is that? (it works for the email field)

Timmy O'Mahony

The default User object doesn't have a name field (so you are actually just saving the content of your name field to the object, and not the database). It has a first_name and last_name so you can either use those fields instead or you can customize the User model to have a separate name field

Edit

Also, just so you know, if you use the first_name and last_name fields instead, the User model has a get_full_name() method built-in which might be useful

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django Registration Redux custom registration form

From Dev

Creating Custom user registration form Django

From Dev

form.isValid() always return false in custom registration form django

From Dev

Django Registration Registration_form

From Dev

Issue with dynamically populating dropdownlist for a field in django custom user registration form

From Dev

Drupal Custom registration form save

From Dev

Wordpress | Woocommerce custom registration form

From Dev

Drupal 8 custom registration form

From Dev

Custom Wordpress Registration Form with Validation

From Dev

Remove custom registration form on wordpress

From Dev

Laravel custom validation on registration form

From Dev

How to subclass registration form in django-registration

From Dev

Django-registration, custom registration and validation

From Dev

Django-registration, custom registration and validation

From Dev

Django User Registration Form KeyError

From Dev

How to make django registration form

From Dev

Devise custom validation for a custom field in the registration form

From Dev

why I get three password fields when I define my custom registration form in Django?

From Dev

Creating a custom registration form in Google Course Builder

From Dev

Eventbrite API - custom form registration in website

From Dev

Drupal custom registration Form element not showing on frontend

From Dev

django-registration passing extra_context to Registration Form

From Dev

Django registration form on startpage, without redirect to loginpage

From Dev

NoReverseMatch: in the password reset form in Django-registration?

From Dev

AJAX Form post for Django registration 1.0

From Dev

How to use django countries in registration form?

From Dev

django 1.7 User Registration form with profile picture

From Dev

How to create a profile registration form in Django?

From Dev

Django - Registration form can't upload image

Related Related

  1. 1

    Django Registration Redux custom registration form

  2. 2

    Creating Custom user registration form Django

  3. 3

    form.isValid() always return false in custom registration form django

  4. 4

    Django Registration Registration_form

  5. 5

    Issue with dynamically populating dropdownlist for a field in django custom user registration form

  6. 6

    Drupal Custom registration form save

  7. 7

    Wordpress | Woocommerce custom registration form

  8. 8

    Drupal 8 custom registration form

  9. 9

    Custom Wordpress Registration Form with Validation

  10. 10

    Remove custom registration form on wordpress

  11. 11

    Laravel custom validation on registration form

  12. 12

    How to subclass registration form in django-registration

  13. 13

    Django-registration, custom registration and validation

  14. 14

    Django-registration, custom registration and validation

  15. 15

    Django User Registration Form KeyError

  16. 16

    How to make django registration form

  17. 17

    Devise custom validation for a custom field in the registration form

  18. 18

    why I get three password fields when I define my custom registration form in Django?

  19. 19

    Creating a custom registration form in Google Course Builder

  20. 20

    Eventbrite API - custom form registration in website

  21. 21

    Drupal custom registration Form element not showing on frontend

  22. 22

    django-registration passing extra_context to Registration Form

  23. 23

    Django registration form on startpage, without redirect to loginpage

  24. 24

    NoReverseMatch: in the password reset form in Django-registration?

  25. 25

    AJAX Form post for Django registration 1.0

  26. 26

    How to use django countries in registration form?

  27. 27

    django 1.7 User Registration form with profile picture

  28. 28

    How to create a profile registration form in Django?

  29. 29

    Django - Registration form can't upload image

HotTag

Archive