I am trying to get the input of the forms by just using requests and no actual django form(I do not need to store this data in a database.) So I am trying to use request.GET.get() method, but so far I have had no luck with it. I am pretty sure that it is something wrong with my HTML, but I could be wrong.
Here is my views.py
if request.method == "GET":
lineGraphInp=request.GET.getlist('lineGraphInp')
lineTableInp=request.GET.get('lineTableInp')
print(lineGraphInp)
print(lineTableInp)
Also, here are my forms
<form method="GET">
<h3>Choose: </h3>
<select class="custom-select" name="lineTableInp">
<option value="myPosition">My Position</option>
<option value="myCompetitors">My Competitors</option>
<option value="top100">Top 100</option>
<option value="bottom100">Bottom 100</option>
</select>
</form>
and
<form method="GET">
<h3>Choose: </h3>
<div style="margin-left:15px;">
<div class="form-check form-check-inline">
<input name="lineGraphInp" class="form-check-input" type="checkbox" value="myPosition">
<label class="form-check-label" for="inlineCheckbox1">My Position</label>
</div>
<div class="form-check form-check-inline">
<input name="lineGraphInp" class="form-check-input" type="checkbox" value="myCompetitors" >
<label class="form-check-label" for="inlineCheckbox2">My Competitors</label>
</div>
<div class="form-check form-check-inline">
<input name="lineGraphInp" class="form-check-input" type="checkbox" value="top100">
<label class="form-check-label" for="inlineCheckbox3">Top 100</label>
</div>
<div class="form-check form-check-inline">
<input name="lineGraphInp" class="form-check-input" type="checkbox" value="bottom100">
<label class="form-check-label" for="inlineCheckbox3">Bottom 100</label>
</div>
</div>
</form>
You have not specified form acion
Eg.
<form method="GET" action="/">
Content Here
</form>
Please make sure that you mention that action it will not work unless you will mention it
I think these only a issue..!!!
Thank You
Related
I made a customer feedback form with rating stars. But problem is, not getting stored data in database. Where the actual problem occured? What is the relevent solution? I tried many time.
forms.py:
class FeedBackForm(forms.Form):
feedBACK = forms.CharField(widget=forms.Textarea, required=True)
rating = forms.CharField(widget=forms.NumberInput)
models.py:
class ProductREVIEWS(models.Model):
rating = models.IntegerField(blank=True, null=True)
feedBACK = models.TextField(blank=True, null=True)
views.py:
def quick_view(request, quick_view_id):
form = FeedBackForm()
if request.method == "POST" and request.user.is_authenticated:
form = FeedBackForm(request.POST)
if form.is_valid():
ProductREVIEWS.objects.create(
feedBACK=form.cleaned_data.get('feedBACK'),
rating=form.cleaned_data.get('product_rating'),
)
template:
<form action="#!" method="POST" class="needs-validation mt-3" style="font-size: 13px;" novalidate="" autocomplete="off">
{% csrf_token %}
<div class="radio-toolbar d-flex">
<input type="radio" id="one_star" name="product_rating" value="1" checked>
<label for="one_star" class="mx-1">1 <i class="fas fa-star"></i></label>
<input type="radio" id="two_star" name="product_rating" value="2">
<label for="two_star" class="mx-1">2 <i class="fas fa-star"></i></label>
<input type="radio" id="three_star" name="product_rating" value="3">
<label for="three_star" class="mx-1">3 <i class="fas fa-star"></i></label>
<input type="radio" id="four_star" name="product_rating" value="4">
<label for="four_star" class="mx-1">4 <i class="fas fa-star"></i></label>
<input type="radio" id="five_star" name="product_rating" value="5">
<label for="five_star" class="mx-1">5 <i class="fas fa-star"></i></label>
</div>
<div class="mb-3 mt-2">
<textarea id="email" placeholder="Share your experiencs..." rows="10" style="font-size: 13px; text-transform: lowercase;" type="email" class="form-control" name="feedBACK" value="" required></textarea>
</div>
Since, you are not doing anything with FeedBackForm, so you can simply get the names of fields through POST request in view as:
from django.shortcuts import redirect
def quick_view(request, quick_view_id):
if request.method == "POST" and request.user.is_authenticated:
ProductREVIEWS.objects.create(
feedBACK=request.POST.get('feedBACK'),
rating=request.POST.get('product_rating')
)
return redirect('success')
return render(request, 'some_folder_name/index.html')
def success(request):
return render(request, 'some_folder_name/success.html')
Note: Its a good practice to redirect after dealing with POST data.
Template file:
<form method="POST" class="needs-validation mt-3" style="font-size: 13px;" autocomplete="off">
{% csrf_token %}
<div class="radio-toolbar d-flex">
<input type="radio" id="one_star" name="product_rating" value="1" checked>
<label for="one_star" class="mx-1">1 <i class="fas fa-star"></i></label>
<input type="radio" id="two_star" name="product_rating" value="2">
<label for="two_star" class="mx-1">2 <i class="fas fa-star"></i></label>
<input type="radio" id="three_star" name="product_rating" value="3">
<label for="three_star" class="mx-1">3 <i class="fas fa-star"></i></label>
<input type="radio" id="four_star" name="product_rating" value="4">
<label for="four_star" class="mx-1">4 <i class="fas fa-star"></i></label>
<input type="radio" id="five_star" name="product_rating" value="5">
<label for="five_star" class="mx-1">5 <i class="fas fa-star"></i></label>
</div>
<div class="mb-3 mt-2">
<textarea id="email" placeholder="Share your experiencs..." rows="10" style="font-size: 13px; text-transform: lowercase;" type="email" class="form-control" name="feedBACK" value="" required></textarea>
</div>
<input type="submit" value="save">
</form>
urls.py
urlpatterns = [
...
path('success/',views.success,name='success')
]
success.html
<body>
<h2>Form successfully submitted.</h2>
</body>
Your code isn't actually utilising the functionality of Django forms. Firstly, I would suggest using a model form rather than just a normal form - it will automatically do some of the lifting to connect the form with your model.
https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/#modelform
Secondly, you are validating the form using is_valid() but you are then manually creating the object - you can instead using save() to create the model.
I have created a form in Flask, and want to submit certain values which need to be processed.But the method used is getting defaulted to GET even though I have specified the method as post in my form
These are the the relevant code files:
app.py
#app.route('/test',methods=["GET","POST"])
def test():
print(request.method)
error = None
try:
if request.method == "POST":
first_name = request.form['firstname']
last_name = request.form['lastname']
flash(first_name)
flash(last_name)
return render_template("test.html")
else:
return "Wrong"
except Exception as e:
return str(e)
test.html
<form method="post" class="text-center" style="color: #757575;" action="">
<div class="form-row">
<div class="col">
<!-- First name -->
<div class="md-form">
<input type="text" name="firstname" value="{{request.form.firstname}}" class="form-control">
<label for="materialRegisterFormFirstName">First name</label>
</div>
</div>
<div class="col">
<!-- Last name -->
<div class="md-form">
<input type="text" name="lastname" value="{{request.form.lastname}}" class="form-control">
<label for="materialRegisterFormLastName">Last name</label>
</div>
</div>
</div>
<!-- File Upload -->
<div class="md-form">
<input type="file" id="fileupload" class="form-control">
</div>
<input class="btn btn-info btn-block" type="submit" value="Submit">
</form>
The method is getting defaulted to post and the response "Wrong" on loading 127.0.0.1:5000/test. The method is always GET
<form method="post" class="text-center" style="color: #757575;" action="/test" method="post">
<div class="form-row">
<div class="col">
<!-- First name -->
<div class="md-form">
<input type="text" name="firstname" value="{{request.form.firstname}}" class="form-control">
<label for="materialRegisterFormFirstName">First name</label>
</div>
</div>
<div class="col">
<!-- Last name -->
<div class="md-form">
<input type="text" name="lastname" value="{{request.form.lastname}}" class="form-control">
<label for="materialRegisterFormLastName">Last name</label>
</div>
</div>
</div>
<!-- File Upload -->
<div class="md-form">
<input type="file" id="fileupload" class="form-control">
</div>
<input class="btn btn-info btn-block" type="submit" value="Submit">
</form>
By adding the "route" to the action attribute and defining the method of submission of form, you can perform the desired objective.
I am trying to use a check box control in a simple django application. Code Logic seems to be fine, But I am getting an empty fruit list ([None, None]). I don't know why it's not working, Can anybody point out the mistake. Thanks in advance
index.html
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Apple" id="apple">
<label class="form-check-label" for="apple">Apple</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Mango" id="mango">
<label class="form-check-label" for="mango">Mango</label>
</div>
view.py
if request.method == 'POST':
fruit = []
fruit.append(request.POST.get('apple'))
fruit.append(request.POST.get('mango'))
As Daniel mentioned, you have to add a name attribute to form elements, so they are submitted to the server.
index.html
<form method="post">
{% csrf_token %}
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Apple" id="apple" name="fruits">
<label class="form-check-label" for="apple">Apple</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Mango" id="mango" name="fruits">
<label class="form-check-label" for="mango">Mango</label>
</div>
<button type="submit">Submit</button>
</form>
That way, you can get a list of fruits in your view:
views.py
if request.method == 'POST':
fruits = request.POST.getlist('fruits')
The fruits variable would be a list of check inputs. E.g.:
['Apple', 'Mango']
input elements need a name attribute, otherwise the browser does not send any data.
I want to submit a form in Python requests. I'm trying to get the list of all members of my organisation from the website.
I've tried the following code:
searchparams={'name':'smith', 'grade':'0', 'format':'html'}
r=requests.post('http://www.thewebsite.co.uk/members/dir/search', data=searchparams)
but it just returns the HTML of the search screen, not the results.
There are also a bunch of other options I could select, but these are the only ones I care about - do I need to submit values for them all?
The form HTML is below, for you information.
<form method="post" action="/members/dir/search" class="col-md-8">
<div class="form-group">
<label for="phone">Surname:</label>
<input type="text" name="name" value="" class="form-control" />
<div class="note">Leave blank to search for all surnames</div>
</div>
<div class="form-group">
<label for="grade">Grade:</label>
<select name="grade" class="form-control">
<option value="0">ALL</option>
<option value="G6">Grade 6</option>
<option value="G7">Grade 7</option>
</select>
</div>
<label>Results format:</label>
<div class="radio">
<label for="format">HTML</label> <input class="tick" type="radio" name="format" value="html" checked="checked" />
</div>
<div class="radio">
<label for="format">Excel</label> <input class="tick" type="radio" name="format" value="excel" />
</div>
<div class="radio">
<label for="format">CSV</label> <input class="tick" type="radio" name="format" value="csv" />
</div>
<div class="form-group">
<button type="submit" name="" class="btn btn-danger">Search</button>
</div>
</form>
I'm a bit confused with all the django stuff and forms, I hope you can help me.
I'm migrating a cmd line app to django, and I think I don't need to mess with models (db) atm. I just would like to pass the parameters filled in a form to a python script that use these fields as parameters. I would like to keep my html code and skip render the form with a python form class since it has a lot of css code inside each one.
At the moment I have this form:
<form class="nobottommargin" id="template-contactform" name="template-contactform" action="{% url 'get_data' %}" method="post">
{% csrf_token %}
<div class="col_half">
<label for="name">Project Name <small>*</small></label>
<input type="text" id="name" name="name" value="{{ current_name }}" class="sm-form-control required" />
</div>
<div class="col_half col_last">
<label for="group">Project Group <small>*</small></label>
<input type="text" id="group" name="group" value="{{ current_group }}" class="required sm-form-control" />
</div>
<div class="clear"></div>
<div class="col_half">
<label for="version">Project Version<small>*</small></label>
<input type="text" id="version" name="version" value="{{ current_version }}" class="required sm-form-control" />
</div>
<div class="col_half col_last">
<label for="pType">Project Type<small>*</small></label>
<select id="pType" name="pType" class="required sm-form-control">
<option value="">-- Select One --</option>
<option value="bundle">Bundle</option>
<option value="multiple">Multiple</option>
<option value="git">Git</option>
</select>
</div>
<div class="col_half">
<label for="pPath">Project path<small>*</small></label>
<input type="text" id="pPath" name="pPath" value="{{ current_path }}" class="required sm-form-control" />
</div>
<div class="clear"></div>
<div class="col_full hidden">
<input type="text" id="template-contactform-botcheck" name="template-contactform-botcheck" value="" class="sm-form-control" />
</div>
<div class="col_full" style="margin: 50px 0 0 0 ">
<button class="button button-3d nomargin" type="submit" id="template-contactform-submit" name="template-contactform-submit" value="submit">Create Project</button>
</div>
</form>
So, I want to figure out what's the best way to write the get_data function. I also need some sort of custom validation about some fields that involves other checks that exceeds the basic checks, such as max_chars on each field. That means I need to create some sort of my own form.is_valid() function.
Hope you can help me!
Regards