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 )
Related
Question in the title.
I have a form like so
class SelectTypeForm(forms.Form):
the_type = forms.CharField(max_length=100)
I have a custom template with a radio button group. How can make the field get the selected value?
Go away from your CharField. Hopefully I understand you correctly and you want to have a radio-button-group with multiple options. Each option represents a string. If the user decides to click the second presented radio-button, the string 'is_stored' gets stored.
class SelectTypeForm(forms.Form):
choose_type = forms.ChoiceField(label='your type field', choices=(
('type_a', 'This string'),
('type_b', 'is stored'),
('type_c', 'if selected')
), widget=forms.RadioSelect, initial='type_b')
If this is not what you meant, please provide your 'custom template with a radio button group'.
I am using form with django, I succeeded to create the form, but I don't know how to change position of label of my input to the right, because by default label is in the center.
This is an example of I have : picture
code :
input= forms.CharField(
label="myLabel",
required=True,
widget=forms.TextInput(attrs={'class':'form-control','maxlength':'30'}))
I will suggest you render the form fields individually at the template level:
Django Docs
First thing, you are using 'form-control' just in your input field, but this class is to use in the entire form. Using as you did you're just applying the features only in your input field
I suggest you use django-bootstrap4 library to render your forms. and select a class for your label as showed in the documentation here
Thanks for your feedbacks, I fixed the issue by adding myLabel<\div>
I am trying to add a extra column to one of my tables, that adds url to another page.
My Table:
class ItemTable(tables.Table):
edit = tables.LinkColumn('item_edit', args=[A('pk')])
class Meta:
model = Item
fields = ('name', 'slot', 'klass', 'rarity', 'price')
my urls:
url(r'^admin/item/edit/(?P<item_id>\d+)/$', views.item_edit, name='item_edit')
Now with this, i get my table, but the last column (edit) only has dashes + the page crashes when i click the title.
i have been looking at http://django-tables2.readthedocs.org/en/latest/#django_tables2.columns.LinkColumn and im not sure where i go wrong
The problems you've encountered are caused by LinkColumn expecting to be bound to a specific attribute in your Item model, i.e. it is looking for an Item.edit attribute on your instances.
Since you don't actually have an Item.edit attribute, ordering over your edit column makes no sense, and you should mark it as non-orderable:
from django_tables2.utils import A
edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False)
The text of the link itself would come from the value of the Item.edit attribute, which you don't have, so you will need to provide it yourself by adding a render_edit method to your table class:
def render_edit(self):
return 'Edit'
You can replace the 'Edit' string with whatever you want displayed in that column.
Update: As suggested by #SunnySydeUp, you also need to specify empty_values=() for the column, in order to get its value rendered:
edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False, empty_values=())
References:
http://django-tables2.readthedocs.org/en/latest/pages/order-by-accessors.html#specifying-alternative-ordering-for-a-column
http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html#table-render-foo-methods
Disclaimer: This answer is based on the django-tables2 documentation and source code, and hasn't been tested on an actual Django application.
To have the link properly formatted and with a link text of your choice, you can do the following in the table class:
def render_edit_link(self,record):
return mark_safe('<a href='+reverse("edit", args=[record.pk])+'>Edit</a>')
Where 'edit' is the name of the url.
I create clickable links in extra columns with
edit = tables.LinkColumn('item_edit', text='Edit', args=[A('pk')], \
orderable=False, empty_values=())
It's not necessary to override the render method; the 'text' parameter changes the text of the link from 'none' to 'Edit', for example.
I'm new to programming so this may be a trivial question...
In django-tables2, I'd like to be able to display the column header name when using CheckBoxColumn. Right now, all the checkboxes are displaying for each row, including in the header. I don't mind having a checkbox in the header (I figure that would be a great way to do a "select all" in the long run), but I need the column name to display. Does anyone have a solution for this?
Create your own custom checkbox column class that inherits from tables.CheckBoxColumn
then override the render method, then specify the check box together with its label as html response.
class CustomCheckBoxColumn(tables.CheckBoxColumn):
def render(self, value, record, bound_column):
return mark_safe(u'column Name<input type=checkbox, … />')
Another option is to use the TemplateColumn() instead of CheckBoxColumn()
template = '<input type="checkbox" name="{{record.name}}" />'
checkbox_column_header = tables.TemplateColumn(template)
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.