Django 表单不呈现 [英] Django form doesn't render

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

问题描述

您好,提前致谢.我有一个未在模板上呈现的 Django 表单.提交"按钮呈现,但表单没有.我已经盯着这段代码看了大约 6 个小时,它可能很简单,我可能需要另外一双眼睛.

Hello and thank you in advance. I have a django form that is not rendering on a template. 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.

我的模型:

#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 

我的观点:

#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} )

我的模板:

#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 />
#...

感谢您的帮助.

推荐答案

阅读答案线程,您似乎仍然不走运.

Reading the answers thread, it seems you're still out of luck.

尝试在 django 交互式 shell (python manage.py shell) 中执行这些命令.当您尝试格式化表单时,可能会引发异常.当模板中的表单生成过程中抛出异常时,您将不会在 HTML 中看到太多内容;但 django shell 通常会揭开罪魁祸首.

Try performing these commands in a django interactive shell (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; but a django shell usually unveils the culprit.

python manage.py shell

In [1]: from simple.models import DraftForm

In [2]: form = DraftForm()

In [3]: form.as_p()

您可以在此处看到生成的 HTML,也可以捕获异常.这将告诉您更多关于您需要修复的内容.

You can either see the HTML generated here, or would catch an exception. This will tell you more about what you need to fix.

这篇关于Django 表单不呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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