Django表单不呈现 [英] Django form doesn't render
问题描述
我的模型:
#models.py
/ pre>
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):
如果request.method =='POST':
form =博士aftForm(request.POST)
如果form.is_valid():
form.save()
else:
form = DraftForm()
return render_to_response('simple / draft_input.html',{'form':form})
我的模板:
#draft_input.html
#...
< h1>草稿输入< / h1>
< h2> Draft< / h2>< br />< br />
< form method =POSTaction =>
< table> {{form}}< / table>
< input type =submitvalue =Draft Player/>
< / form>< br />< br />
#...
感谢您的帮助。
解决方案阅读答案的线程,似乎还是没有运气。
尝试执行这些命令在django交互式shell(
python manage.py shell
)中。当您尝试格式化表单时,可能会抛出异常。当模板中的表单生成期间抛出异常时,HTML中看不到太多;但是一个django shell通常会揭示罪魁祸首。python manage.py shell
在[1] :from simple.models import DraftForm
在[2]中:form = DraftForm()
在[3]中:form.as_p()
您可以查看此处生成的HTML,也可能会捕获异常。这将告诉您更多关于您需要修复的内容。
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.
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
). 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()
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屋!