I have a formset that adds new forms by javascript in diferent order in the page. someones in the bigining and others in the end. But when i save, the errors are shows in order of form-n form-n+1, etc. no in the order that i put in the hiddenfield form-N-ORDER.
how i cant show in template ordered forms by the form-N-ORDER? because in the view, i cant use:
for form in formset.ordered_forms:
but ordered_forms not work in template.
this is the problem:
I want to add span tags after input tags in forms to show icons or so. the idea is in Django the forms is just rendered automatically by calling the form's context name in a template tag so is there any way I can add tags to each or only one tag without rendering the whole form manually?
can I add anything in the forms.py file to make these changes?
I suggest you to use Crispy Forms, this plugin has nice features and more DRY than editing manually the style of django form: https://django-crispy-forms.readthedocs.io/en/latest/
Is there a way to submit simultaneously two forms connected through a foreign key in django?
I need to send them in order, so the best idea is to use synchronous option, right?
Any ideas? A simple example would be very useful!
Edit: Each form is on a different tab in the template. I have 5 tabs so I have five form tags.
You can post 2 forms at the same time using prefixes. Let's say you have these models:
Post => Category and you want to save a post and a category at the same time.
In your view:
post_form = PostForm(prefix="post", ...)
category_form = CategoryForm(prefix="category", ...)
...
if post_form.is_valid() and category_form.is_valid():
...
In your template you just show them within the same form tag.
I have 2 forms in my Django view. How can I do a check to see which one has been submitted?
Thanks
Put a different name attribute on the submit buttons for each form, then check for that key in request.POST in your view.
Also don't forget to give each form a separate prefix attribute when you instantiate them, to avoid any possible field name collisions.
Here are some ideas:
Use different action URLs for the forms, associated with different views.
Use different action URLs for the forms, associated with the same view but with using different parameters to the view (using the URLconf)
Use an <input type="hidden" /> to differentiate between the forms.
Philip
I'm wrestling with how to best create HTML pages in Django that can either be used for displaying or editing data. That is, I'd like the field's values to appear as text in display mode, but in their widgets when in edit/add mode. It appears that Django wasn't designed to do this: the fields always appear in their widgets (eg, text input, text area,
etc).
Is there a common technique for handling this, short of using forms for one, and not the other?
I was thinking of a custom templatetag filter that could be used for every form field, like:
{{ form.field_name|render_field:mode }}
where render_field would either return the field's HTML widget, or just the value as text, based on the mode.
Have I missed something, or is this a viable solution?
Answering my own question, apparently. I ended up with a three-part solution:
attach the model to the form, so I'll have the default display widgets for each field
write the form using a template tag, passing it the form.field, user, and action
write a render template tag to handle #2
Step one:
form.model = Model(...)
Step two:
{{form.field1.label}}
{% render form.field1 user action %}
{{form.field2.label}}
{% render form.field2 user action %}
Step three:
Something like:
def render(formfield, user, action, default_text="Private"):
if not user.is_authenticated():
action = "view"
if action == "view":
if user.is_authenticated():
fieldname = formfield.name
retval = str(getattr(formfield.form.model, fieldname))
else:
retval = default_text
else:
retval = formfield.as_widget()
return retval
Since you are saving the data, you must have a model attached to the form somehow, a modelform or not. So you can just use that model directly to get the values and render it in a template like you want.
The above suggestion would be possible, but since forms can be rather complex, it's probably not an easy task or worth the bother. Depends on how often you want to do this. But then it would probably be easier to create a filter for the model instead of the form.
Have I missed something
Forms not only display the field widgets but also handle the post data. A post sent would cause it to process the data cleanup, form and field error handling, etc.
It's kind of breaking the pattern - why create and render a form object only to tweak it not to look like a form?
If you are worried about too much work on templates, try to solve it with template inheritance in a best possible way. If you are very sure you want to change only the field tag, you can make sth like
{% if form %}
{% for error in form.field.errors %}
{{ error|escape }}
{% endfor %}
{{ form.field }}
{% else %}
{{ object.field }}
{% endif %}
for every field, but IMO that's not the point, YMMV.
Also what comes to mind (thinking of your solution) is dynamically attach widgets to form fields, but that'd be overengineering.
I have the same problem. Right now I have separate templates for display and editing; the former renders object fields, the latter form fields (but sometimes also object fields, for things which are not editable). The HTML structure can be quite complex: for example, on some pages I have large tables representing a multi-level hierarchy of objects. As a result, I end up with a large amount of duplicated code in the two templates, which is the opposite of DRY.
I have used template filters with form fields before, to save code when displaying errors together with the field. I think it would be a viable solution in this case. Another possibility may be to use a ModelForm subclass which can be told to render uneditable versions of the fields. This would help keep the templates simple. You could even render both the static and the editable version of a field, and use JavaScript to switch between them, activating and deactivating an editing mode.