refer to ModelField property in template - django

There is a possibility in Django templates to refer to attributes of the field directly in a template. For example to .help_text or .label. Like that:
{% for field in form %}
{{field.label|safe}}
{{field}}
{% endfor %}
What is the right way to refer to a custom defined field property?
For example:
{{field.myproperty}}
I use ModelForm and in models.py I use my own ModelField. Everything works perfectly but any attempts to add my own property fail. In a sense that everything is ok but if I refer to this property in a template it just doesn't get it :-(
Models.py:
class MyFormField(forms.IntegerField):
MyProperty = 'whatever'
def __init__(self,active1='default',*args, **kwargs):
self.MyProperty = 'whatever'
super(MyFormField, self).__init__(*args, **kwargs)
class MyOwnField(models.IntegerField):
def __init__(self, active1='asdf',*args, **kwargs):
super(MyOwnField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
defaults = {'form_class': MyFormField}
defaults.update(kwargs)
return super().formfield(**defaults)
class MyModel(Model):
ns6_1 = MyOwnField()

Firstly, case is significant; field.myproperty is not the same as field.MyProperty.
Secondly, when you iterate through a form you don't directly get the form fields, you get BoundField instances. Each of these has a field property which is the field itself. So:
{{ field.field.MyProperty }}

Related

Django Crispy Forms CustomLayout with Fieldset

So I want to build a custom layout, that extends LayoutObject for a form that I have.
class NewBookForm(LayoutObject):
template = 'library/layouts/new_book_layout.html'
def __init__(self, fields, template=None, *args, **kwargs):
self.fields = fields
# Overrides class variable with an instance level variable
if template:
self.template = template
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs):
fields = self.get_rendered_fields(form, form_style, context, template_pack, **kwargs)
template = self.get_template_name(template_pack)
return render_to_string(template, {'fields': fields})
And I'm calling it using
self.helper.layout = Layout(
NewBookForm(Fieldset('book_id', 'name', 'author'))
)
Right now Django is saying "template does not exist."
However, this is not getting me the result I'm looking for.
UPDATE 1:
Now that library/layouts/new_book_layout.html has something like
<div class="w-40 mr-2">
{{name|as_crispy_field}}
{{name.errors.as_ul }}
{{author|as_crispy_field}}
{{author.errors.as_ul }}
</div>
I'm now getting the error:
CrispyError at library/layouts/new_book_layout.html
|as_crispy_field got passed an invalid or inexistent field
and highlighted:
{{name|as_crispy_field}}
That is because once you call get_rendered_fields it returns an string object not a crispy object, so instead of using |as_crispy_field filter, you should use the |safe filter since what your context contains is an HTML string.

Django: ModelChoiceField as MultipleHiddenInput

I am trying to render a ModelChoiceFiel using the MultipleHiddenInput widget but the template does not generate any inputs at all.
Here is what I am trying:
class PresetSelectForm(forms.Form):
presets = forms.ModelChoiceField(queryset=Preset.objects.none(), widget=forms.MultipleHiddenInput())
def __init__(self, presets, *args, **kwargs):
super(PresetSelectForm, self).__init__(*args, **kwargs)
self.fields['presets'].queryset = presets
And in the template I am using the following:
{% csrf_token %}
{{ form }}
The csrf token input is generated, but nothing is for {{ form }}.
What am I missing?
Edit: I am including the essential and relevant code I am using in my view. It's just a FormView, so the form object is automatically created.
class PresetSelectView(FormView):
form_class = PresetSelectForm
The overriden methods are: get_form_kwargs, get_context_data, form_valid, form_invalid and dispatch.
I guess it's worth mentioning that I am not using the form instance anywhere in these methods except in form_valid, where I am getting form.cleaned_data['presets'] but not modifying form at all.
Here is the overriden get_context_data method:
def get_context_data(self, **kwargs):
request = self.request
context = super(PresetSelectView, self).get_context_data(**kwargs)
# now some lines retrieving models
# then we attach some additional attributes to the instances of some of these models (these attributes will be used in the template)
context.update({...})
return context

How to programmatically access {{ form.my_field.id_for_label }}?

My Django project uses Bootstrap v4.
I iterate through all the fields to add the HTML attribute class="form-control". I also need to add an HTML attribute aria-describedby="{}Help" where {} is the id_for_label for the widget.
In the template language I could print that value easily with {{ form.my_field.id_for_label }} but programmatically I don't know how to do it.
I tried it with ModelForm().fields['…'].widget.id_for_label() but that method expects an argument. And by lookind at the Django source code, the return value is literally the argument.
Any ideas?
class AbonnentForm(ModelForm):
class Meta:
model = Abonnent
fields = '__all__'
def __init__(self, *args, **kwargs):
super(AbonnentForm, self).__init__(*args, **kwargs)
# Add Bootstrap class to all <input> elements.
for key, value in self.fields.items():
value.widget.attrs.update({'class': 'form-control'})
if value.help_text:
id_for_label = "{}Help".format(value.widget.id_for_label())
value.widget.attrs.update({'aria-describedby': id_for_label})
self.fields['erste_ausgabe'].widget.attrs.update({'placeholder':'MM/JJJJ'})
self.fields['letzte_ausgabe'].widget.attrs.update({'placeholder':'MM/JJJJ'})
You can contruct it yourself like this:
for key, value in self.fields.items():
value.widget.attrs.update({'class': 'form-control'})
if value.help_text:
django_id_for_label = self.auto_id % key # self.auto_id == 'id_%s'
my_id_for_label = '{}Help'.format(django_id_for_label)
value.widget.attrs.update({'aria-describedby': my_id_for_label})

how to set form field value - django

I want to show empty form field if there is nothing to show, otherwise a form field with the value inside:
{% if somevalue %}
{{form.fieldname}} #<---- how do i set the `somevalue` as value of fieldname here?
{% else %}
{{form.fieldname}}
{% endif %}
In your view, if it is a class-based view, do it like so:
class YourView(FormView):
form_class = YourForm
def get_initial(self):
# call super if needed
return {'fieldname': somevalue}
If it is a generic view, or not a FormView, you can use:
form = YourForm(initial={'fieldname': somevalue})
There are multiple ways to provide initial data in django form.
At least some of them are:
1) Provide initial data as field argument.
class CityForm(forms.Form):
location = ModelChoiceField(queryset=City.objects.all(), initial='Munchen')
2) Set it in the init method of the form:
class CityForm(forms.Form):
location = ModelChoiceField(queryset=City.objects.all())
def __init__(self, *args, **kwargs):
super(JobIndexSearchForm, self).__init__(*args, **kwargs)
self.fields['location'].initial = 'Munchen'
3) Pass a dictionary with initial values when instantiating the form:
#views.py
form = CityForm(initial={'location': 'Munchen'})
In your case, I guess something like this will work..
class CityForm(forms.Form):
location = ModelChoiceField(queryset=City.objects.all())
def __init__(self, *args, **kwargs):
super(JobIndexSearchForm, self).__init__(*args, **kwargs)
if City.objects.all().exists():
self.fields['location'].initial = ''
else:
self.field['location'].initial = City.objects.all()[:1]
That all is just for demonstration, you have to adapt it to your case.

django - dynamic form fieldsets

A form will be spitting out an unknown number of questions to be answered. each question contains a prompt, a value field, and a unit field. The form is built at runtime in the formclass's init method.
edit: each questions receives a unique prompt to be used as a label, as well as a unique list of units for the select element.
this seems a case perfect for iterable form fieldsets, which could be easily styled. but since fieldsets - such as those in django-form-utils are defined as tuples, they are immutable... and I can't find a way to define them at runtime. is this possible, or perhaps another solution?
Edit:
formsets with initial_data is not the answer - initial_data merely enables the setting of default values for the form fields in a formset. a list of items can't be sent to the choicefield constructor by way of initial_data.
...unless I'm wrong.
Check out formsets. You should be able to pass in the data for each of the N questions as initial data. Something along these lines:
question_data = []
for question in your_question_list:
question_data.append({'prompt': question.prompt,
'value': question.value,
'units': question.units})
QuestionFormSet = formset_factory(QuestionForm, extra=2)
formset = QuestionFormSet(initial=question_data)
Old question but I am running into a similar problem. The closest thing that I have found so far is this snippet based of a post that Malcom did a couple years ago now.
http://djangosnippets.org/snippets/1955/
The original snippet did not address the template side and splitting them up into fieldsets, but adding each form to its own fieldset should accomplish that.
forms.py
from django.forms.formsets import Form, BaseFormSet, formset_factory, \
ValidationError
class QuestionForm(Form):
"""Form for a single question on a quiz"""
def __init__(self, *args, **kwargs):
# CODE TRICK #1
# pass in a question from the formset
# use the question to build the form
# pop removes from dict, so we don't pass to the parent
self.question = kwargs.pop('question')
super(QuestionForm, self).__init__(*args, **kwargs)
# CODE TRICK #2
# add a non-declared field to fields
# use an order_by clause if you care about order
self.answers = self.question.answer_set.all(
).order_by('id')
self.fields['answers'] = forms.ModelChoiceField(
queryset=self.answers())
class BaseQuizFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
# CODE TRICK #3 - same as #1:
# pass in a valid quiz object from the view
# pop removes arg, so we don't pass to the parent
self.quiz = kwargs.pop('quiz')
# CODE TRICK #4
# set length of extras based on query
# each question will fill one 'extra' slot
# use an order_by clause if you care about order
self.questions = self.quiz.question_set.all().order_by('id')
self.extra = len(self.questions)
if not self.extra:
raise Http404('Badly configured quiz has no questions.')
# call the parent constructor to finish __init__
super(BaseQuizFormSet, self).__init__(*args, **kwargs)
def _construct_form(self, index, **kwargs):
# CODE TRICK #5
# know that _construct_form is where forms get added
# we can take advantage of this fact to add our forms
# add custom kwargs, using the index to retrieve a question
# kwargs will be passed to our form class
kwargs['question'] = self.questions[index]
return super(BaseQuizFormSet, self)._construct_form(index, **kwargs)
QuizFormSet = formset_factory(
QuestionForm, formset=BaseQuizDynamicFormSet)
views.py
from django.http import Http404
def quiz_form(request, quiz_id):
try:
quiz = Quiz.objects.get(pk=quiz_id)
except Quiz.DoesNotExist:
return Http404('Invalid quiz id.')
if request.method == 'POST':
formset = QuizFormSet(quiz=quiz, data=request.POST)
answers = []
if formset.is_valid():
for form in formset.forms:
answers.append(str(int(form.is_correct())))
return HttpResponseRedirect('%s?a=%s'
% (reverse('result-display',args=[quiz_id]), ''.join(answers)))
else:
formset = QuizFormSet(quiz=quiz)
return render_to_response('quiz.html', locals())
template
{% for form in formset.forms %}
<fieldset>{{ form }}</fieldset>
{% endfor %}
I used the trick below to create a dynamic formset. Call the create_dynamic_formset() function from your view.
def create_dynamic_formset(name_filter):
"""
-Need to create the classess dynamically since there is no other way to filter
"""
class FormWithFilteredField(forms.ModelForm):
type = forms.ModelChoiceField(queryset=SomeType.objects.filter(name__icontains=name_filter))
class Meta:
model=SomeModelClass
return modelformset_factory(SomeModelClass, form=FormWithFilteredField)
Here is what I used for a similar case (a variable set of fieldsets, each one containing a variable set of fields).
I used the type() function to build my Form Class, and BetterBaseForm class from django-form-utils.
def makeFurnitureForm():
"""makeFurnitureForm() function will generate a form with
QuantityFurnitureFields."""
furnitures = Furniture.objects.all()
fieldsets = {}
fields = {}
for obj in furnitures:
# I used a custom Form Field, but you can use whatever you want.
field = QuantityFurnitureField(name = obj.name)
fields[obj.name] = field
if not obj.room in fieldsets.keys():
fieldsets[obj.room] = [field,]
else:
fieldsets[obj.room].append(field)
# Here I use a double list comprehension to define my fieldsets
# and the fields within.
# First item of each tuple is the fieldset name.
# Second item of each tuple is a dictionnary containing :
# -The names of the fields. (I used a list comprehension for this)
# -The legend of the fieldset.
# You also can add other meta attributes, like "description" or "classes",
# see the documentation for further informations.
# I added an example of output to show what the dic variable
# I create may look like.
dic = [(name, {"fields": [field.name for field in fieldsets[name]], "legend" : name})
for name in fieldsets.keys()]
print(dic)
# Here I return a class object that is my form class.
# It inherits from both forms.BaseForm and forms_utils.forms.BetterBaseForm.
return (type("FurnitureForm",
(forms.BaseForm, form_utils.forms.BetterBaseForm,),
{"_fieldsets" : dic, "base_fields" : fields,
"_fieldset_collection" : None, '_row_attrs' : {}}))
Here is an example of how dic may look like :
[('fieldset name 1',
{'legend': 'fieldset legend 2',
'fields' ['field name 1-1']}),
('fieldset name 2',
{'legend': 'fieldset legend 2',
'fields' : ['field 1-1', 'field 1-2']})]
I used BetterBaseForm rather than BetterForm for the same reason this article suggests to use BaseForm rather than Form.
This article is interesting even if it's old, and explains how to do dynamic forms (with variable set of fields). It also gives other ways to achieve dynamic forms.
It doesn't explain how to do it with fieldsets though, but it inspired me to find how to do it, and the principle remains the same.
Using it in a view is pretty simple :
return (render(request,'main/form-template.html', {"form" : (makeFurnitureForm())()}))
and in a template :
<form method="POST" name="myform" action=".">
{% csrf_token %}
<div>
{% for fieldset in form.fieldsets %}
<fieldset>
<legend>{{ fieldset.legend }}</legend>
{% for field in fieldset %}
<div>
{% include "main/furniturefieldtemplate.html" with field=field %}
</div>
{% endfor %}
</fieldset>
{% endfor %}
</div>
<input type="submit" value="Submit"/>
</form>