dpbklyn
dpbklyn

Reputation: 791

Django two views on a webpage

Hello and thank you in advance,

I am very much a Django/Python noobie. I just need guidance, not necessarily answers. I have read all the pertinent documentation and I can't seem to find a concise example of what I am trying to do.

I have two views, one is a form and the other is a view that lists data contained in a database table. I am trying to display both these views on the same webpage page that is called by the user going to one URL listed in the URLS.py file. I am sure this is possible, I am equally sure I am missing something basic.

Thank you for your help.

dp

Upvotes: 2

Views: 1451

Answers (1)

jterrace
jterrace

Reputation: 67063

I usually try and define my views in small pieces. For example, the form might be something like this:

<!-- form.html -->
<form><input type="submit"/></form>

and the model listing is this:

<!-- listing.html -->
<ul>
{% for item in items %}
<li>Item {{ item.name }}</li>
{% endfor %}
</ul>

Then, the page that is just the form would look like this:

<!-- form_page.html -->
<html><body>
{% include "form.html" %}
</body></html>

The page with just the listing:

<!-- listing_page.html -->
<html><body>
{% include "listing.html" %}
</body></html>

and the page with both:

<!-- both_page.html -->
<html><body>
{% include "form.html" %}
{% include "listing.html" %}
</body></html>

This is a very simplified example, but hopefully you get the idea.

Upvotes: 5

Related Questions