Remove declaration of first_name, last_name & email in Form calss.
class SignUpForm(UserCreationForm):
    class meta():
        model = User 
        fields = ('username', 'first_name', 'last_name', 'email', 
'password1', 'password2', )

 And while Saving the form, follow below steps.
if form.is_valid():
   new_user = form.save(commit=False)
   new_user.set_password(form.cleaned_data['password1'])
      new_user.save()
      # Your next steps


I think you might need to declare password1 & password2 fields in your 
Form. Do Check.



On Monday, October 29, 2018 at 10:04:39 PM UTC+5:30, Adrian Chipukuma wrote:
>
> Hello, 
>
> I am new to Django and enjoying the learning process, unfortunately I am 
> stuck, and I need expert guidance. 
> I am learning through developing a User Authentication System. The system 
> is supposed to have a user registration functionality, login, user profile 
> editing and logout. I have managed to create the login, logout 
> functionalities and the registration functionality partly. The problem is 
> on the registration, I am only able to save the 'username and password ' 
> using django forms, I have written the code for saving the first_name and 
> the last _name as well as email but it seems not to be working, only the 
> 'username and password' are saved.. I think I am missing something though 
> no error comes up. Please anyone to guide me. Thank you.
> the code is as shown below:
>
>
> views.py
> from django.shortcuts import render, redirect
> from django.contrib.auth import authenticate, login, logout
> from django.contrib.auth.models import User
> from django.contrib import auth, messages
> from django.contrib.auth.forms import UserCreationForm
> from django import forms
> from .forms import SignUpForm
>
> def home(request):
> return render(request, 'authenticate/home.html', {})
>
> def login_user(request):
> if request.method == 'POST':
> username = request.POST['username']
> password = request.POST['password']
> user = authenticate(request, username=username, password=password)
> if user is not None:
> login(request, user)
> messages.success(request,('You have been logged in'))
> return redirect('home')
> else:
> messages.success(request,('Error Logging in!'))
> return redirect('login')
> else:
> return render(request, 'authenticate/login.html', {})
>
> def logout_user(request):
> """if request.method =='POST':"""
> logout(request)
> messages.success(request,('You have been logged out'))
> return redirect('home')
>
> def register_user(request):
> if request.method =='POST':
> form = SignUpForm(request.POST)
> if form.is_valid():
> form.save()
> username = form.cleaned_data['username']
> password = form.cleaned_data['password1']
> user = authenticate(username=username, password=password)
> login(request, user)
> messages.success(request,(' Successfully Registered!'))
> return redirect('home')
> else:
> form = SignUpForm()
>
> context = {'form': form }
> return render(request, 'authenticate/register.html', context)
>
> urls.py
>
> from django.urls import path
> from . import views
> urlpatterns = [
> path('', views.home, name="home"),
> path('login/', views.login_user, name="login"),
> path('logout/', views.logout_user, name='logout'),
> path('register/', views.register_user, name='register'),
> ]
>
> forms.py
> from django.contrib.auth.forms import UserCreationForm, UserChangeForm
> from django.contrib.auth.models import User
> from django import forms
>
> class SignUpForm(UserCreationForm):
> email = forms.EmailField()
> first_name = forms.CharField(max_length=100,)
> last_name = forms.CharField(max_length=100,)
> class meta():
> model = User 
> fields = ('username', 'first_name', 'last_name', 'email', 'password1', 
> 'password2', )
>
> register.html
> {% extends 'authenticate/base.html'%}
> {% block content%}
> <h2>This is the Registration Page</h2>
> <form method="POST" action="{% url 'register' %}">
> {% csrf_token %}
> {% if form.errors %}
> <p>Your form has errors</p>
> {{ error }}
> {% endif %}
> <div class="container">
> {{ form.as_p }}
> <input class="btn btn-primary" type="submit" value="Register">
> <br />
> </div>
> </form>
> {% endblock %}
>
>
> Chao!
>
> Adrian
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

-- 
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+unsubscr...@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/9a58de45-95d1-4fc6-8948-0cc6994c85fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to