Django { %include% } HTML 未正确呈现
我是 Django 和 Python 的新手,如果这是一个菜鸟问题,请原谅我。我有一个应用程序,其中有 3 个视图:
#views.py
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.acmetest.models import Player
from acme.acmetest.models import PickForm
def playerAdd(request, id=None):
form = PickForm(request.POST or None,
instance=id and Player.objects.get(id=id))
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/draft/')
return render(request, 'makepick.html', {'form':form})
def draftShow(request):
draft_list = Player.objects.all()
context_instance=RequestContext(request)
return render_to_response('listpicks.html', {'draft_list' :draft_list})
def draftPage(request):
return render(request, 'pickpage.html')
playerAdd 和 DraftShow 视图在浏览器中单独调用时会正确呈现(映射到单独的 HTML 文档上的单独 URL)。 DraftPage视图调用一个HTML页面:
pickpage.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML lang="en">
<head>
<title>PickPage</title>
</head>
<body>
{% include "makepick.html" %}
{% include "listpicks.html" %}
</body>
</HTML>
当我调用这个页面时,我只得到其他两个html页面的一部分。我得到了表单上的按钮,但没有得到输入测试的字段,我得到了列表选择的文本,但没有得到从数据库中提取的列表。这些只是 html 页面,正如我之前所说,当我单独调用它们时它们会起作用。
谢谢你的帮助。
DP
I am new to Django and Python, please forgive me if this is a noobie question. I have an app where I have 3 views:
#views.py
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.acmetest.models import Player
from acme.acmetest.models import PickForm
def playerAdd(request, id=None):
form = PickForm(request.POST or None,
instance=id and Player.objects.get(id=id))
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/draft/')
return render(request, 'makepick.html', {'form':form})
def draftShow(request):
draft_list = Player.objects.all()
context_instance=RequestContext(request)
return render_to_response('listpicks.html', {'draft_list' :draft_list})
def draftPage(request):
return render(request, 'pickpage.html')
The playerAdd and draftShow views render correctly when they are call on their own in a browser (mapped to separate URLs on separate HTML documents). The draftPage view calls an HTML page:
pickpage.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML lang="en">
<head>
<title>PickPage</title>
</head>
<body>
{% include "makepick.html" %}
{% include "listpicks.html" %}
</body>
</HTML>
When I call this page, I only get part of the other two html pages. I get the button on the form, but not the field in which to enter test and I get the text of the listpicks, but not the list that is pulled from the database. These are just html pages, and as I said earlier they work when I call them on their own.
Thank you for you help.
dp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您将包含与视图混淆了。
当您使用 include 时,所发生的一切就是在此时插入该模板的 html。
与使用您所包含的模板的其他视图没有关系。因此,您必须为您正在使用的视图中包含的模板指定上下文。
因此,在您的情况下,来自playerAdd和draftShow的上下文不可用,因为您尚未将其添加到draftPage中的渲染方法中。
会给你你所追求的东西。只需确保表单提交到playerAdd 视图即可正确处理。
Your problem is that you're confusing includes with views.
When you use an include all that happens is the html for that template is inserted at that point.
There's no relation to other views that use the template you're including. So you have to specify the context for the included templates in the view you're using.
So in your case, the context from playerAdd and draftShow isn't available, because you haven't added it to the render method in draftPage.
Will give you what you're after in your view. Just make sure that the form submits to the playerAdd view to process it correctly.