How to post data via form HTML? - python

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.

Related

Cannot resolve file 'signup'

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.

request.POST parameters are missing

Hello I am trying to pass some values from my signup form to a django view but for some reason the values are missing.
My view.py :
#csrf_exempt
def signup(request):
if request.method =='POST':
for key, value in request.POST.lists():
print(" DEBUG %s %s" % (key, value))
print(request.POST['name'],email=request.POST['email'])
return render(request, 'front_app/login.html')
My register.html :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" required>
<input type="email" class="email" placeholder="Email" id="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>
and my urls.py :
urlpatterns = [
# other not related urls
path('signup', views.signup, name='signup'),
]
My problem is that when the views.signup function is running it is missing all three values that I am trying to send : name, email and password.
It is a POST request (and not GET) but the only values in the QueryDict are the "encoding" and "csrfmiddlewaretoken" values.
Any help is appreciated!
As Selcuk comment,
The request.POST data is a dict filled with the name of the input fields of the HTML form.
Try with :
<form class="form" method="post" action="{% url 'signup' %}" name="submitForm">
{% csrf_token %}
<input type="text" class="name" placeholder="Name" id="name" name="name" required>
<input type="email" class="email" placeholder="Email" id="email" name="email" required>
<div>
<input type="password" class="password" placeholder="Password (8 characters minimum)" id="password" minlength="8" name ="password" required>
</div>
<input type="submit" class="submit" value="Register" >
</form>

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

how to access radio button selected value in Django

I am trying to access radio button selected value in my django view but it returns on instead of selected value.
Template form
<form class="col-md-8 mx-auto" action="upload" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group" id="div_id_sample" >
<label for="id_sample" class="col-form-label requiredField">Select File</label><br>
<div class="btn btn-primary">
<input type="file" name="sample" class="clearablefileinput" required id="id_sample" accept=".exe">
</div>
</div>
<div class="form-group">
<label for="radiogroup">Enforce Timeout</label><br>
<div class="btn-group" data-toggle="buttons" id="radiogroup">
<label class="btn btn-secondary">
<input type="radio" name="timeoptions" id="option60" autocomplete="off" value="60"> 60 seconds
</label>
<label class="btn btn-secondary">
<input type="radio" name="timeoptions" id="option120" autocomplete="off" value="120"> 120 seconds
</label>
<label class="btn btn-secondary active">
<input type="radio" name="timeoptions" id="option180" autocomplete="off" checked value="180"> 180 seconds
</label>
</div>
</div>
<div class="form-group">
<label for="radiogroup2">Select Machine:</label><br>
<div class="btn-group" data-toggle="buttons" id="radiogroup2">
<label class="btn btn-secondary">
<input type="radio" name="machineoptions" id="machine1" autocomplete="off" value="0"> Windows XP
</label>
<label class="btn btn-secondary active">
<input type="radio" name="machineoptions" id="machine2" autocomplete="off" value="1" checked> Windows 7
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
views.py
in my views i am using ModelForm but not in my HTML and the reason behind that is i copied and pasted the htmlcode generated by the ModelForm. Reason is because i need to collect two more items that are not in my data MODEL so that why.
def upload(request):
if request.user.is_authenticated:
if request.method == 'POST':
file = request.FILES['sample']
form = SampleForm(request.POST, request.FILES)
if form.is_valid():
new_sample = form.save(commit=False)
new_sample.file_name = file.name
new_sample.user = request.user
print(request.POST)
TimeOut = request.POST.get('timeoptions')
machine = request.POST.get('machineoptions')
print(TimeOut)
print(machine)
form.save()
return redirect('reports')
else:
messages.info(request,'Invalid Form')
else:
form = SampleForm()
if request.method == 'GET':
return render(request, 'upload2.html', {'form': form})
else:
return redirect(reverse('login'))
Print output
<QueryDict: {'csrfmiddlewaretoken': ['VvAqdOp8RrpKOgwxLD2dpGartPvUrTHTg9AUbqsX6vphxdojTJqn7tsvLCkneWOm'], 'timeoptions': ['on'], 'machineoptions': ['on']}>
on
on
I am just trying to access the selected button value.
I guess this is mostly because you are enclosing the <input> tag inside the <label> tag.Also you must give the for attribute for and put the input element's id in the <label> tag
Try like this:
<label class="btn btn-secondary" for="option60">60 seconds</label>
<input type="radio" name="timeoptions" id="option60" autocomplete="off" value="60">
You can do this for all the radio buttons.Then in your view you can access with TimeOut = request.POST.get('timeoptions')

Problem in getting data from form in flask

I am basically creating a flask web app which takes username from user and gets his /her codeforces data I am having a problem in extracting the email-id
from the form. I am getting the username but not the email-id.They are both in one form tag in the html file.
Error in browser:
werkzeug.exceptions.HTTPException.wrap..newcls: 400 Bad Request: KeyError: 'email-id'
<form method="POST">
<div class="field">
<label class="label">Username:</label>
<div class="control">
<input class="input" type="text" placeholder="Enter Codeforces username" name="username" value="{{user.handle}}"></input>
</div>
</div>
<div class="has-text-centered">
<input class="button is-link" name="get-info" type="submit" value="Get Info">
</div>
<div class="field">
<label class="label">EMail id:</label>
<div class="control">
<input class="input" type="text" placeholder="Enter mail id" name="email-id" value="">
</div>
</div>
<div class="has-text-centered">
<input class="button is-link" name="get-mail" type="submit" value="Get Mail">
</div>
</form>
#app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST':
username = request.form['username']
email_id = request.form['email-id']
print(email_id)
print(username)

Categories