[Answer]-Django – what is the most commonly accepted way of including a form within another template?

1👍

The {% include %} tag doesn’t mean “include this view + template”; it means “include the template itself, just as if you copied and pasted the template text directly into here, nothing more and nothing less.

Therefore, it’s not using your contact view at all.

Do you need the contact view to be a separate view? (i.e., will you use this as an independent page ever?) If not, I’d put the logic of your view into whatever view drives your homepage.

If you do need a separate contact view and to get this on your homepage, I’d try to factor out the form-handling stuff so that you can call it from both the homepage view and the independent contact view. I think that kind of thing is often easier with Class-Based Views, myself. If factoring it out is too difficult for you, you could just copy and paste this same stuff into the home page–that violates the principle of “Don’t repeat yourself”, but it’s a short amount of pretty straightforward code; this might be the lesser of two evils if the alternative is confusing for you.

Good luck!

Leave a comment