I hava a form in my template which is for search.I did not made any form class to it. Is it possible to have access form data in view or should I make a form class to it.
<form class="navbar-form" role="search" action="{% url 'my_url_name' %}" method="get">
<div class="input-group add-on">
<input class="form-control" placeholder="search" name="srch-term" id="srch-term" type="text">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
I use this form for its style and I can not make this style with Form class
You can access form data in the request.GET (or request.POST if it is a post request) dictionary in your view. For example:
srch_term = request.GET.get('srch-term')
You can by using the QueryDict object you get with the request :
def myview(request):
if request.method == "POST":
data = request.POST
print(data['foo']) # <--Will print the value I entered into my form.
[...]
But be careful with this : There is no validation tool to ensure the data you receive is correctly formated.
Related
I am working on a weather app where the client will be entering the name of the city in a search bar, and the name will be used to gain weather data from an API. To get the name of the city from the form, I have created this condition in view:
if request.method == 'POST':
city = request.data
print(city)
however when being printed, this error occurs: 'WSGIRequest' object has no attribute 'data'
What am I doing wrong? Or is there a completely different way to do this?
you must get data from template like this: (name of function in views.py is something same as in url.py.
def something(request):
if request.method == 'POST':
city = request.POST.get('data')
print(city)
data is the name of your input tag in template:
<input name='data'>
Update:
you need to have a action for your form. it includes a url. this url connect you to your view function for this form.
also you must add name to your input tag not your form tag.
template:
<form method="POST" action = "{% url "url_something" %}" >{% csrf_token %}
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" type="text" placeholder="City Name" name="data" >
</div>
<div class="control">
<button class="button is-info" type="submit">
Search City
</button>
</div>
</div>
</form>
in url.py you must have a url like this.:
url(r'^something', views.something, name='url_something'),
So I am building an app that users are able to use a range slider to update a database with a new integer value. I have the slider which provides me with the integer value that I would like to return to the view, but I don't know how to return this value from the template.
https://docs.djangoproject.com/en/3.0/ref/forms/widgets/
I looked at the documentation but I couldn't find anything regarding range inputs.
# forms.py
from django import forms
class UpdateForm(forms.Form):
update = forms.IntegerField(widget=forms.HiddenInput(**something like:value='slider-input**}}))
# views.py
def home_view(request):
form = UpdateForm()
return render(request, 'main/index.html', {'form': form})
# index.html
...
<form id="update-form" method="post">
{% csrf_token %}
{{ form }}
<div id="slider">
<input class="slider-widget" min="0" max="25" step="1" value="0" type="range">
</div>
<div class="options">
<a href="{% url 'main-home' %}">
<img src="{% static 'main/media/cancel.png' %}" alt="cancel">
</a>
<button type="submit">
<img src="{% static 'main/media/confirm.png' %}" alt="confirm">
</button>
</div>
</form>
...
In it's simplest form you can take the slider value directly and do not need a HiddenField.
Your simple form
class UpdateForm(forms.Form):
update = forms.IntegerField()
In your html template just make sure that you add the 'name' attribute to your slider and give it the same name as the field name in your form:
<div id="slider">
<input name="update" class="slider-widget" min="0" max="25" step="1" value="0" type="range">
</div>
and in your view just process the form:
form = UpdateForm(request.GET or None)
if form.is_valid():
slider_value = form.cleaned_data['update']
# do somthing
Actually you do not really need a form for a simple case like this. You can also fetch the value directly in your view
slider_value = request.GET.get('update')
I need to create function based or class based view which can edit/update multiple forms in one page. How to create this?
You should be able to have more than one form appear on a template by passing two different form variables in the view. Something like this:
def formview(request):
if request.method == 'POST'
form1 = form.Form1()
form2 = form.Form2()
context = {'form1': form1, 'form2': form2}
Then in your template you simply need to handle each form within the form tags like so:
<form action="" method="post">
{% csrf_token %}
{{ form1.as_p }}
{{ form2.as_p }}
<input class="btn btn-primary" type="submit" value="Submit">
</form>
Hi i am using django + bootstrap4 to render forms. I have 'submit' and 'cancel' buttons on the forms. i am using ModelForm with Validators assigned to most of the form attributes.
template file
<form action="{% url 'actor-create' %}" method="post" class="w-25 mx-auto">
{% csrf_token %}
{% bootstrap_form form layout="horizontal" %}
<button class="btn btn-primary" type="submit"><i class="fas fa-plus"></i> Save</button>
<button class="btn btn-primary" type="submit"><i class="fas fa-times"></i> Cancel</button>
</form>
in the view
def actor_create(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
print(request.POST)
if "cancel" in request.POST:
return HttpResponseRedirect('/')
..... rest of the code
When cancel button is pressed validation of the form attributes prevents the form from submitting. so view functionality never gets executed.
I want to know how to avoid validation when form is cancelled?
Following Q&A has a JavaScript based solution, I preferably don't want to write such code for every form in my website.
How to cancel form submission?
as suggested by Iain Shelvington making it a 'a' worked for me!
<i class="fas fa-times"></i> Cancel
The following is all I have for the client server
<div>
<form action="/next/" role="form" method="POST">
{% csrf_token %}
<textarea class="form-control" rows="10"></textarea>
<input type="submit" value="submit"/>
</form>
</div>
and from the server(django) I just want to process the form.
def next(request):
request.body // nothing here except CSRF
request.POST // nothing here except CSRF
It's embarrassing, but what am I doing wrong?
It is not embarrassing. you just forgot name attribute in form element that you want to catch in backend.
<textarea name="element_name" class="form-control" rows="10"></textarea>
and in views.py
def next(request):
if request.method == "POST":
textarea_value = request.POST.get('element_name') # viola!
You forgot to add name attribute to the text area:
<textarea name="some_name" class="form-control" rows="10"></textarea>
Add a "name" attribute to the textarea ?