Wednesday, August 28, 2019

Re: For validation not working for UserCreationForm


Firstly I Have More than 1 Million Data In my database , while i want to perform aggregation or annotation or filter it took huge time , i have to show all data at a time,,, i use select_related() for foreign key but i want to optimize database hitting , like i want to fetch all data from database in first database hit and convert them into dictionary or list and perform another queries into dictionary or list without hitting database i hope it can optimize database hit Example : In Laravel I Fetch All data from database and convert it into Array while i have to perform queries into it i convert them into collect() and perform queries , and it dont hit the database .
[a link ]:(https://laravel.com/docs/5.8/collections) !

I Can perform this in Django ???


On Wed, Aug 28, 2019 at 11:51 PM 'Amitesh Sahay' via Django users <django-users@googlegroups.com> wrote:
Hey Kean, 

I am already working on some Djago user authentication . Below are my codes, may be they would help you.

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from templates.authenticate.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 loggedin success')
return redirect('home')
else:
messages.success(request, 'Error loggin in....')
return redirect('login')

else:
return render(request, 'authenticate/login.html', {})


def logout_user(request):
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(request, username=username, password=password)
login(request, user)
messages.success(request, 'registration successfull....')
return redirect('home')
else:
form = SignUpForm()
context = {'form': form}
return render(request, 'authenticate/register.html', context)


def edit_profile(request):
if request.method == 'POST':
form = UserChangeForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(request, username=username, password=password)
login(request, user)
messages.success(request, 'registration successfull....')
return redirect('home')
else:
form = UserChangeForm(instance=request.user)

context = {'form': form}
return render(request, 'authenticate/edit_profile.html', context)
forms.py

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


class SignUpForm(UserCreationForm):
email = forms.EmailField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'email address'}))
first_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'first name'}))
last_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'last name'}))


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


def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)

self.fields['username'].widget.attrs['class'] = 'form-control'
self.fields['password1'].widget.attrs['class'] = 'form-control'
self.fields['password2'].widget.attrs['class'] = 'form-control'
I have also attached the HTML pages with this email in the txt format. I hope that helps you. Below is the django official doc link that I am referring.


Regards,
Amitesh Sahay


On Wednesday, 28 August, 2019, 10:24:38 pm IST, Kean <keanld1@gmail.com> wrote:


Hi,
New to Django.

I've created a registration form which inherits from a User form.
The issue is none of the validators are working. e.g reporting to user password miss-match, or any other error if user enters data incorrect to validation.
The form simply validates with errors and redirect user to to their user page (as defined,).

Please can anyone help or advise, as I though the validators were an inherent part of the django forms?

forms.py

class CRegisterForm(UserCreationForm):
    email = forms.EmailField(max_length=30, required=True)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

views.py

def cregister(request):
    if request.method == "POST":
        form = CRegisterForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            form.save()
        return redirect('cpage')
    else:
        form = CRegisterForm()
        return render(request, 'cregister.html', {'form': form})

urls.py

 path('login/customer/', views.cpage, name='cpage'),

html

{% load crispy_forms_tags %}
<!DOCTYPE html>
<html>
<head>
<title>Customer</title>
</head>
<br>
<body>
<div>
<div class = "container" >
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-3">Customer Login</legend>
{{ form|crispy }} 
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Login</button>
</div>
{% if messages %}
  {% for messages in messages %}
    <div class="alert alert-{{ message.tags }}">
      {{ message }}
    </div>
    {% endfor %}
    {% endif %}
</form>
<div class="border-top pt-3">
<small class = "text-muted">
Need a customer account <a class="ml-2" href="cregister">Customer register</a>
</small>
</div>
</div>
</body>
</html>



Best,

K




 

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4739118-05b9-4706-b1cb-08062cc6f93d%40googlegroups.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1441704490.2800369.1567014644390%40mail.yahoo.com.


--

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BEpJfwc3Yyoacz2-f7tYKtxr8K%3Dz2p%3Dg9ReZtDK5o0nL%2BBKAw%40mail.gmail.com.

No comments:

Post a Comment