Django two views on a webpage

99封情书 提交于 2019-12-22 11:28:11

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/9255002/django-two-views-on-a-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!