我不知道这里发生了什么:我认为我正在实现一个非常简单的Django表单,遵循Django Docs中的ContactForm示例(尽管我使用了另一个示例来表示forms.py)。但是,出于某种原因,当页面加载时,我得到模板,但没有出现任何表单。根据模板,所有的HTML都在那里,但是Django的东西(我添加的形式和theTest
变量)没有呈现。
我一半希望在某个地方有一个我无法识别的并发症,但是,我担心我犯了一个n00b错误,我应该能够接受......
如果有人能帮助我,我会非常感激!
forms.py:
class ContactSupportForm(forms.Form):
fields = ('theSubject', 'theMessage')
widgets = {
'theMessage': forms.Textarea(attrs={'cols': 55, 'rows': 12}),
}
error_css_class = 'error'
required_css_class = 'required'
views.py:
from forms import ContactSupportForm
@login_required
def postSupportMessage(theRequest):
"""The new view to which the support form is submitted"""
isLoggedIn = linfiniti.isUserLoggedIn(theRequest)
if isLoggedIn == True:
myRequestingUser = theRequest.user
myRequestingUserEmail = myRequestingUser.email
else:
return HttpResponseForbidden()
if theRequest.POST:
theForm = ContactSupportForm(theRequest.POST)
if theForm.is_valid():
theIssueSummary = theForm.cleaned_data['theSubject']
theMessageDesc = theForm.cleaned_data['theMessage']
theIssueDesc = '%s \n \n Username: %s \n \n User Email: %s' % \
(theMessageDesc, myRequestingUser, myRequestingUserEmail)
theIssue = json.dumps({
"fields": {
"project":
{
"key": "SUPPORT"
},
"summary": theIssueSummary,
"description": theIssueDesc,
"issuetype": {
"name": "Bug"
}
}
})
myRequest = urllib2.Request('http://MYURL')
myAuthString = base64.standard_b64encode('%s:%s' % ('USERNAME', 'PASSWORD'))
myRequest.add_header("Authorization", "Basic %s" % myAuthString)
theResult = urllib2.urlopen(myRequest, theIssue, {'Content-Type': 'application/json'})
myReturn = theResult.read()
if myReturn:
theNewKey = myReturn.key
return HttpResponse(json.dumps({
"success": True,
"theNewKey": theNewKey
}))
else:
return HttpResponse(json.dumps({
"success": False
}))
else:
theForm = ContactSupportForm()
theTest = 'crap'
else:
theForm = ContactSupportForm()
theTest = 'rubbish'
return render_to_response('contact-support.html',
{
'theForm': theForm,
'test': theTest
},
context_instance=RequestContext(theRequest)
)
HTML:
<h5>Contact Support</h5>
<div class="buttons" id="reload-support-form">
<a data-ignore="true" class="btn btn-mini" href="javascript:void(null);" id="show-sent" onClick="reLoadSupportForm();">
<i class="icon-refresh"></i> Reload Form
</a>
</div>
</div>
<h1>{{test}}</h1>
<div class="widget-content" id="support-message-container">
<div id="message-support-content">
<form action="" method="post" id="compose-message-form" class="form-horizontal">
{% csrf_token %}
{% for field in theForm %}
<fieldset class="control-group {% if field.errors %}error{% endif %}">
<label class="control-label" for="{{ field.auto_id }}">{{ field.label }}</label>
<div class="controls">
{{ field }}
{% if field.help_text %}
<span class="help-inline">{{ field.help_text }}</span>
{% endif %}
{% if field.errors %}
<span class="help-inline">{{ field.errors|striptags }}</span>
{% endif %}
</div>
</fieldset>
{% endfor %}
<div class="control-group">
<div class="controls">
<input id="support-message-submit" class="btn btn-primary" type="submit"/>
</div>
</div>
根据下面的答案/评论,我已更新了我的forms.py(我还从视图中删除了第二个else
):
class ContactSupportForm(forms.Form):
theSubject = forms.CharField(max_length=100)
theMessage = forms.CharField(widget=forms.Textarea(attrs={'cols': 55, 'rows': 12}))
error_css_class = 'error'
required_css_class = 'required'
但是,我仍然没有在模板中获取表单,我也没有在模板中获得theTest
。视图正确返回模板,但它不返回表单或theTest
。
答案 0 :(得分:1)
正如Aamir所指出的那样,你没有在你的表单上定义任何字段,而且你不清楚你有什么想法以你的方式去做。 The documentation清楚地显示了该做什么:
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea(attrs={'cols': 55, 'rows': 12}))
error_css_class = 'error'
required_css_class = 'required'
当您要覆盖该模型上字段的默认字段/小部件时,widgets
和fields
属性仅用于ModelForms的内部Meta类,它是从模型生成的表单。 。你不是在这里做的,所以他们毫无意义。
此外,在您的视图中,您应该删除第二个else
子句:当您存在验证错误时,您将重新实例化表单,因此错误不会显示在HTML页面上。删除该子句将允许执行直接进入render_to_response调用。
(关于变量名称的主题,除了camelCase问题之外,我无法想象为什么你觉得需要在the
前加一半变量,在my
加上另一半。添加什么信息?)