簡體   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