Manual Django Form Display - django

I want to manually render the form in my template, but what I'm trying is not yielding the expected result, and it is not apparent as to why.
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['field1'] = forms.BooleanField()
self.fields['field2'] = forms.BooleanField()
systems = System.objects.all()
for i in range(len(systems)):
self.fields['s' + str(i)] = forms.BooleanField()
self.fields['field3'] = forms.BooleanField()
self.initial_fields = [self.fields['field1'], self.fields['field2']]
now when I do this in my template:
{% for field in form.visible_fields %}
{{ field }}
{% endfor %}
it returns what you would expect...after looking up the method visible_fields it simply returns a list of fields. So in theory, if I create my own list of fields as with self.initial_fields, the form generator should render the following same as above:
{% for field in form.initial_fields %}
{{ field }}
{% endfor %}
but instead I get this output in my template:
<django.forms.fields.BooleanField object at 0x000001242F51E588>
<django.forms.fields.BooleanField object at 0x000001242F51E400>
I'm assuming I'm missing some initialization of the field itself? I don't understand why one works and the other doesn't. Anyone know?

You need to get the bound field object and not the field itself. It's not really a clean way of doing so, but if you are looking to hack around it, you should do like so,
...
self.initial_fields = [self.fields['field1'].get_bound_field(self, 'field1'),
self.fields['field2'].get_bound_field(self, 'field2')]
...
Hope this helps!

Related

How to gain specific field value manually in django admin fieldsets

I'm overriding admin/includes/app-name/fieldset.html to change the behavior of some fields using the JavaScript.
In the original fieldset.html by django theres a loop to display the fields, But I want to pick the value of first field for some usecase. How can I do that
# I removed some code to make it simpler to read
{% for line in fieldset %}
{% for field in line %}
{{ field.field }}
{% endfor %}
{% endfor %}
I'm trying
{{ fieldset[0].field }}
But this gives the error:
Could not parse the remainder: '[1].field' from 'fieldset[1].field'
If i do that
{{ fieldset }}
This gives
<django.contrib.admin.helpers.Fieldset object at 0x000002873F9B1940>
How I can Pick the data of first field instead of running a loop, means I do not need iteration here, Just wants to pick values manually. I'm not fully aware of django template tags. So your help would be needed here.
See Edit, this is wrong
I believe it would be this: {{fieldset.0.0.field}}
Thought Process:
{% for line in fieldset %} means fieldset is an List
{% for field in line %} means line is an List
So we're looking at something like:
fieldset = [
[fieldObj0, fieldObj1], # Line 0
[fieldObj2, fieldObj3], # Line 1
]
Get Line 0 (first line):
{{fieldset.0}} = [fieldObj0, fieldObj1]
Get first fieldObj in Line 0:
{{fieldset.0.0}} = fieldObj0
Get field attribute of the first fieldObj in the first line
{{fieldset.0.0.field}} = fieldObj0.field
Hopefully that's correct (it was not)
Edit
Two Solutions
Manually Fetching field by Name: {{fieldset.form.actual_field_name_here}}
Lose the dynamic part of first field
Using Counter in Loop
{% for line in fieldset %}
{% for field in line %}
{% if forloop.parentloop.first and forloop.first %}
first line + first field: {{ field.field }}
{% else %}
{{ field.field }}
{% endif %}
{% endfor %}
{% endfor %}
Things I've learned
After looking at the source code, fieldset actually uses an __iter__ method and that's why my original stuff didn't work.
And What is extra annoying is they create the Fieldline Object in the __iter__ method instead of just generating them beforehand and throwing it into an attribute. This means the only way you can access those objects is during the loop. This extends to the Fieldline object which does the same thing with AdminField
contrib\admin\helpers.py
class Fieldset:
def __init__(self, form, name=None, readonly_fields=(), fields=(), classes=(),
description=None, model_admin=None):
self.form = form # Is Django Form
self.name, self.fields = name, fields # Fields is an array of strings
## more stuff - chopped out
def __iter__(self):
for field in self.fields:
yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin)
class Fieldline:
def __init__(self, form, field, readonly_fields=None, model_admin=None):
self.form = form # A django.forms.Form instance
if not hasattr(field, "__iter__") or isinstance(field, str):
self.fields = [field]
else:
self.fields = field
## more stuff - chopped out
def __iter__(self):
for i, field in enumerate(self.fields):
if field in self.readonly_fields:
yield AdminReadonlyField(self.form, field, is_first=(i == 0), model_admin=self.model_admin)
else:
yield AdminField(self.form, field, is_first=(i == 0))

WTForms - Pass argument to form then create fields if variable exists

Depending on the arguments I pass to the form I want to return different form fields.
class TestForm(FlaskForm):
"""Test Form."""
if one:
field1 = StringField('Field 1')
if two:
field2 = StringField("Field 2")
if three:
field3 = StringField("Field 3")
submit = SubmitField("Add Service")
def __init__(self, one=None, two=None, three=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.one = one
self.two = two
self.three = three
I am not able to see the arguments when doing the if statements.
I am aware of the option to have logic in html when rendering the form, however due the nature of the project have opted to use quick_form on the html side.
Here is the html code I am using.
{% import 'bootstrap/wtf.html' as wtf %}
<h3 >Add Service</h3>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
</div>
Instead of creating this logic in your form class. I would recommend to create all the fields you need and then dynamically choose which to show the user using jinja2 in your html file.
Here's an example.
{% for fields in fields_list %}
{% if field == '1' %}
{{ form.field1.label(class="form-control-label") }}
{{ form.field1(class="form-control form-control-lg") }}
{% endif %}
{% if field == '2' %}
{{ form.field2.label(class="form-control-label") }}
{{ form.field2(class="form-control form-control-lg") }}
{% endif %}
{% if field == '3' %}
{{ form.field3.label(class="form-control-label") }}
{{ form.field3(class="form-control form-control-lg") }}
{% endif %}
{% endfor %}
And then when you render or redirect to your .html from your routes code, don't forget to send
the proper arguments, such as
# create your fields list, which do I want to show?
# Make it a list of integers
fields_list = [1, 2, 3]
return render_template('insert-name.html', fields_list , form=form)
If my answer helped you, please consider accepting my answer.
I am new to this site and trying to build up some reputation :)
Thank you! Happy Coding!
If someone ever comes accross the same question, here is a quick solution I came upon some times ago.
It is in part inspired from the accepted answer here.
In your forms.py or wherever you declare your TestForm class, put the class declaration inside a function that takes your parameter as an argument and returns the class object as output.
The argument will now be accessible within the class itself, allowing for any test you may want to perform.
Here's a working example based on your original question (I just added a default value to get at least one StringField in case the parameter is ommited):
def create_test_form(var='one'):
class TestForm(FlaskForm):
"""Test Form."""
if var == 'one':
field1 = StringField('Field 1')
if var == 'two':
field2 = StringField("Field 2")
if var == 'three':
field3 = StringField("Field 3")
submit = SubmitField("Add Service")
return TestForm()
Then simply create the form in your routes like so:
form = create_test_form('two')
Finally pass it to your HTML to render the form with quick_form like you did.
This example will render a form with a single StringField named "Field 2" and a "Add Service" submit button.

Manually render Django form fields with variable label

I am creating forms with dynamic fields (and field names) using the below code
class BuyItemForm (forms.Form):
def __init__(self, inventory_list, *args, **kwargs):
super(BuyItemForm, self).__init__(*args, **kwargs)
for item in inventory_list:
self.fields["count%s"%item.item_name] = forms.IntegerField()
self.fields["price%s"%item.item_name] = forms.FloatField()
So I get a form that has field names like "counteggs", "priceeggs", "countmilk", etc... when these items are in the inventory_list
I now want to render the fields manually in my template. I am able to iterate through the set of fields, for example
{% for field in form %}
{{ field }}
{% endfor %}
But I am unable to pick out each field individually by using the field name in a string. I have tried
{{ form.fields['counteggs'] }}
but this doesnt work. Any ideas how I can make this work?
Did you try {{ form.fields.counteggs }} ? In the templates, dictionaries are accessed with dot notation.
So, I found a rather convoluted way of doing this by creating a filter in views.py that receives the form and a key as parameters. It iterates through the form till a field that has a label matching the key is found.
#register.filter
def get_countitem(myform, key):
for field in myform:
if field.label.lower() == key.lower():
return field
return "none"
and in the template
{{ form|get_countitem:"counteggs" }}
It works. I can get my template to render form fields by passing the field label in a string but doesnt seem like a particularly elegant solution to me. Any other ideas are most welcome.

How to target a specific object with Django templates

I have a class called Features in my models.py. In my html, I am displaying a list on the right that excludes two of these Features, one is the active feature that has been selected, the other is the most recently added since they are the main content of my page. The remaining Features in the list are displayed by date and do show what I am expecting.
Now, I want to single out the first, second and third Features (title only) in THAT list so I can place them in their own separate divs - because each has unique css styling. There are probably numerous ways of doing this, but I can't seem to figure any of them out.
This is a link to my project to give a better idea of what I want (basically trying to get the content in those colored boxes on the right.)
I'm just learning Django (and Python really), so thanks for your patience and help!
HTML
{% for f in past_features %}
{% if f.title != selected_feature.title %}
{% if f.title != latest_feature.title %}
<h1>{{ f.title }}</h1>
{% endif %}
{% endif %}
{% endfor %}
VIEWS
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
past_features = Feature.objects.order_by('-pub_date')
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
MODELS
class Feature(models.Model):
title = models.CharField(db_index=True, max_length=100, default='')
content = models.TextField(default='')
pub_date = models.DateTimeField(db_index=True, default=datetime.now, blank=True)
def __str__(self):
return self.title
def __iter__(self):
return [
self.id,
self.title ]
You can either store the first three Features in separate variables in your context or add checks to your template loop like {% if forloop.first %} or {% if forloop.counter == 2 %}.
If all you want is to not have the
selected_feature
latest_feature
these two records out of the past_features queryset, then you can use exclude on the past_features query and pass the id's of the selected_features and latest_feature objects.
The views.py would look like:
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
# Collect all the id's present in the latest_feature
excluded_ids = [record.pk for record in latest_feature]
excluded_ids.append(selected_feature.pk)
#This would only return the objects excluding the id present in the list
past_features = Feature.objects.order_by('-pub_date').exclude(id__in=excluded_ids)
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
Django provides a rich ORM and well documented, go through the Queryset options for further information.
For access to a specific object in Django templates see following example:
For access to first object you can use {{ students.0 }}
For access to second object you can use {{ students.1 }}
For access to a specific field for example firstname in object 4 you can use {{ students.3.firstname }}
For access to image field in second object you can use {{ students.1.photo.url }}
For access to id in first object you can use {{ students.0.id }}

In Django, why does my form context variable disappear?

Take this example form.
class TestForm(forms.Form):
def __init__(self, *args, **kwargs):
super(TestForm, self).__init__(*args, **kwargs)
self.fields["test"] = forms.CharField()
In my view, I have this code:
test_form = TestForm()
context['test_form'] = test_form
If I print(test_form), it will appear just fine. Now, when I use {% debug %} in my template, this is the output: { ... 'test_form': , ... }. In other words, the form disappears. I suspect it has something to do with adding a field after initialization, but I would really like to know why? Also how to get around the seeming limitation, if possible.
To test if your form has disappeared, you can try displaying it:
{% for field in test_form %}
{{ field }}
{% endfor %}
This should display the each field of the form as HTML. Hope it helps!
Your contact variable hasn't disappeared: it's right there, in the output you show us. If it had disappeared, you wouldn't see the test_form key in that dictionary.
As to why you don't see a value for that key, that's simply because the default representation of a python object is like this <TestForm object at ....>, and your browser is interpreting the angle brackets as an unknown HTML tag which it ignores.
Put your debug tag within <pre>...</pre> tags and you'll see it just fine.