I am trying to use django forms, and I am interested in rending a form with " blank label".
Something like:
class SearchForm(forms.Form):
q = forms.CharField(required=True,widget=forms.TextInput(attrs {'id':'field','name':'field'}),label="Search")
and then I render the form in my html using
{{form.as_p}}
However, I have this annoying "Search:" being displayed on my html, which I dont want. I have tried using just:
q = forms.CharField(required=True,widget=forms.TextInput(attrs {'id':'field','name':'field'}))
but this outputs "Q:", which I guess is the default label. How do I tell django that I do not need the label rendered?
Many thanks.
Check out this example, it should clarify how to use it: https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template
Are you sure {{ form.q }} does not work?
You could just do:
class SearchForm(forms.Form):
q = forms.CharField(required=True,widget=forms.TextInput(attrs {'id':'field','name':'field'}),label="")
That will just set the label to the empty string.
Related
I like Django form render(‘form’: form) and template {{ form }}, but it doesn’t allow you to arrange fields. I was just wondering if there was an easier way to do it, without rendering the whole form manually.
What Django does:
First Name
Middle Name
Last Name
What I want to do:
First Name Middle Name
Last Name
What makes Django {{ form }} so great is that it puts field labels and help text and all that jazz in the right places. Where as manually, you have to put all that in. If there isn’t an easier way, I’m ok with that, but I thought I would at least ask.
You could try to render individual fields of the form, like this part of the docs shows.
I am using django widget tweaks for displaying and tweaking my fields in django templates, every thing is working fine but for a select box when I render it, its initial displayed value is -----, where as I want to show my custom value instead of -----, like --Select a Country--
My syntax looks like this:
{{ form.countries }} // countries is a choice field, therefore it is displayed as a select box
If any change is required I do it like this:
{{ form.countries|attr:"class:span6" }} //just for an example
Please help me doing this,
I have tried this:
{{ form.countries|attr:"option:--Select a Country--" }}
Please help me out!
Editing would be appreciated!
Thanks.
This should be done in the view where you initialize your form using the initial attribute.
So something like:
countries = forms.ChoiceField(
choices=YourCountriesGoesHere,
initial='--- Select a country ---')
You can read the documentation here
I am fiddling around with Django forms and Django Crispy Forms (using Bootstrap as my theme). I defined two fields:
test1 = forms.BooleanField(label='Some label here', help_text='help text here')
and
test2 = forms.CharField(label='another label', help_text='more help text')
When I render my form, the test2 field shows the label like:
another label: <input box>
with 'more help text' underneath.
However, for the test1 (BooleanField), the label seems to change the value displayed after the text box, i.e.
[] 'Some label here'
help text here
Is there a way to make it display more like:
Some label here []
help text here
Thanks!
I ended up using a custom template for certain fields, such as:
Field('my_field', template='my_field_template.html')
In my_field_template.html, I was able to specify the order I wanted.
Use can create the other way
my_field= forms.ChoiceField(choices=(('A','A'),),widget=forms.CheckboxSelectMultiple, required=False )
I am using a TextArea provided by Flask-WTF.
numbers = forms.TextField('Numbers', [forms.validators.Required()], widget=forms.widgets.TextArea())
I want to change cols of the resulting fields to say 80. How do I accomplish this. I don't want to do it in template and would like to do this in form.
Django provides:
name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})
I want something similar.
You can do it during output in your html
like: form.numbers(cols=20)
Oh, sorry did not notice that you mentioned template.
Update:
Actualy also had this problem, my solution was to extend Form class and add dictionary to form objects.
So all custom attributes go into that dictionary and used on output. This can be done with custom template macros or redefining render methods
In django admin I am using the textarea widget.
When I save data, it is saved inside a <p></p> tag.
I dont want this - any solutions, I just want to get the data out without being wrapped in a <p>.
Any suggestions?
I don't know how to save it without html coding, but maybe it is enough to you to display without that . Just put a |safe in the template after the variable name, example:
{{ variable|safe }}
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs