Display empty default field in Django form - django

I would like to initialize my form with a default empty value.
In my views.py i have one like so:
form = BookingForm(initial={'name':''})
and in the template like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value={{ form.name }}/></td></tr>
The output however, in the html input field shows '/' instead of the placeholder.
Any ideas how to do this?

Try adding quotes around your {{ form.name }} like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value="{{ form.name }}"/></td></tr>
and if that doesn't change anything you can try in your forms.py:
name = forms.CharField(initial='')
rather than trying to set the default input value upon instantiation.

Related

Django formset - custom input HTML

I need to expand a formset's field, so instead of
{{ form.name }}
I'm using something like
<input type="text" name="{{ form.name }}" ... >
My custom implementation does not print the formset prefix, so the HTML I get is
<input type="text" name="name" ... >
But what I would need to have the form working properly with formset is
<input type="text" name="attachments-3-name" ... >
where attachments-x is automatically added.
How can I get that?
I noted there's an helper for ID (auto_id) which prints something similar: id_attachments-3-name; is there something similar for names?
Looks like {{ form.name.html_name }} is what I've been looking for the last 2 hrs.

how to create same name submit in flask-wtf?

i want to create two same name submit in html like this
<input type="submit" name="key" value="up">
<input type="submit" name="key" value="down">
but i want to use flask-wtf to do it, i don't know how to create Class?is like this?
class NameForm(FlaskForm):
submit = SubmitField('up')
submit = SubmitField('down')
No. Doing it like that will simply overwrite the class attribute submit. Do it like this:
class NameForm(FlaskForm):
key = SubmitField('not_used_string')
Then in your html after return render_template('page.html', form=form) you render it like this:
{{ form.key(value='up', id="A1") }} # -> will render <input id="A1" name="key" type="submit" value="up">
{{ form.key(value='down', id="A2") }} # -> will render <input id="A2" name="key" type="submit" value="down">
You don't have to supply id's but if you don't they will both be key.
Note that in order to have the same name you can only have one class attribute with that name.

Passing a variable from a form input to a flask URL

I have a dummy project in Flask, which consists in a web form, where you fill in 3 cities, which are then printed upon submition.
This is my init.py file:
#app.route('/average_temperatures/<city1>/<city2>/<city3>', methods=['POST', 'GET'])
def results_temperature(city1, city2, city3):
return "The cities are: {}, {}, {}".format(city1, city2, city3)
The function works, but I am unable to pass the variables from the form straight into the function, as parameters.
edit:
My goal is to have the city variables as part of the URL in the clean form of /city1/city2/city3.
This is the form:
<div class="input-group">
<form action="{{ url_for('results_temperature', city1=city1, city2=city2, city3=city3) }}" method="POST">
<input type="text" class="form-control" placeholder="City 1" name="city1"></input>
<input type="text" class="form-control" placeholder="City 2" name="city2"></input>
<input type="text" class="form-control" placeholder="City 3" name="city3"></input>
<div class="row" style="margin-bottom: 20px;"></div>
Filling out the form results in a URL
http://example.com/average_temperatures///
So I evidently fail to pass the form fields in the form action part.
Any hits are appreciated, cheers.
You should get the cities from the request.form variable.
#app.route('/average_temperatures', methods=['POST', 'GET'])
def results_temperature():
return "The cities are: {}, {}, {}".format(request.form['city1'], request.form['city2'], request.form['city3'])
In your HTML form:
<form action="{{ url_for('results_temperature') }}" method="POST">
<input type="text" class="form-control" placeholder="City 1" name="city1"></input>
<input type="text" class="form-control" placeholder="City 2" name="city2"></input>
<input type="text" class="form-control" placeholder="City 3" name="city3"></input>
</form>

Display the post data after error in django and html5

django form
{% for field in form.fields %}
{{field}}
</div>
{% endfor %}
If there is an error {{field.email}} will output this html with a post data value
<input id="id_email" type="text" value="gffdg" />
I want to use HTML 5 inputs but don't know how to get the post value if there is error
<input id="id_email" type="email" >
edit..
I was hoping not to use widgets with my django forms and just type the html5 code in my template (type="email" not type="text")
<input id="id_email" type="email" >`
but can't figure out how to get the value back after a post with errors.
<input value="?????" />
If you are trying to get the bound value of the email field, then the following template code should work:
<input id="id_email" type="email" value="{{ form.email.value }}">
If the value is not set, then it will be a blank field.

How can I access data sent in a post request in Django?

I have a form that is supposed to create a new 'Quote' record in Django. A 'Quote' requires a BookID for a foreign key.
This is my form
<form method="POST" action="{% url 'quotes:createQuote' %}">
{% csrf_token %}
<section>
<label for="q_text">Quote Text</label>
<input type="text" name="text" id="q_text" placeholder="Enter a Quote" style="padding-left:3px"> <br>
<label for="q_book">Book ID</label>
<input type="text" name="bookID" id="q_book" placeholder="Enter Book ID" style="padding-left:3px"> <br>
<label for="q_disp">Display Quote Now?</label>
<input type="radio" name="display" id="q_disp" value="True"> True
<input type="radio" name="display" value ="False">False <br>
<button value="submit">Submit</button>
</section>
</form>
And this is the method that it is targeting
def createQuote(request):
#b = get_object_or_404(Book, pk=request.bookID)
return HttpResponseRedirect(reverse('quotes:index'))
Somewhere in that request argument I assume there is some sort of field that contains the bookID the user will pass in on the form. How do I get at that information?
Bonus points for anyone who can tell me some way I can visualise data like I might with console.log(some.collection) in Javascript
if request.method == "POST":
book_id = request.POST['book_id']
Assuming you're sure it's in there. Otherwise you'll need to verify/provide a default value like you would for a normal python dictionary.
As for visualising the data, do you mean printing it to the console? In which case if you're running the django runserver you can just do print some_data. If you want it formatted a little nicer, you can use pretty print:
import pprint
pp = pprint.PrettyPrinter()
pp.pprint(some_data)