Monday, October 29, 2018

New to Django: Please Help!! Django User Model not saving first_name, last_name and email - Authentication App

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+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_LKtZ7VP1BrKv_HFYckBAqM%2BMWwjv5WJKjx9bpYkNLd3tw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment