I have sorted it out using a different approach 'AbstractUser'. Thanks
On Tue, Oct 30, 2018, 12:19 PM Adrian Chipukuma <chipukumaa@gmail.com> wrote:
Thanks for the help. Though if I remove the declarations of the first_name, last_name and email. Then I will not be able to see these in the Register form.. The fields password1 and password2 are just working well and can be saved... The issue is on the first_name, last_name and email fields... I can't save them using the django form..On Tue, Oct 30, 2018, 7:49 AM Manjunath <dmanju.manju58@gmail.com> wrote: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():
password1'])
new_user = form.save(commit=False)
new_user.set_password(form.cleaned_data['
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.pyfrom django.shortcuts import render, redirectfrom django.contrib.auth import authenticate, login, logoutfrom django.contrib.auth.models import Userfrom django.contrib import auth, messagesfrom django.contrib.auth.forms import UserCreationFormfrom django import formsfrom .forms import SignUpFormdef 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.pyfrom django.urls import pathfrom . import viewsurlpatterns = [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.pyfrom django.contrib.auth.forms import UserCreationForm, UserChangeFormfrom django.contrib.auth.models import Userfrom django import formsclass SignUpForm(UserCreationForm):email = forms.EmailField()first_name = forms.CharField(max_length=100,)last_name = forms.CharField(max_length=100,)class meta():model = Userfields = ('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+unsubscribe@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.
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+unsubscribe@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/CAHBk_LLvcjm-Jkfom02c2UpbZziA5aF3YY%2B%2BdHnZn%3DxOkK3Hfw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment