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>
Related
I have a field in my Flask WTForm which uses a SelectMultipleField. I'm using a frontend library which renders the choices fine from the db and adds them to the input field as tags when I select them (like the tags do on this website when creating a question!).
However the data is not being saved, form.field.data and request.form.getlist('field') etc all show None. If I add option_widget=widgets.CheckboxInput to the SelectMultipleField, I can select and save data.
So what I'm wondering is, do I need make a custom field or widget in order for the form to use the selected options as the form data (for example, instead of checking if the field has been checked, it checks if it's in the input field). Going a bit mad reading all the documentation so grateful for a hint in the right direction! Code below:
field = SelectMultipleField(
"fieldname",
validators=[Optional()],
widget=widgets.ListWidget(prefix_label=False),
# option_widget=CheckboxInput(),
)
please try this way
from flask_appbuilder.fieldwidgets import Select2ManyWidget
"users": QuerySelectMultipleField(
_("group.user"),
query_factory=lambda: db.session.query(User),
widget=Select2ManyWidget(),
default=[],
),
The result will look like the image below
Currently implementing searchbox which provides two interface - textbox if required input is not normalized, and dropdown if required input is normalized and is defined with choices param in model, and select class in form.
Current model(partial):
id = models.CharField(args)
submitter = models.CharField(args)
experiment_type = models.CharField(args, choices=<tuple with choices>)
Current form.Meta.widgets for model above:
'id': TextInput(attrs={'id':'exp_id', readonly: ''})
'experiment_type': Select(attrs={'id': 'experiment_type', required=''})
I've currently found that I can retrieve each fields' html snippet by doing str(field), for field in form, and with beautifulsoup or other html parsing library I can find input type(is text field or dropdown(select)), and option values for dropdown.
But it seems too compilcated, and I'm currently guessing that there might be
some way django provides to determine such things.Looking forward, and thanks in advance for any help.
Is there any simple way for removing the labels under forms for New resource in RailsAdmin?
I have "Optional. Length up to 255." under most of the fields although those are mandatory and it's confusing to end users.
I don't have any manually added models, I just use the default config:
config.actions do
dashboard
index
new
export
bulk_delete
show
edit
delete
show_in_app
end
Thanks in advance!
rails_admin looks at model validations to determine the help text for each field. Since your fields are being marked Optional, I'm guessing they're mandatory by some other means.
You can turn off the help messages using the help false statement. If you'd like to do it for all fields on the form, you can use the fields method:
config.model 'Bug' do
edit do
fields do
help false
end
end
end
RailsAdmin determines whether a field is optional via the presence validation in the model.
To remove the optional label, add this line into your model:
validates_presence_of :attribute1, :attribute2
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 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