简体   繁体   English

Django脆皮表单-VariableDoesNotExist

[英]Django crispy forms - VariableDoesNotExist

i am trying to do the tutorial at http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html 我试图在http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html上完成本教程

When i try to open the page i get the following error; 当我尝试打开页面时,出现以下错误;

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

It tries to look up example_form but can not find it. 它尝试查找example_form,但找不到它。 As i am really new to django and python i am at a loss where the missing part is. 因为我真的是django和python的新手,所以我迷失了缺少的部分。 Also do i need a views.py in this situation or can i directly reference the forms from the urls.py? 在这种情况下我还需要一个views.py还是可以直接从urls.py引用表格?

My urls.py 我的urls.py

    urlpatterns = patterns('',

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

My views.py 我的views.py

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

My forms.py 我的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'))

My index.html 我的index.html

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

You never pass a form instance to the view's renderer. 您永远不会将表单实例传递给视图的渲染器。

Very simply to at least see your form rendered... 非常简单地至少看到您的表单已呈现...

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

You will want to look at the django docs to see how to handle data returned from the form and such, but that will let you see it rendered on the page. 您将需要查看django文档,以了解如何处理从表单等返回的数据,但这将使您能够看到呈现在页面上的数据。

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

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