简体   繁体   English

Django表单无法呈现

[英]Django form doesn't render

Hello and thank you in advance. 您好,谢谢您。 I have a django form that is not rendering on a template. 我有一个未在模板上呈现的django表单。 The "submit" button renders, but the form doesn't. “提交”按钮呈现,但表单不呈现。 I have been staring at this code for about 6 hours and it is probably something very simple, I probably need another pair of eyes on it. 我一直在看这段代码大约6个小时,这可能很简单,我可能需要另一双眼睛。

My Model: 我的模特:

#models.py

from django.db import models
from django.forms import ModelForm

class DraftInput(models.Model):
    player_id = models.IntegerField(help_text = 'Input Player ID Number', max_length = 5)

    def __unicode__(self):
        return self.player_id

class DraftForm(ModelForm):
    class Meta:
        model = DraftInput 

My views: 我的看法:

#views.py

from django.shortcuts import render_to_response

from simple.models import DraftInput
from simple.models import DraftForm

from django.template import RequestContext

#...    

def draft_view(request):

    if request.method == 'POST':
        form = DraftForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = DraftForm()

    return render_to_response('simple/draft_input.html', {'form': form} )

My template: 我的模板:

#draft_input.html

#...

        <h1>Draft Input</h1>
        <h2>Draft</h2><br /><br />
            <form method="POST" action="">
            <table>{{ form }}</table>
            <input type="submit" value="Draft Player" />
        </form><br /><br />
#...

Thank you for your help. 谢谢您的帮助。

Reading the answers thread, it seems you're still out of luck. 阅读答案线程,看来您仍然不走运。

Try performing these commands in a django interactive shell ( python manage.py shell ). 尝试在Django交互式外壳程序( python manage.py shell )中执行这些命令。 It's possible that an exception is thrown when you try to format the form. 当您尝试格式化表单时,可能会引发异常。 When an exception is thrown during the form generation in the template, you won't see much in the HTML; 在模板中的表单生成过程中引发异常时,HTML中不会看到太多内容; but a django shell usually unveils the culprit. 但是通常用django贝壳揭开罪魁祸首。

python manage.py shell

In [1]: from simple.models import DraftForm

In [2]: form = DraftForm()

In [3]: form.as_p()

You can either see the HTML generated here, or would catch an exception. 您可以看到此处生成的HTML,或者将捕获异常。 This will tell you more about what you need to fix. 这将告诉您更多有关您需要解决的问题的信息。

使用{{ form.as_table }}或从您的表单标签周围删除<table></table>

I think that it is a sign to move to generics classes :) Just try CreateView class 我认为这是转向泛型类的标志:)只需尝试CreateView

In your views.py file: 在您的views.py文件中:

class CreateDraftInputView(CreateView):
    model = DraftInput
    template_name = "test/draftinput_form.html"
    success_url = "/test/listdraft/"

Simple create template: 简单的创建模板:

<form method="POST" action=".">
    <table>
    {% csrf_token %}
    {{ form.as_table }}
    <input type="submit" />
    </table>
</form>

And very simple list of records in your urls.py: 还有urls.py中非常简单的记录列表:

, (r"^listdraft/$", ListView.as_view(model = DraftInput, template_name = "draftinput_list.html"))

Could not quickly find the root cause, but the fields in my form only show up when the variable declaration follows the conventional module.Class form. 无法快速找到根本原因,但是表单中的字段仅在变量声明遵循常规module.Class表单时显示。 The issue might be somewhere in classloading order. 问题可能在类加载顺序中。

from simple import models

# ...
form = models.DraftForm()

Try {{ form.as_p }} for example. 例如,尝试{{ form.as_p }} There are other shortcuts. 还有其他快捷方式。 You can also manually create the form. 您也可以手动创建表单。 Have a look into the Django Docs : https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs 看看Django Docs: https ://docs.djangoproject.com/en/dev/topics/forms/?from =olddocs

Try "form.as_p" or "form.as_table" instead of just "form" in your html file. 尝试在HTML文件中尝试“ form.as_p”或“ form.as_table”,而不只是“ form”。

EDIT: check out the documentation and use one of their examples as a model: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/ 编辑:查看文档并使用其示例之一作为模型: https : //docs.djangoproject.com/en/dev/topics/forms/modelforms/

In your example, you define form parameters in your model, which may be incorrect. 在您的示例中,您在模型中定义了表单参数,这可能是不正确的。 You want to leave form specific modifications, like help_text in the DraftForm class, now in the DraftInput model. 您想保留表单特定的修改,例如DraftForm类中的help_text,现在保留在DraftInput模型中。

This is a simple issue, you should just replicate something from the docs and then work towards what you want. 这是一个简单的问题,您应该只复制文档中的内容,然后朝着想要的方向工作。

Hope this helps. 希望这可以帮助。

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

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