繁体   English   中英

Django脆皮表单-VariableDoesNotExist

[英]Django crispy forms - VariableDoesNotExist

我试图在http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html上完成本教程

当我尝试打开页面时,出现以下错误;

VariableDoesNotExist at / Failed lookup for key [example_form] in u

它尝试查找example_form,但找不到它。 因为我真的是django和python的新手,所以我迷失了缺少的部分。 在这种情况下我还需要一个views.py还是可以直接从urls.py引用表格?

我的urls.py

    urlpatterns = patterns('',

        url(r'^$', 'ian.views.home', name='home'),
        url(r'^admin/', include(admin.site.urls)),
    )

我的views.py

def home(request):  
    return render_to_response("index.html",
                              context_instance=RequestContext(request))

我的forms.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class ExampleForm(forms.Form):
    like_website = forms.TypedChoiceField(
        label = "Do you like this website?",
        choices = ((1, "Yes"), (0, "No")),
        coerce = lambda x: bool(int(x)),
        widget = forms.RadioSelect,
        initial = '1',
        required = True,
    )

    favorite_food = forms.CharField(
        label = "What is your favorite food?",
        max_length = 80,
        required = True,
    )

    def __init__(self, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-exampleForm'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'
        self.helper.form_action = 'submit_survey'

        self.helper.add_input(Submit('submit', 'Submit'))

我的index.html

{% load crispy_forms_tags %}
{% crispy example_form example_form.helper %}

您永远不会将表单实例传递给视图的渲染器。

非常简单地至少看到您的表单已呈现...

def home(request):  
    example_form = ExampleForm()
    return render_to_response("index.html",
                              {"example_form": example_form},
                              context_instance=RequestContext(request))

您将需要查看django文档,以了解如何处理从表单等返回的数据,但这将使您能够看到呈现在页面上的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM