Does not show forms errors in Django - python

I want to show the required errors but not showing them. I made custom errors in form.py but it does not show either my current or default and why?
When I submit a blank form I want it to appear, my created error
this is my code -->
home.html
{% extends "home/base.html" %}
{% block content %}
<h2>Sign up Form</h2>
<div class="frm">
<form method="post" novalidate>
{% csrf_token %}
{{form}}
<button type="submit">sign up</button>
</form>
</div>
{% endblock %}
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
User = get_user_model()
class SignUpForm(UserCreationForm):
username = forms.CharField(max_length=30)
username.error_messages['required'] = ('Username field is required')
email = forms.EmailField(max_length=200)
def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'inp'
for field in self.fields.values():
field.error_messages = {'required':'The field {fieldname} is required'.format(
fieldname=field.label)}
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2','mob', )
views.py
from .forms import SignUpForm
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.contrib.auth.forms import authenticate
from django.contrib.auth import login
def home(request):
form = SignUpForm(request.POST)
if request.method=="POST":
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
else:
form = SignUpForm()
return render(request, 'home/home.html', {'form': form})
def about(request):
return render (request, 'home/about.html')
who can help me?
I searched a lot on Google but could not find a solution

You are returning a clean form without the invalid data, when your form is invalid.
It seems your else block is not correctly indented.
def home(request):
if request.method=="POST":
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
else: # << change indentation
form = SignUpForm()
return render(request, 'home/home.html', {'form': form})

In your views.py, how you call the form is wrong, and also how you pass POST request. It should be like this
def home(request):
form = SignUpForm() # here
if request.method=="POST":
form = SignUpForm(request.POST) # and here
if form.is_valid():
...
or this,
def home(request):
form = SignUpForm(request.POST or None) # here
if request.method=="POST":
if form.is_valid():
...
The other main issue is mentioned in HÃ¥ken Lid's answer.

Related

user register form in the django not getting register

I am trying to register the user of the django website but it was not getting register
I am posting the code please help me
this is the code of the views.py where I get the input
from django.shortcuts import render , redirect
# Create your views here.
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm
from .models import User_Main
def become_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request , user)
user_main = User_Main.objects.create(name = user.username, created_by=user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'user_of_ecommerce/become_user.html' ,{'form':form})
code of urls.py
from django.urls import path
from . import views
urlpatterns = [
path('become-user/' , views.become_user , name="become_user")
]
and here is the code of the form
{% extends "core_of_ecommerce/base.html" %}
{% block content %}
<form action="." method="post" >
{% csrf_token %}
{{form.as_p}}
<div>
<button>sbmot</button>
</div>
</form>
{% endblock content %}
another URLs.py
from django.urls import path
from . import views
urlpatterns = [
path('' ,views.home, name='home')
]
models.py
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
class User_Main(models.Model):
name = models.CharField(max_length = 150)
created_at = models.DateTimeField( auto_now_add=True)
created_by = models.OneToOneField(User, on_delete=models.CASCADE , related_name='user_name')
class Meta:
ordering = ['name']
def __str__(self):
return self.name
when I click on the button after entering all the details it refreshes me to the same page I hope you can help me
and I even checked from the admin panel It doesn't register
your template are causing this ,because you did not submit correctly ,chagge the button submit
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{form.as_p}}
<div>
<input type="submit" value="Register">
</div>
</form>
as for action it is better that you create a url named login ,that handle a login view
Update
ok let's do like this ,replace your register view with this,
def become_user(request):
if request.method == 'POST':
form = UserCreationForm(data=request.POST)
if form.is_valid():
user = form.save()
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'user_of_ecommerce/become_user.html' ,{'form':form})
and verify that the user is acually saved (in you admin page), then we worry about logging in.
once the save is working, modify you view like this:
from django.contrib.auth import authenticate, login
def become_user(request):
if request.method == 'POST':
form = UserCreationForm(data=request.POST)
if form.is_valid():
user = form.save()
cd = form.cleaned_data
user = authenticate(username=cd['username'], password=cd['password1']) #new line
login(request, user, backend='django.contrib.auth.backends.ModelBackend') #new line
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'user_of_ecommerce/become_user.html' ,{'form':form})
def become_user(request):
if request.method == 'POST':
form = UserCreationForm(data=request.POST)
if form.is_valid():
user = form.save()
login(request , user)
user_main = User_Main.objects.create(name = user.username, created_by=user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'user_of_ecommerce/become_user.html' ,{'form':form})

Images Along With Post Only Applying In Django Admin and Not Regular Form

Like the title says, I'm trying to create a social media app and my users can upload text to their websites. Currently I'm working on adding Image functionality. I can easily add this images in the Django admin page, but whenever I try to do it from the User's create post form, nothing shows up. Here's my post create form:
{% extends "social/base.html" %}
{% load crispy_forms_tags %}
{% block content4 %}
<h1>Make Your Post</h1>
<p>Write a post / Share an image</p>
<br>
<div class="container">
<form method="post">
{% csrf_token %}
{{form|crispy}}
<button type="submit" name="button">Make Post</button>
</form>
</div>
{% endblock content4 %}
Here's my views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Posts
from django.contrib.auth.forms import AuthenticationForm
from .forms import NewUserForm
from django.contrib import messages
from django.contrib.auth import logout, authenticate, login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView
from django.contrib.auth.decorators import login_required
# Create your views here.
def home(request):
context = {
'posts' : Posts.objects.all()
}
return render(request, 'social/social.html', context)
def register(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"New Account Created: {username}")
login(request, user)
return redirect ("/social")
else:
for msg in form.error_messages:
print(form.error_messages[msg])
messages.error(request, f"{msg}: {form.error_messages[msg]}")
return render(request, 'social/register.html', {"form" : form})
form = NewUserForm
return render(request, 'social/register.html', {"form" : form})
def logout_request(request):
logout(request)
messages.info(request, "Logged out successfully!")
return redirect('/social')
def login_request(request):
if request.method == 'POST':
form = AuthenticationForm(request, data = request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username = username, password = password)
if user is not None:
login(request, user)
messages.info(request, f"You are now logged in as {username}")
return redirect ('/social')
else:
messages.error(request, "Invalid username and/or password.")
else:
messages.error(request, "Invalid username and/or password.")
form = AuthenticationForm()
return render(request, 'social/login.html', {'form' : form})
class PostCreateView(CreateView):
model = Posts
fields = ['post_title', 'post_text_content', 'post_image']
def form_valid(self, form):
form.instance.post_author = self.request.user
print(self.request.user)
return super().form_valid(form)
my model:
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Posts(models.Model):
post_title = models.CharField(max_length = 40, help_text = 'Enter post title')
post_text_content = models.TextField(max_length = 1000)
post_author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
post_date = models.DateField(auto_now = True, auto_now_add = False)
#Make optional Image Field
post_image = models.ImageField(upload_to = 'images', blank = True)
class Meta:
ordering = ['-post_date', 'post_title', 'post_author', 'post_text_content', 'post_image']
def __str__(self):
return self.post_title
def get_absolute_url(self):
return reverse('social-home')
That's pretty much it. If you need any more code, please ask me.
Thank you in advance!
EDIT: To be clear, when I add the images from the admin "create new post" it works, but when I add the image from the regular django form it does not show up.
You need to set enctype="multipart/form-data" on your form tag in you HTML to upload files.
<form method="post" enctype="multipart/form-data">

Django - authenticate() A user with that username already exists

Okay I'm confused.
I'm trying to build a login page, but whenever I try to login, django gives the error that the username already exists. I haven't used save() anywhere.
I'm using authenticate(), I referred the Django docs for that:
https://docs.djangoproject.com/en/1.10/topics/auth/default/#how-to-log-a-user-in
Here is my code, please tell me where I'm going wrong:
forms.py
class LoginForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))
class Meta:
model = User
fields = ['username', 'password']
views.py
class LoginFormView(View):
form_class = LoginForm
template_name = 'login.html'
# display a blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
# authenticate user
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('slrtcebook:home')
return render(request, self.template_name, {'form': form})
login.html
<div class="form-container">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
{{ field }}
{{ field.errors }}
{% endfor %}
<input id="submit" type="submit" value="Log in" />
</form>
</div>
<p>Don't have an account? Register here</p>
Don't use a ModelForm for this; it will assume you're trying to create a user, and validate that you can do so with the data you've entered. Just use a standard form - inherit from forms.Form and remove the Meta class.
For those of you who want the code here is what I did to fix it:
inside of views.py:
class UserLoginView(View):
form_class = LoginForm
template_name = 'music/login_form.html'
#display a blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
#proces form data
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
# user = form.save(commit=False)
#cleaned (normalized) data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
# user.set_password(password) #this is the only way to change a password because of hashing
#returns the User obejects if credintials are correct
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('music:index')
return render(request, self.template_name,{'form': form})
inside of froms.py:
class LoginForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))
fields = ['username', 'password']
don't forget to also import LoginForm at the top of views.py, where you import UserForm:
from .forms import UserForm, LoginForm

login form keeps telling my username already exists django

So I am trying to make a login form for a django website using a forms.py file to generate a form in my html. I have already made a registration form using the same methods and that works fine but for some reason my login form keeps working like a registration form and saying that my username already exists. i assume it is just a litle mistake but I can't figure out what it is exactly.
forms.py
from django.contrib.auth.models import User
from django import forms
class LoginForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'password']
models.py
class LoginFormView(View):
form_class = LoginForm
template_name = 'intranet/login_form.html'
def get(self, request):
form = self.form_class(None)
return render(request,self.template_name,{'form': form})
def post(self,request):
form = self.form_class(request.POST)
if form.is_valid():
username = request.POST.get['username']
password = request.POST.get['password']
user = authenticate(username=username,password=password)
if user is not None:
if user.is_active:
login(request,user)
return redirect('werknemers_list')
return render(request, self.template_name, {'form': form})
login_form.html
<h1>Log in</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="log in" />
</form>
urls.py
from django.conf.urls import url
from intranet import views
urlpatterns = [
url(r'^$', views.LoginFormView.as_view(), name='login'),
url(r'^registreer$', views.UserFormView.as_view(), name='registreer')]
You shouldn't use a ModelForm for this. That will always call the model validation, which in this case checks for username uniqueness.
Use a standard Form class instead.

Why my django registration form does not pass is_valid()

I am new to Django and was following a tutorial on how to build a register view. I did exactly the same but my form does not pass form.is_valid().
Here is what I did:
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
views.py
def register_user(request):
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
user = form.save()
return HttpResponseRedirect('/accounts/register_success')
form = MyRegistrationForm()
return render(request, 'register.html', {'form':form})
def register_success(request):
return render(request, 'register_success.html')
register.html
{% extends "base.html" %}
{% block content %}
<h2>Register</h2>
<form action="/accounts/register/" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Register" />
</form>
{% endblock %}
When I tried to register new users on the webpage, none of them passed. Even when I used username: testuser email: testuser#example.com password:testuser123, it failed. So what is wrong?
Thanks in advance!
You should follow the correct view pattern. Put the line form = MyRegistrationForm() inside an else block, then the page itself will tell you why the form is not valid.

Categories