Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell if a form is an 'edit' form in a template?

I have a page with a form I'd like to use both for editing and adding. I'd also like to change out the copy on this page based on whether the form is being used for editing or adding.

When being used for editing an existing object, I'm simply creating the form object with form = MyForm(instance=existingObject).

I tried using {% if form.instance %} but this is true even for a form being used for adding (created with form = MyForm()).

Is there some way to tell if a form has an existing object in it from within a template?

like image 624
fredley Avatar asked Nov 15 '12 13:11

fredley


1 Answers

Instantly figured this out after submitting question:

{% if form.instance.id %} will only be true for an 'edit' form.

like image 152
fredley Avatar answered Sep 28 '22 08:09

fredley