Can';我的Django表格不起作用
我不知道这里发生了什么:我以为我是在实现一个非常简单的Django表单,遵循Django文档中的ContactForm示例(尽管我对forms.py使用了不同的示例)。然而,由于某种原因,当页面加载时,我得到了模板,但没有出现表单。根据模板,所有HTML都在那里,但是Django的东西(我添加的表单和Can';我的Django表格不起作用,django,forms,Django,Forms,我不知道这里发生了什么:我以为我是在实现一个非常简单的Django表单,遵循Django文档中的ContactForm示例(尽管我对forms.py使用了不同的示例)。然而,由于某种原因,当页面加载时,我得到了模板,但没有出现表单。根据模板,所有HTML都在那里,但是Django的东西(我添加的表单和test变量)没有呈现 我有点希望在某个我无法识别的地方有一个复杂的地方,但是,我担心我犯了一个n00b错误,我应该能够发现 如果有人能帮我,我将非常感激 我的代码: forms.py: class
test
变量)没有呈现
我有点希望在某个我无法识别的地方有一个复杂的地方,但是,我担心我犯了一个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'
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)
)
<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>
视图。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'
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)
)
<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>
HTML:
class ContactSupportForm(forms.Form):
fields = ('theSubject', 'theMessage')
widgets = {
'theMessage': forms.Textarea(attrs={'cols': 55, 'rows': 12}),
}
error_css_class = 'error'
required_css_class = 'required'
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)
)
<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>
但是,我仍然没有在模板中获得表单,也没有在模板中获得测试。视图正确地返回模板,但它不返回表单或测试,正如Aamir指出的那样,您还没有在表单上定义任何字段,也不清楚您是从何处获得这样做的想法的。清楚地显示要执行的操作:
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'
窗口小部件
和字段
属性仅在ModelForms的内部元类中使用,ModelForms是从模型生成的表单,当您希望覆盖该模型上字段的默认字段/窗口小部件时。你没有在这里这么做,所以他们没有意义
此外,在您的视图中,您应该删除第二个else
子句:当出现验证错误时,您将重新实例化表单,这样错误就不会显示在HTML页面上。删除该子句将允许直接执行render_to_响应调用
(关于变量名的问题,除了camelCase问题,我无法想象为什么你觉得有必要在一半变量前面加上
the
,而另一半变量前面加上my
。这是在添加什么信息?,正如Aamir指出的那样,您还没有在表单上定义任何字段,也不清楚您是从何处获得按现有方式执行的想法的。清楚地显示要执行的操作:
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'
窗口小部件
和字段
属性仅在ModelForms的内部元类中使用,ModelForms是从模型生成的表单,当您希望覆盖该模型上字段的默认字段/窗口小部件时。你没有在这里这么做,所以他们没有意义
此外,在您的视图中,您应该删除第二个else
子句:当出现验证错误时,您将重新实例化表单,这样错误就不会显示在HTML页面上。删除该子句将允许直接执行render_to_响应调用
(关于变量名的问题,除了camelCase问题,我无法想象为什么你觉得有必要在一半变量前面加上
the
,而另一半变量前面加上my
。这是在添加什么信息?Argh,请停止使用有趣的JavaStyleVariableNames!camelCase有什么不喜欢的?!为我工作!你的班次错了。请再次阅读文档。我不知道你在那里干什么。您的表单中没有定义字段。啊,请使用funnyJavaStyleVariableNames停止!camelCase有什么不喜欢的?!为我工作!你的班次错了。请再次阅读文档。我不知道你在那里干什么。您的表单中没有定义字段。谢谢Daniel-我已经更新了问题-尽管更改了forms.py,它仍然不起作用。关于<代码>和<代码>我的<代码>主题,想法是(不是我的,我可能还没有完全正确地理解它)表示属于函数(my
)或由函数(my
)创建或传入/传出(the
)的变量。。。。我没有解释清楚。。。不用说,我通常不会做太多的Django编码。但是这个想法是为了让其他人更容易理解代码。出于同样的原因,我们使用camelCase/描述性词语。这可能很愚蠢/没有必要,但事实就是这样!谢谢Daniel-我已经更新了问题-尽管更改了forms.py,它仍然不起作用。关于<代码>和<代码>我的<代码>主题,想法是(不是我的,我可能还没有完全正确地理解它)表示属于函数(my
)或由函数(my
)创建或传入/传出(the
)的变量。。。。我没有解释清楚。。。不用说,我通常不会做太多的Django编码。但是这个想法是为了让其他人更容易理解代码。出于同样的原因,我们使用camelCase/描述性词语。这可能很愚蠢/没有必要,但事实就是这样!