Why form is not valid in django - python

Why form is not valid in django!
hi
i dont know why my form is not valid!
i tried this
class SignupForm(forms.Form):
first_name = forms.CharField(required=True, max_length=35)
last_name = forms.CharField(required=True, max_length=35)
username = forms.CharField(required=True, max_length=50)
email = forms.EmailField(required=True)
password = forms.CharField(required=True, min_length=8, max_length=64)
confirm_password = forms.CharField(required=True, min_length=8, max_length=64)
template.html
<form id="signupForm" action="" method="POST" accept-charset="utf-8" class="form" role="form">
{% csrf_token %}
<div class="row">
<div class="col-xs-6 col-md-6">
<input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />
</div>
<div class="col-xs-6 col-md-6">
<input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name" />
</div>
</div>
<input id="username" type="text" autocomplete="off" name="username" value="" class="form-control input-lg" placeholder="Choose Username" />
<input id="email" type="text" autocomplete="off" name="email" value="" class="form-control input-lg" placeholder="Your Email" />
<input id="password" type="password" autocomplete="off" name="password" value="" class="form-control input-lg" placeholder="Password" />
<input id="confirm_password" type="password" name="confirm_password" value="" class="form-control input-lg" placeholder="Confirm Password" />
<div class="topmargin active">
<div class="row" style="display: flex;">
<div class="col-xs-5 col-md-5">
<label>I'm </label>
<select name="month" class = "form-control input-lg">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<br />
<span class="help-block">By clicking Create my account, you agree to our Terms and that you have read our Data Use Policy, including our Cookie Use.</span>
<button class="btn btn-block signup-btn" type="submit">Create my account</button>
</form>
i think it's because i dont pass the form to template, but i dont wanna do that!
it mixes the responsive style
commands are welcome
thank you

The problems are here:
<input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />
<input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name" />
In your form you use first_name and last_name with underscore, but on input they are without the underscore.
You need to change the input names to match the form fields:
<input id="first_name" type="text" autocomplete="off" name="first_name" value="" class="form-control input-lg" placeholder="First Name" />
<input id="last_name" type="text" autocomplete="off" name="last_name" value="" class="form-control input-lg" placeholder="Last Name" />

Related

I want to enable or disable fields for some users from admin panel

This is template
<div class="container mt-3">
<h5 class="card-title">Kindly enter the details. All details are required</h5>
<form class="row g-3" method="post" action="{% url 'home' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="col-md-4">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-4">
<label for="parentage" class="form-label">Father's Name</label>
<input type="text" class="form-control" id="parentage" name="father" required >
</div>
<div class="col-md-4">
<label for="mothername" class="form-label">Mother's Name</label>
<input type="text" class="form-control" id="mothername" name="mother" required>
</div>
<div class="col-md-4">
<label for="phonenumber" class="form-label">Phone number</label>
<input type="tel" class="form-control" id="phonenumber" name="pnumber" required>
</div>
<div class="col-md-4">
<label for="ephonenumber" class="form-label">Emergency contact number</label>
<input type="tel" class="form-control" id="ephonenumber" name="enumber" required>
</div>
<div class="col-md-4">
<label for="dob" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="dob" name="dob" required>
</div>
<div class="col-12">
<label for="inputAddress" class="form-label">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="" name="address" required>
</div>
<div class="col-md-6">
<label for="photo" class="form-label">Upload a photo</label>
<input type="file" class="form-control" id="photo" name="photo" accept="image/*,.pdf" required>
</div>
<div class="col-md-4">
<label for="schoolname" class="form-label">School Name</label>
<input type="text" class="form-control" id="schoolname" name="sname" required >
</div>
<div class="col-md-4">
<label for="inputState" class="form-label">Role</label>
<select id="select" class="form-select" name="role" required>
<option selected>Choose...</option>
<option value="student">Student</option>
<option value="teacher">Teacher</option>
</select>
</div>
<div id="show">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
This is models.py
class Student(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,null=True)
fullname= models.CharField(max_length=50)
father = models.CharField(max_length=30)
mother = models.CharField(max_length=30)
phno=models.IntegerField()
ephno = models.IntegerField()
dob=models.DateField()
address=models.CharField(max_length=100)
photo= models.ImageField(upload_to='photos/')
role=models.CharField(max_length=20)
rollNumber = models.IntegerField()
standard = models.CharField( max_length=10)
def __str__(self):
return self.fullname
This is admin.py
class studentAdmin(ExportActionMixin,admin.ModelAdmin):
list_display=('fullname', 'father',"mother",'phno','ephno',"dob",'address','photo','role','rollNumber','standard')
admin.site.register(Student,studentAdmin)
class teacherAdmin(ExportActionMixin,admin.ModelAdmin):
list_display=('fullname', 'father',"mother",'phno','ephno',"dob",'address','photo','role')
list_filter =['user']
admin.site.register(Teacher,teacherAdmin)
I want admin to enable or disable form fields and model fields from the admin panel.
Is there any way to do the same? For some users, I want to for example keep the mother's name and for some, I want to hide to it

I am working on a website that requires users to register and login but Having problem in adding an additional field to auth_user using django

I have this form in html of my site
<form class="row contact_form" action="." method="post" novalidate="novalidate">
{% csrf_token %}
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="firstname" name="first_name" value=""
placeholder="First Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="lastname" name="last_name" value=""
placeholder="Last Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="mobile" name="mobile" value=""
placeholder="Mobile Number" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="email" name="email" value=""
placeholder="Email" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="password" name="password" value=""
placeholder="Password" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="cpassword" name="cpassword" value=""
placeholder="Confirm Password" required>
</div>
<div class="col-md-12 form-group">
<button type="submit" value="submit" class="btn_3">
SignUp
</button>
</div>
</form>
Now the problem I am facing is:
1)When I try to post data it shows me an error that username is required which i dont want(create_user() missing 1 required positional argument: 'username').
2)There is no mobile number section in auth_user which i want.
I have tried reading the django docs but couldnt understand(OnetoOne thing and custom usermodel thing I couldnt understand both)
Here is my views.py
def signup(request):
if request.method == "POST":
first_name=request.POST['first_name']
last_name=request.POST['last_name']
email=request.POST['email']
mobile=request.POST['mobile']
password=request.POST['password']
cpassword=request.POST['cpassword']
username=request.POST['username']
user=User.objects.create_user(first_name=first_name,last_name=last_name,email=email,password=password,mobile=mobile)
user.save();
return redirect('/')
else:
return render(request,"signup.html")
(The indentations are correct)
Also I want that the users can login using mobile number or email but I dint find any explanation for that.
from django.contrib.auth.models import User
...
class MyUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# add other custom fields
first_name = models.CharField(...)
...
# create as
user = User.objects.create_user(username=email,...)
my_user = MyUser.objects.create(user=user, first_name=first_name, ...)
# authenticate as
user.authenticate(username=email, password=password)
This might help
Django default auth

Retrieve post from flask using a string

How can I retrieve the data from POST in Flask using a for loop in python. I want to dynamically build pages and this would be very useful.
schema=['username', 'phone', 'postal_code', 'address', 'email']
for i in schema:
if request.form.get(i):
db.execute("UPDATE manpower SET :field=:input WHERE username=:username", field=i, input=request.form.get(i), username=request.form.get("user"))
else:
print(request.form.get(i))
Here is my html, as you can see the text names match what I have in schema but for some reason request.form.get(i) always returns None in my python code.
<form action="/manpower" method="post">
<fieldset>
<div class="form-group">
<input type="submit" name="submit" value="query">
<input type="submit" name="submit" value="addNew">
<input type="submit" name="submit" value="update">
</div>
<div class="form-group">
<select name="user">
<option value=""></option>
<option value="bo">bo</option>
<option value="dog2">dog2</option>
<option value="dunkin">dunkin</option>
<option value="tom">tom</option>
</select>
</div>
<div class ="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="username: bo" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="phone" placeholder="phone: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="postal_code" placeholder="postal_code: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="address" placeholder="address: None" type="text"/>
<input autocomplete="off" autofocus class="form-control" name="email" placeholder="email: None" type="text"/>
</div>
</fieldset>
</form>
I found the answer and it's pretty simple. request.form sends everything that was posted from flask and from that I can figure out how to iterate my loops. Thanks for the help guys.
request.form

Redirect after form submission cgi script

I wrote a simple email script in python that takes the data from a form and emails it using boto. What I need to do now is redirect the user to another page "thankyou.html" without showing any cgi output.
HTML FORM:
<form method="post" action="/cgi-bin/iemail.py">
<div class="row uniform 50%">
<div class="6u 12u$(xsmall)">
<input type="text" name="name" id="name" value="" placeholder="Name" required="required"/>
</div>
<div class="6u 12u$(xsmall)">
<input type="text" name="company" id="company" value="" placeholder="Company Name" required="required"/>
</div>
<div class="6u 12u$(xsmall)">
<input type="text" name="phone" id="phone" value="" placeholder="Phone Number" required="required"/>
</div>
<div class="6u$ 12u$(xsmall)">
<input type="email" name="email" id="email" value="" placeholder="Email" required="required"/>
</div>
<div class="12u$">
<div class="select-wrapper">
<select name="category" id="category">
<option value="">- Category -</option>
<option value="Additional Information">Additional Information</option>
<option value="Services Request">Services Request</option>
<option value="Consulting Request">Consulting Request</option>
<option value="Development Request">Development Request</option>
<option value='Support Request'>Support Request</option>
</select>
</div>
</div>
<div class="4u 12u$(small)">
<input type="radio" id="priority-low" name="priority" checked>
<label for="priority-low">Low</label>
</div>
<div class="4u 12u$(small)">
<input type="radio" id="priority-normal" name="priority">
<label for="priority-normal">Normal</label>
</div>
<div class="4u$ 12u$(small)">
<input type="radio" id="priority-high" name="priority">
<label for="priority-high">High</label>
</div>
<div class="12u$">
<textarea name="message" id="message" placeholder="Enter your message" rows="6" required="required"></textarea>
</div>
<div class="6u 12u$(small)">
<!--<input type="checkbox" id="demo-copy" name="demo-copy">
<label for="demo-copy">Email me a copy</label>
</div>-->
<div class="6u$ 12u$(small)">
<input type="checkbox" id="human" name="human" required="required">
<label for="human">Not a robot</label>
</div>
<div class="12u$">
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<input type="hidden" name="redirect" value="index.html" />
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>
Python Script:
#!/usr/bin/python2.7
import cgi
import html
import boto.ses
import cgitb; cgitb.enable() #for troubleshooting
print("Content-Type: text/html")
print
def getdata():
form = cgi.FieldStorage()
name = form["name"].value
company = form["company"].value
phone = form["phone"].value
email = form["email"].value
category = form["category"].value
message = form["message"].value
return name, company, phone, email, category, message
#main Program
if __name__=="__main__":
try:
name, company, phone, email, category, message = getdata()
conn = boto.ses.connect_to_region('us-west-2')
conn.send_email(
'admin#gmail.com',
category,
'%r, %r, %r, %r %r' % (email, name, company,
phone, message),
to_addresses = 'admin#gmail.com'
)
except:
cgi.print_exception()
I only can get redirected to the output for the cgi script, I need to send customers to thankyou.html

One form shared data split into multiple rows

I'm looking for ideas on how to save an big survey into multiple rows (MySQL). Any help would be greatly appreciated. I am relatively new to python and django programming.
Form:
<form method="post">
<label for="InputOD1" class="col-sm-2 control-label">Order Number</label>
<input type="text" id="InputOD1" name="InputOD1" placeholder="Order Number">
<label for="InputSA1" class="col-sm-2 control-label">Shipping Address</label>
<input type="text" id="InputSA1" name="InputSA1" placeholder="Shipping Address">
<label for="InputFN1" class="col-sm-2 control-label">First Name</label>
<input type="text" id="InputFN1" name="InputFN1" placeholder="Last Name">
<label for="InputLN1" class="col-sm-2 control-label">Last Name</label>
<input type="text" id="InputLN1" name="InputLN1" placeholder="Last Name">
<label for="InputGR1" class="col-sm-2 control-label">Gender</label>
<input type="text" id="InputGR1" name="InputGR1" placeholder="Gender>
<label for="InputFN2" class="col-sm-2 control-label">First Name</label>
<input type="text" id="InputFN2" name="InputFN2" placeholder="Last Name">
<label for="InputLN1" class="col-sm-2 control-label">Last Name</label>
<input type="text" id="InputLN2" name="InputLN2" placeholder="Last Name">
<label for="InputGR1" class="col-sm-2 control-label">Gender</label>
<input type="text" id="InputGR2" name="InputGR2" placeholder="Gender>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Expected database outcome:
Order Number, Shipping Address, First Name 1, Last Name 1, Gender 1
Order Number, Shipping Address, First Name 2, Last Name 2, Gender 2
....
Order Number, Shipping Address, First Name 5, Last Name 5, Gender 5
For this task you should use django formsets.

Categories