I'm fairly new to Django so please bear with me.
I'm using a ModelForm along with Django-Material (By Viewflow) and it's working really well expect for one thing. It doesn't display checkboxes.
My 'Person' model has a ManyToMany relationship with 'Problem' and I've set the form to show checkboxes as so -
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ('',)
widgets = {
'problems': forms.CheckboxSelectMultiple(),
}
But it shows up like this
When I remove the Material template the checkboxes show up properly
I'm not really sure if there's a problem with Django-Material or something I may have missed. Help would be really appreciated, thanks!
Actually it was not implemented in time when question was.
Not it works as expected and in addition could be rendered in several columns
http://forms.viewflow.io/demo/hospital/
Alright, so it turned out that they haven't included that functionality in Django-Material yet
Related
I'm looking to understand a way to change attribute of my html code generate by the django's template system, especially for Form html code.
So I found some stuff to change class from some models like that
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name', 'title', 'birth_date')
widgets = {
'name': Textarea(attrs={'cols': 80, 'rows': 20}),
}
coming from here.
It's fine and it could works. But actually django, if I understand almost well, use a templates system which can be read by someone else, a front-end developper for a simple example. Now, Boostrap come whith the need to manipulate class attribute to work on the style. When we work on a loop, it's not a problem, but when whe generate the HTML code from a class who inerhit Form and just display it in a template, it will be be quickly harder for someone else to manipulate html balise. For sure they can do that with javascript, it's the solution that I've in mind, but it seem not really nice. There is an alternativ way to do that ?
Thanks for suggestion, I hope I was relatively clear.
I installed django haystack using whoosh. Everything works great, but I want to alter the names displayed next to the check boxes. I know they are generated using the verbose name set up on the models but I still have an issue with the 's' being added at the end of the names. I know there are custom forms and custom views but I am new to programming and some of the concepts do not make sense. I have also tried to search for any ideas but have had no luck. Any suggestions/advice?
Thanks in advance!
:)
is it not the label=_("Field_name") parameter in the checkbox field?
if its about the verbose name there is also verbose_name_plural which can be set up in models
I'm trying to populate a django table with autocomplete light so that the user can fill in data in the table, which would then be saved (the whole table is in a form tag). I have the table working to display the existing data and I have the autocomplete working in model forms (well, a team member got that part working), but I don't know how to combine the two. The docs are a bit of a mystery to me, but maybe if someone could at least point me in the right direction I'd greatly appreciate it.
I've tried a few random things to combine them, but honestly they were such stabs in the dark that I don't think they're even worth mentioning.
tables.py
class ModifyTable(tables.Table):
name = tables.LinkColumn('app-view', args=[A('pk')], verbose_name='Name')
primary_contact = tables.Column()
secondary_contact = tables.Column()
autocomplete
autocomplete_light.register(Person,
search_fields=['first_name', 'last_name', 'username'],
split_words=True,
autocomplete_js_attributes={'placeholder': 'Find a user',},
)
Django-tables2 provides an API to generate data tables in HTML.
Django-autocomplete-light provides a widget that enables autocompletion inputs.
This widget must be used in a Form. The django Form class will combine your the HTML <form> with models used by django-tables2.
However, a Form must be used by a Formsets to be repeated for every row in the table. Note that you could consider modelformset_factory to generate such a formset.
Use a formset and your work is done here ;)
This is really frustrating,
I can't and can't find how to create a form (I'm guessing a forms.Form form) to update just one field of a more complex model.
The model has 5 fields, and a form to create, update all of them.
But in a different case i need to let the user update only the title (a field in the model), so i need tried so many things until now (including creating an HTML form by hand and from the view to save it, creating a forms.Form and many more, nothing seem to work), There is no code here because i don't even know which one to put....
Maybe some one can help me with that, I'm sure it is a simple thing, But for some reason i am stuck on this for a long time...
Thank you,
Erez
If you're using ModelForms, you just have to define a fields attribute in Meta as a tuple containing just the names of the fields you want. See the documentation.
I have a Django form that uses a different number of fields based on the year/month. So I create the fields in the form like this:
for entry in entry_list:
self.fields[entry] = forms.DecimalField([stuffhere])
but now I don't know how to get the submitted data from the form.
Normally I would do something like:
form.cleaned_data["fieldname"]
but I don't know what the names of the fields are. The debug screen shows my POST data as simply "Entry Object" with a value of "u''". Calling POST.lists() doesn't show anything.
I am sure I am missing something obvious, but I've been stuck on this for a few days too many. Is there a better way to do this? Is all of the data in the request object, but I just don't know how to use it?
Here is the code for the model/form/view: http://pastebin.com/f28d92c0e
Much Thanks!
EDIT:
I've tried out both of the suggestions below. Using formsets was definitely easier and nicer.
I think you might be better off using formsets here. They're designed for exactly what you seem to be trying to do - dealing with a variable number of items within a form.
In this line:
self.fields[entry] = forms.DecimalField(max_digits=4, decimal_places=1, label=nice_label)
entry is a model instance. But fields are keyed by field names (strings). Try something like:
self.fields[entry.entry_name] = forms.Decimal(...)
(substitute appropriate for "entry_name").