One form shared data split into multiple rows - python

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.

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

Add form info into SQLite database python

I am a beginner at databases and I want to make a form in my website that requires any user's information. So the user needs to write in their name and email, add a description to an image and upload the image itself. I want to store the information into an SQLite table in Python.
This is my form:
<form role="form" method="post" id="reused_form" enctype="multipart/form-data">
<div class="form-group">
<label for="name">
Name:</label>
<input type="text" class="form-control"
id="name" name="name" required maxlength="50">
</div>
<br>
<div class="form-group">
<label for="email">
Email:</label>
<input type="email" class="form-control"
id="email" name="email" required maxlength="50">
</div>
<br>
<div class="form-group">
<label for="name">
Message:</label>
<textarea class="form-control" type="textarea" name="message"
id="message" placeholder="Your Message Here"
maxlength="6000" rows="7"></textarea>
</div>
<br>
<div class="form-group">
<label for="name">
Image Upload:</label>
<input type="file" class="form-control" id="image" name="image" required>
</div>
<input type="submit" name="my_button" value="Submit">
</form>
I've started with this python code:
connection = sqlite3.connect('database1.sqlite')
cur = connection.cursor()
cur.execute('DROP TABLE IF EXISTS Artpieces')
cur.execute('CREATE TABLE Artpieces ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, description TEXT NOT NULL, photo BLOB NOT NULL)')
I don't know how to continue. The image part is pretty difficult and I found a tutorial, but I doesn't say how to store an image from a user (so not me). Here it is: https://pynative.com/python-sqlite-blob-insert-and-retrieve-digital-data/ .
Does anyone have any ideas?

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

Why form is not valid in django

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" />

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

Categories