16👍
One way to do this without displaying your form in the template using form.as_ul, is with django-uni-form. First you’ll have to download it here and install it. Then the code for setting up your form could looks something like this:
from uni_form.helpers import FormHelper, Submit, Layout, Fieldset
class MyForm(Form):
#personal data
firstname = CharField()
lastname = CharField()
#education data
university = CharField()
major = CharField()
#foobar data
foobar = ChoiceField()
# now attach a uni_form helper to display the form
helper = FormHelper()
# create the layout
layout = Layout(
# first fieldset
Fieldset("Here you enter your personal data...",
'firstname', 'lastname'),
Fieldset("Here you enter your education data...",
'university', 'major'),
Fieldset('foobar')
# and add a submit button
sumbit = Submit('add', 'Submit information')
helper.add_input(submit)
Now, to display this in your template you would do this:
{% load uni_form %}
{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}
This would output HTML (roughly) like this:
<form>
<fieldset><legend>Here you enter your personal data...</legend>
<input name='firstname'>
<input name='lastname'>
</fieldset>
<fieldset><legend>Here you enter your education data...</legend>
<input name='university'>
<input name='major'>
</fieldset>
<fieldset>
<input name='foobar'>
</fieldset>
</form>
For more info on uni_form, read their docs (see the link above).
PS: I realize this reply is late, and I’m sure you already solved this problem, but I think this should be helpful for anyone just coming across this now.
11👍
If you want to customize the form, you don’t have to render it form.as_ul
. Django knows how to render foobar if you have set up the forms model properly…try it…no worries.
Look at what python on the server sent your page. For example if it sent a django form like this:
return respond(request, user, 'templateName', { 'myform':myform })
then templateName.html will have:
<blockquote>
<form>
<p>Here you enter your personal data...</p>
{{myform.firstname }}
<p>Here you enter your education data...</p>
{{myform.university }}
<p> a choice field </p>
{{myform.foobar}}
</form>
</blockquote>
- How can I send signals from within Django migrations?
- Return list of objects as dictionary with keys as the objects id with django rest framerwork
4👍
Since each section is a collection of multiple, independent form fields, I recommend using a custom form template. This gives you absolute full control over the layout with minimal extra work. Django’s Customizing the Form Template docs have the details.
- ValueError: Field 'id' expected a number but got 'Processing'
- How to solve PytestConfigWarning: Unknown config option: DJANGO_ SETTINGS_MODULE error?
2👍
Remember also that a Django Form object is just a collection of fields; there is no need for a 1:1 correspondence between HTML form tags and Django Form objects. If the various sections of the form are actually logically separate, you could consider splitting it up into three Forms, which you could then render in your template with any HTML you want between them (but still within a single HTML form tag).
Whether this is a sensible solution depends quite a bit on the design of your app and the view, of course.
- Django 1.8 migration unable to cast column id to integer
- Does Django use processes or threads to handle user requests in view?
- Grouping dates in Django
- Testing for empty/null string in django
- Logging from Django under UWSGI
2👍
There is a help_text
field in forms you know.
in forms.py:
myField = forms.myFieldType(help_text="Helping friendly text to put in your form", otherStuff=otherStuff)
in forms.html:
{{form.myField.help_text}}
- Registering Django system checks in AppConfig's ready() method
- Django not sending error emails – how can I debug?
- How to fetch data server-side in the latest Next.js? Tried getStaticProps but it's not running and getting undefined
- Django create superuser from batch file
0👍
Even though your form is populated from a database, you should still be able to manually write out the form, or use a template. Check out the Django form documentation for more details.
- Django form with ManyToMany field with 500,000 objects times out
- Confusion in Django admin, static and media files
- Difference in sequence of query generated in Django and Postgres for select_for_update
0👍
For those into a similar situation as the author, I recommend falling back to CSS and the :before
and/or :after
pseudoselectors either on the input
or the label
elements of the form. They work just as well and could make your life a lot easier as you can still use {{ form.as_p }}
.
- Python: How can I override one module in a package with a modified version that lives outside the package?
- Django admin many-to-many intermediary models using through= and filter_horizontal
- Client-Side (JavaScript) Django/Jinja-like template inheritance
- How to change my django server time
0👍
Solution
The most simple way to display some free text in between django form fields is to use the django-crispy-forms
package which is very easy to use for controlling the rendering behavior of your Django forms (see Crispy docs).
Using the Layout
class of this package (see Crispy/Layouts docs) allows you to change the way the form fields are rendered. In this Layout class you can use the HTML
layout object which lets you render the text however you want.
Code
To obtain the desired result, your class definition should look like this:
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, HTML
class MyForm(Form):
# personal data
firstname = CharField()
lastname = CharField()
# education data
university = CharField()
major = CharField()
# foobar data
foobar = ChoiceField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
HTML('<p>Here you enter your personal data...</p>')
Field('firstname'),
Field('lastname'),
HTML('<p>Here you enter your education data...</p>')
Field('university'),
Field('major'),
HTML('<p>Here you select...</p>')
Field('foobar')
)
Which will render this HTML:
<form>
<p>Here you enter your personal data...</p>
<input name='firstname'>
<input name='lastname'>
<p>Here you enter your education data...</p>
<input name='university'>
<input name='major'>
<p>Here you select...</p>
<input name='foobar'>
</form>
-1👍
agreed with @mipadi, seems it’s the easiest way.
So something like
$('.selector').append('<html></html>')
- Django drf simple-jwt authentication"detail": "No active account found with the given credentials"
- Test if ValidationError was raised
- Django – include app urls
- Django 1.3 logging: 500 errors are not logged
- Django and Dropzone.js