如何链接Django模板中的表单?

时间:2013-11-03 17:12:53

标签: python html django templates

我是Python和Django的新手,所以请耐心等待。我正在尝试在一个base.html类中包含一个表单部分。 这就是我做到的:

Views.py:

class WikiForm(forms.Form):
    original = forms.Textarea()
    wikified = forms.Textarea()
    raw_html = forms.Textarea()

def index(request):
    wikiform = WikiForm()
    template = loader.get_template('base.html', wikiform)
    context = RequestContext(request, {})

    return HttpResponse(template.render(context))

base.html文件

<div class="sub-background">
    {% block content %}
       {{ wikiForm }}
    {% endblock %}
</div>

这是有效的,只是因为尝试在表单部分添加它失败并出现此错误。

非常感谢任何帮助!

修改 这是完整的错误:

> C:\Python27\django-trunk\django\core\handlers\base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
E:\Dropbox\University Project\wikify\Wikify_Project\Wikify_Project\views.py in index
    template = loader.get_template('base.html', wikiform) ...
▶ Local vars
C:\Python27\django-trunk\django\template\loader.py in get_template
    template, origin = find_template(template_name, dirs) ...
▶ Local vars
C:\Python27\django-trunk\django\template\loader.py in find_template
    raise TemplateDoesNotExist(name) ...
▶ Local vars

我可以通过不将wikiform传入模板来修复此错误,但是如何将表单传递给模板以包含在HTML中呈现它?

1 个答案:

答案 0 :(得分:1)

此行不正确:

template = loader.get_template('base.html', wikiform)

根据开发版本docs,此方法的结构如下:

get_template(template_name[, dirs])

对于django 1.6及更早版本,dirs参数不存在。如果你没有使用django的dev版本,该行应该给你一些关于只允许一个参数的错误。如果您使用的是开发版本,wikiform不是目录列表,那么它将无法工作。

如果要将表单传递给模板,则需要执行以下操作:

wikiform = WikiForm()
template = loader.get_template('base.html')
context = RequestContext(request, {'form': wikiform})
相关问题