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
Related
I am newbie to Django and i need help to solve this problem. It states that cannot resolve file'signup'.
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>
You need to have a button to submit.
<input type="submit">
Try that :
<h3>Create your account!</h3>
<form method="post" action="/signup">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Create A Username (use only letters and numbers)" Required>
</div>
<input type="submit">
</form>
Your form was not a valid one and was not submitting anything.
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
(only in Django 2 & 2.0.1)
if I use {{form}} it works but if I use {{form.field}} it disappears all
<form method="POST" class="post-form" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" id="salva" class="btn btn-primary" />
</form>
forms.py
class PreventivoForm(forms.ModelForm):
class Meta:
model = Preventivo
fields = ['cliente','prestazione1', 'ripetizione1', 'prestazione2', 'ripetizione2', 'prestazione3', 'ripetizione3', 'prestazione4', 'ripetizione4', 'prestazione5', 'ripetizione5']
When you use {{form}} then it gets all these value:
<tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" autofocus maxlength="254" required id="id_username" /></td></tr>
<tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password" /></td></tr>
But if you use {{form.username}} and {{form.password}} then:
<input type="text" name="username" autofocus maxlength="254" required id="id_username" />
<input type="password" name="password" required id="id_password" />
So you will only see the input field not label for that input field
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" />
I want to post the data to the server via form. How can I do this?
<div class="panel-body">
<form role="form" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sender-email" class="control-label">Username:</label>
<div class="input-icon"> <i class="icon-user fa"></i>
<input id="sender-email" type="text" placeholder="Username" class="form-control email">
</div>
</div>
<div class="form-group">
<label for="user-pass" class="control-label">Password:</label>
<div class="input-icon"> <i class="icon-lock fa"></i>
<input type="password" class="form-control" placeholder="Password" id="user-pass">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary btn-block" id="authenticate_user" type="submit" value="Submit">
</div>
</form>
</div>
My views.py
#csrf_exempt
def signin(request):
if request.method == 'POST':
print request.POST.get('sender-email') #prints None
username = request.POST['username'] # throws error, username not available.
password = request.POST['password']
How do I post the data without using jquery? I know of a way of posting through AJAX jquery, but dont wanna do that.
The 'name' attribute of input tags is set as keys of request.GET and request.POST.
Add name attribute to all your input tags.
<input name="username" id="sender-email" type="text" placeholder="Username" class="form-control email">
<input name="password" type="password" class="form-control" placeholder="Password" id="user-pass" >
POST data are populated using the name attribute of your input elements. Change your id attributes or add a name one accordingly. E.g:
<input type="password" class="form-control" placeholder="Password" name="user-pass">
You should also consider using a form to handle your data more easily.