I'm using a flask-wtf submit button as follows:
{{ wtf.form_field(form.submit, button_map={'submit': 'primary'}) }}
I want to add a class to the submit button but I can not find anything about that,
I'm pretty new with this but this http://pythonhosted.org/Flask-Bootstrap/basic-usage.html#templates did not helped me a lot.
Anything to recommend?
It's super-easy, usually, so unless Flask-Bootstrap does something odd you can just tell it what you want the class to be:
{{ wtf.form_field(form.submit, class="something", button_map={'submit': 'primary'}) }}
Generally, anything you pass to the rendering function that isn't recognised will be added as a parameter to the html, so you can do things like:
{{ wtf.form_field(form.submit, cats="mew") }}
And your resulting html field would be something like <input cats="mew" id="name" name="name" type="text" value="">
Related
in forms.py
class PlaceOrder(forms.ModelForm):
class Meta:
model = Order
fields = ["Product_ID","HowMany","DateSubmit",]
to call a form i usually use
{{ Form }}
that will render the form automatically
but is it possible to make it manually?
for example i want the form to be exactly like this
<input id="Product_ID" type="hidden" value="{{ Product.PId }}" >
<input id="HowMany" type="text">
<input id="DateSubmit" type="hidden" value="{{ date }}" >
i have tried wrapping them in
{% for field in form %}
but it gave the wrong output
Sorry if this is confusing
but i don't really know how to explain it,
i am still new to Django
You should be able to access individual fields in the form using something like
{{Form.Product_ID}}
which will give you the Product_ID field widget.
Now if you want to access the already pre-filled data you should be able to do so with
{{Form.Product_ID.label}}
or
{{Form.Product_ID.value}}
Also check:
https://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
I have a WTF form with several fields where I only want to display certain fields based on the responses to previous fields without refreshing the page. For example, say I have a field:
URLInd = BooleanField(u'Do you have a website?',validators=[DataRequired())
Which if checked, I want to display:
URL = TextField(u'Please enter your web address',validators=[DataRequired(), URL()])
So "URLInd" can be shown at the initial page load, but I only want "URL" shown if URLInd == True.
HTML:
<form class="form form-horizontal" method="post" role="form" enctype=multipart/form-data action>
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.URLInd) }}
{{ wtf.form_field(form.URL) }}
<button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>
Based on my research, I suspect I need to use Javascript and AJAX, but I have no experience using either and I haven't been able to apply any of the examples I've found to this problem. Thanks in advance for your help.
Field have arg message for replace default error message in defaults validators. For replace use kwargs message="u error message for view invalid this field"
Simple example
URLInd = BooleanField(u'Do you have a website?',validators=[URL(message="I want MY MESSAGE ERROR THIS FIELD")])
I'm looking at a package called crispy forms I was wondering if it was possible to change the html name from within the template?
i.e.
{{ form.username|changename }}
Rules:
I don't want to change the original form or view (at all), I only have access to the template.
Since you're changing the label, I suggest you do it template-ish, without using a templatetag or a filter:
<label for="id_username">Your label name here:</label>
{{ form.username }}
Or, if you can't resist, you have to apply a filter to your field.label property:
{{ form.username.label|my_custom_filter }}
A simple filter could be:
#register.filter(name='my_custom_filter')
def my_custom_filter(value):
# here value is the value of your label
# process it as you please then return the new value
if value == 'changeme':
return 'changed label'
return value
This is just an example to show you how a template filter works, you have then to elaborate it and change name, logic etc etc
This could be over simplifying it, but what not just raw output it?
<label for="">what ever you want</label>
<input type="text" name="{{ form.username.html_name }}" id="{{ form.username.auto_id }}">
I would still do this in the form.py myself but if all you have is the template to work with then it does not need anything fancy here.
I have a BooleanField in a standard Django Form defined as:
my_boolean_field = BooleanField(initial=True)
when I render my form with initial data, and my_boolean_field is set to an initial value of False, when the form renders it is still checked despite the fact that the value is false as the html shows:
<p><label for="id_2">my_boolean_field</label>:
<input checked="checked"
type="checkbox"
name="2"
value="False"
id="id_2" />
</p>
Has anyone else experienced this, or knows how to fix it so that when the initial data/input value is false then the checkbox is not checked?
UPDATE: Even if I remove the initial=True argument from the BooleanField the same thing happens.
Firstly, you can't have required=False on a BooleanField, use NullBooleanField instead:
https://docs.djangoproject.com/en/dev/ref/models/fields/#booleanfield
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.NullBooleanField
let me know what the HTML output of the new widget is once you've done this?
EDIT
I assumed that this is a ModelForm. Is this a regular django.forms.Form?
EDIT 2
My test form:
from django import forms
class MyForm(forms.Form) :
"""The test form"""
my_boolean_field = forms.BooleanField(initial=True)
My template:
<div>
{{ form.my_boolean_field.label_tag }}
{{ form.my_boolean_field }}
{{ form.my_boolean_field.errors }}
</div>
This is what I get for output when I view page source. EDITED
<div>
<label for="id_my_boolean_field">My boolean field</label>
<input type="checkbox" name="my_boolean_field" id="id_my_boolean_field" />
</div>
My View NEW
form = MyForm(initial={'my_boolean_field':False})
What you are showing and what I'm seeing aren't matching up. paste your full form, view and template please?
EDIT 3
I only see output like this:
<div>
<label for="id_my_boolean_field">My boolean field</label>
<input checked="checked" type="checkbox" name="my_boolean_field" value="False" id="id_my_boolean_field" />
</div>
when I put False in quotes:
form = FormLogin(initial={'my_boolean_field':"False"})
I'm posting this answer because I had the same problem and Francis's answer didn't help me.
I eventually found the solution was to re-run manage.py syncdb. My fields had previously been IntegerField and I had converted them to BooleanField. BooleanField read the presence of value="0" in the database as being initialized and checked. This is regardless of what your model says. You want to see:
<input checked="checked" id="id_cost_track_destruction" name="cost_track_destruction" type="checkbox">
when you inspect an element. Since this bug shows up in the Django Admin panel, it has nothing to do with your forms.py.
This is purely a model and database issue. Try regenerating your database if you've changed your models.py field types.
For those who are using ModelForms, this is what worked for me:
password_required = forms.CheckboxInput(attrs={'checked' : ''})
This correctly generates the checkbox with it unchecked.
Is it possible to have some of the new attribute only attributes used in HTML5, inside of WTForms?
Eg, say you want to create a TextField with placeholder="foo", required, and autofocus attributes. How would this be done in WTForms?
In html it would look like this: <input maxlength="256" name="q" value="" placeholder="foo" autofocus required>
Note that placeholder="foo" is easily done in WTForms. autofocus and required, because they have no value, are .. well, as far as i've seen, not supported in WTForms.
Can WTForms support this?
In WTForms 1.0, released yesterday, HTML5 compact syntax is now the default. Now you can do (in jinja):
{{ form.field(autofocus=true, required=true, placeholder="foo") }}
Note that in Jinja, the literal is true instead of True but if you were to try this in the python console you will need to use the python literal True for this to work.
In WTForms 0.6.x, which used XHTML as the default output, you could do e.g.
{{ form.field(autofocus="autofocus", required="required", placeholder="foo" }}
This is the recommended way for representing boolean attributes in XHTML, and this happens to still be 100% valid HTML5 and completely equivalent, though the HTML generated is a bit more verbose.
I'm new with WTForms but it seems to me that the solution could be improved instead of using :
{{ form.field(autofocus=true, required=true, placeholder="foo") }}
use :
{% if field.flags.required %}
{{ field(autofocus=true, required=field.flags.required, placeholder="foo") }}
{% else %}
{{ field(autofocus=true, placeholder="foo") }}
{% endif %}
WTForms seems not to handle correctly required=false for 100% HTML5 and sets in the HTML an attr required="False" instead of removing the attr. Could this be improved in next version of WTForms?
You'll probably need to create a custom widget.
Check out the docs on custom widgets.