Django form fields not rendering when grouping fields - python

I am using Django 4.0.3 with a bootstrap webinterface.
For layout reasons I want my fields in a ModelForm to be grouped and I'm doing it with:
class UserForm(ModelForm):
template_name = "container/form.html"
field_groups = [["email", "company"], ["last_name", "first_name"]]
grouped_fields = []
class Meta:
model = MyUser
fields = ["email", "company", "first_name", "last_name"]
__init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
def group_fields(self):
for group in self._field_groups:
group_entry = []
for entry in group:
group_entry.append(self.fields[entry])
self.grouped_fields.append(group_entry)
in the view I initialize my form and the regroup the fields:
def user_form(request):
form = UserForm()
form.group_fields()
render(request, "page.html, {"form", form})
The page.html looks like this:
<body>
<div id="form-wrapper">
{{ form }}
</div>
</body>
and the form.html looks like this:
<form action="" method="POST">{% csrf_token %}
{% for field_group in form.grouped_fields %}
<div class="row">
{% for field in field_group %}
<div class="col">
<label>{{ field.label }}</label> {{ field }}
</div>
{% endfor %}
</div>
{% endfor %}
However the rendered fields are displayed as string representations of the field objects:
Email <django.forms.fields.EmailField object at 0x7f98c00e03a0>
Company <django.forms.fields.CharField object at 0x7f98c00e0250>
Last name <django.forms.fields.CharField object at 0x7f98c00e0790>
First name <django.forms.fields.CharField object at 0x7f98c00e10c0>
Whereas a common call renders as expected a form with input fields, but not with the desired layout.
{% for field in form %}
<div class="row">
<div class="col">
{{ field.label_tag }} {{ field }}
</div>
</div>
{% endfor %}
I also tried to move the call of the group_fields method to the form init but had no success.
Is there a way to render the fields, that are stored in my grouped_fields container correctly?

After quiet a time of trying this and that I ended up writing a wrapper for ModelForm, define my field_groups there and rendered it in the template.
It is not nice, but it does the trick.
<form action="" method="post">
{% for fieldgroup in form.field_groups %}
<div class="row fieldgroup">
{% for fieldname in fieldgroup %}
{% for field in form %}
{% if field.name == fieldname %}
<div class="col pt-2">
<label class="form-label">{{ field.label }}</label>
{{ field }}
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
<button type="submit">Submit</button>
</form>

Related

django formset template loop through fields disables delete featuer

Thank you for being here.
I am not able to delete objects in django formset While i am looping through the fields in templates the.
I can see the deletion box but when i hit submit the page refreshes and the object is exist.
template.html
{% for form in request_equipment_form %}
<div class="card" style="width: 100% ">
<div class="card-body">
<div class="d-flex flex-row" style="width:100%">
{{form.equipment}}
{{form.quantity}}
{{form.DELETE}}
{{form.ORDER}}
</div>
</div>
{% endfor %}
But when i do not loop through the fields the delete feature works like charm
{% for form in request_equipment_form %}
<div class="card" style="width: 100% ">
<div class="card-body">
<div class="d-flex flex-row" style="width:100%">
{{form.as_p}}
</div>
</div>
{% endfor %}
views.py
if formset.is_valid():
instances = formset.save(commit=False)
for d_obj in formset.deleted_objects:
d_obj.delete()
if not instances:
return redirect(request.META.get('HTTP_REFERER'))
for instance in instances:
instance.user = request.user
instance.flight = flight
instance.station = flight.station
instances = formset.save(commit=False)
for instance in instances:
instance.save()
print(instance.date)
return redirect(request.META.get('HTTP_REFERER'))
When you loop through forms in a formset you need to include the the management form for things to work:
<form method="post">
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{{ field.label_tag }} {{ field }}
{% endfor %}
{% endfor %}
</form>
Also, you should consider using the CSRF token ({% csrf_token %}) to secure your form.

Filter templates by ChoiceField - Django

consider this model on Django:
class My_model(models.Model):
my_choices = { '1:first' 2:second'}
myfield1=CharField()
myfield2=CharField(choices=my_choices)
Then on my form:
class My_form(forms.ModelForm):
class Meta:
model = My_model
fields = ['myfield1', 'myfield2']
My views:
def get_name(request):
if request.method == 'POST':
form = My_form(request.POST)
if form.is_valid():
return HttpResponseRedirect('/')
else:
form = My_form()
return render(request, 'form/myform.html', {'form': form})
On my template:
{% extends "base.html" %}
{% block content %}
<form action="/tlevels/" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
On my base.html, I will load this template like this:
{% extends "base.html" %}
{% block content %}
{% load crispy_forms_tags %}
<div class="p-3 mb-2 bg-info text-white" style="margin-left:20px; margin-bottom:20px;">Status</div>
<div class="form-row" style="margin-left:20px; margin-bottom:20px; margin-top:20px;">
<div class="form-group col-md-6 mb-0">
{{ form.myfield1|as_crispy_field }}
</div>
<div class="form-group col-md-6 mb-0">
{{ form.myfield2|as_crispy_field }}
</div>
</div>
<input type="submit" class="btn btn-primary" value="Submit" style="margin-left:20px;">
</form>
{% endblock %}
What I want, is to have 2 other different templates, with whatever difference on them, and load them depending on the choice made on the ChoiceField, I guess that one way could be on the view, by adding some kind of conditional, and load a different template (html file).
Any ideas?
It is possible to use {% include %} with a variable.
def some_view_after_post(request):
# ... lookup value of myfield2 ...
return render(request, "path/to/after_post.html", {'myfield2: myfield2})
The in the after_post.html template:
<!-- include a template based on user's choice -->
<div class="user-choice">
{% include myfield2 %}
</div>
You'll want to make sure there is no possible way the user can inject an erroneous choice. For example, make sure the value of myfield2 choice is valid before adding it to the context.

Django Model Form validation doesn't show

I needed some validation on a Django ModelForm field. So I changed 2 lines in my models.py (just below). The validation is blocking as necessary, but I can't find the proper way to display the ValidationError. Maybe there is a cleaner way to do this in the model form ?
models.py
class Lexicon(models.Model):
[...]
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', _('Only alphanumeric characters are allowed'))
filename = models.CharField(_("Filename"), max_length=40, validators=[alphanumeric])
forms.py
class LexiconForm(forms.ModelForm):
class Meta:
model = Lexicon
fields = ['filename', 'language', 'comment', 'alphabet', 'case_sensitive', 'diacritics']
views.py
#login_required
def new_pls_view(request):
if request.method == 'POST':
form = LexiconForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
return redirect('pls_edit')
else:
form = LexiconForm()
return render(request, 'main/new_pls.html', {
'form': form,
})
template.html
<form class="form-horizontal" method="post" action="{% url 'new_pls' %}">
{% csrf_token %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
[...]
{% if form.is_bound %}
{% if form.filename.errors %}
{% for error in form.filename.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% endif %}
{% if form.filename.help_text %}
<small class="form-text text-muted">{{ form.filename.help_text }}</small>
{% endif %}
{% endif %}
{% render_field form.filename type="text" class+="form-control" id="plsFilename" placeholder=form.filename.label %}
Replacing my entire form by {{ form }} as #Alasdair suggested is working, so I guess something is wrong with my template rendering.
I've simply replaced my error printing by this and the error prints!
<form class="form-horizontal" method="post" action="{% url 'new_pls' %}">
{% csrf_token %}
{{ form.non_field_errors }}
[...]
{{ form.filename.errors }}
{% render_field form.filename type="text" class+="form-control" id="plsFilename" placeholder=form.filename.label %}

Django user selects multiple images from gallery and saves it as strings in database backend

Im trying to create a form where the user selects from a gallery of images and it saves it to the database. The code below currently renders some radio buttons in the html output. Is there anyway I can change these to images i have saved in a static directory so the user can click on images instead? Would be great if I could change what it saves in the database to what I needed instead of image urls as well. Theres lots of documentation on uploading images but not much I could find on selecting images. Im using django 1.9.7 and python 3.5
models.py
client_choices = (('client1', 'Client 1'),
('client2', 'Client 2'),
('client3', 'Client 3'),
('client4', 'Client 4'),
('client5', 'Client 5'))
class ClientSelect(models.Model):
client = MultiSelectField(choices=client_choices)
forms.py
from app.models import ClientSelect
class ClientSelectForm(forms.ModelForm):
class Meta:
model = ClientSelect
fields = '__all__'
views.py
class FormWizard(SessionWizardView):
template_name = "app/clientchoices.html"
#define what the wizard does when its finished collecting information
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
return render_to_response('app/about.html', {'form_data': form_data})
urls.py
url(r'^clientchoices$', FormWizard.as_view([ClientSelectForm]) , name='clientchoices'),
clientchoices.html
{% load staticfiles %}
{% block content %}
<section class="content">
<div class="container">
<div class="fit-form-wrapper">
<h2>Client Choices</h2>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
{% for field in form %}
{{field.error}}
{% endfor %}
<form action="{% url 'clientchoices' %}" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button class="btn btn-brand" name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">"First Step"</button>
<button class="btn btn-brand" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">"Previous Step"</button>
{% endif %}
<input class="btn btn-brand" type="submit" value="Submit" />
</form>
</div>
</div>
</section>
{% endblock %}
Any help is appreciated thank you

Django form wizard multiple formsets on one page

I am trying to create a form in Django with the form wizard where there are multiple (repeating) formsets in the same step of the wizard.
What I have is:
# forms.py
class FormStep1A(forms.Form):
answerA = forms.CharField(max_length=100)
remarkA = forms.CharField(widget=forms.Textarea)
class FormStep1B(forms.Form):
answerB = forms.CharField(max_length=100)
remarkB = forms.CharField(widget=forms.Textarea)
class FormStep1(forms.Form):
partA = formset_factory(FormStep1A)
partB = formset_factory(FormStep1B)
and
# urls.py
STEPS = (
('1', FormStep1),
# ...
)
# FormWizard is a subclass of NamedUrlSessionWizardView
wizard = FormWizard.as_view(STEPS, url_name='form_new_step')
urlpatterns = patterns(
'',
url(r'^new/(?P<step>.+)/$', wizard,
name='form_new_step'),
url(r'^new/$', wizard, name='form_new'),
)
And my template looks the same as in the documentation. However, my form is rendered empty with no fields at all.
I know there are similar questions or recipes out there:
Django formwizard with formsets and forms on same page
django wizard, using form and formset in the same step
Combining a Form and a FormSet in Django: How to pass them in the context?
But I just cannot get this to work. Any help is much appreciated, thank you!
UPDATE: My template looks like this:
{% extends 'base.html' %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<div class="row">
<div class="large-12 columns">
<h3>{% trans "Questionnaire" %}</h3>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">
{% csrf_token %}
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
{% if wizard.steps.prev %}
<button class="button tiny" name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "First step" %}</button>
<button class="button tiny" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "Prev step" %}</button>
{% endif %}
<input class="button tiny" type="submit" value="{% trans "Submit" %}"/>
</form>
</div>
</div>
{% endblock %}

Categories

Resources