我试图同时在同一页面上显示两个不同的视图。如果我注释掉user_creation
网址,则会显示userlist
视图。否则,如果两个网址都被取消注释,则首先列出的网址将指示哪个视图可见(在本例中为user_creation
)。
如何同时在同一页面上显示两个视图?
(PS:我是Django的初学者)
编辑:由于我不能有两个观点,我将描述我的目标。我想显示:
同样,我显示的代码分别用于各个目的,但我无法同时显示这两个部分/模块。
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'users.views.user_creation', name='user_creation'),
url(r'^$', 'users.views.userlist', name='userlist'),
url(r'^admin/', include(admin.site.urls)),
)
views.py
from django.shortcuts import render_to_response, RequestContext
from django.http import HttpResponseRedirect
from users.models import User
from users.forms import UserForm
def user_creation(request):
form = UserForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
return HttpResponseRedirect('/')
return render_to_response("users/base.html",
locals(),
context_instance=RequestContext(request))
def userlist(request):
return render_to_response("users/userlist.html",
{'users': User.objects.all()})
base.html文件
<html>
<body>
<h1>User Management</h1><br>
<form method='POST' action=''> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value="Create">
</form>
<!--userlist.html block-->
{% block userlist_block %}
{% endblock %}
</body>
</html>
userlist.html
{% extends "users/base.html" %}
{% block userlist_block %}
{% for user in users %}
{{ user.name_first }} {{ ' ' }}
{{ user.name_last }} {{ '; ' }}
{{ user.email_1 }}</li>
{% endfor %}
{% endblock %}