Django不渲染 - 遵循文档 [英] Django Form not rendering - following documentation

查看:410
本文介绍了Django不渲染 - 遵循文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是以下人员:
我在同一个网站上建立了一个遵循通用教程的项目。
https:// docs。 djangoproject.com/en/1.8/topics/forms/#building-a-form-in-django
我已经创建了这个forms.py并将其放置在我的应用程序文件夹(民意调查)中。



我的民意调查/模板/民意调查/ index.html:

 < form action =/ your-name /method =post> 
{%csrf_token%}
{{form}}
< input type =submitvalue =Submit/>
< / form>

页面显示提交按钮,但没有输入字段。 b

My polls / views.py:

  class IndexView(generic.ListView):
template_name ='polls / index.html'
context_object_name ='latest_question_list'


get get(请求):
...如链接

民意调查/ forms.py:
from django import forms

  class NameForm(forms.Form):
print(NAMEFORM NAMEFORM NAMEFORM)
your_name = forms.CharField(label =Your name ,max_length = 100)
age = forms.IntegerField(label =Your age)



<我看到的只是提交按钮。当我点击它时,表单动作就起作用了(指向 http://127.0.0.1:8000/your -name / 这是404if,这很重要 - 我的主要问题是我无法获得输入字段显示)
感谢您的时间

form_class = NameForm ,还有 get post 方法。



您应该阅读部分的文档中,使用了基于类的视图来处理表单。好。

I am following: I have built a project from following the general tutorial on the same site. https://docs.djangoproject.com/en/1.8/topics/forms/#building-a-form-in-django I have created this forms.py and placed it in my apps folder (polls).

My polls/templates/polls/index.html:

<form action="/your-name/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

The page shows the button "Submit", but no input field.

My polls/views.py:

class IndexView(generic.ListView):
   template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'


def get_name(request):
           ... As in the link

polls/forms.py: from django import forms

class NameForm(forms.Form):
    print("NAMEFORM NAMEFORM NAMEFORM")
    your_name = forms.CharField(label="Your name", max_length = 100)
    age = forms.IntegerField(label="Your age")

All I see is the "Submit" button. When I click it, the form-action works (directs to http://127.0.0.1:8000/your-name/ which is 404if that matters - my main problem is that I cant get the input field to show up) Thank you for your time

解决方案

You are missing form_class = NameForm, and also the get and post methods.

You should read the Handling forms with class-based views section of the the documentation as well.

这篇关于Django不渲染 - 遵循文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆