这几乎肯定是一个新手错误,但我无法弄清楚是什么问题。我在这里读过其他类似的问题,但看起来并不完全相同。
我有一个发布数据的简单表单,但我总是得到一个完全空白的回复。
以下是代码:
url.py
urlpatterns = patterns('shotgun.views',
# shotgun App URL patterns
url(r'^$', ControlPanelView.as_view(), name='index'),
)
form.py
class ProjectAssignmentForm(forms.Form):
projects = MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
def __init__(self, user_projs, *args, **kwargs):
super(ProjectAssignmentForm, self).__init__(*args, **kwargs)
self.fields["projects"].choices = tuple([(item.name, item.name) for item in SGProject.objects.all() if item.name != "Template Project"])
self.fields["projects"].initial = user_projs
view.py
class ControlPanelView(SLBaseView):
template_name = "shotgun/index.html"
def get_context_data(self, **kwargs):
context = super(ControlPanelView, self).get_context_data(**kwargs)
sg_user = SGCustomNonProjectEntity01.objects.filter(code=self.request.user.get_full_name())[0]
projs = SGProject.objects.filter(Q(entities__entity_id=sg_user.id), Q(entities__type="CustomNonProjectEntity01"))
context["assigned_projs"] = projs
if self.request.method == "POST":
form = ProjectAssignmentForm(projs, self.request.POST)
if form.is_valid():
# update user projects logic
else:
form = ProjectAssignmentForm(projs)
context["pas_form"] = form
return context
template.html
<div class="full_width" >
<h4 class="no_pad" >{{ user.first_name|upper }}'S CONTROL PANEL</h4>
</div>
<div class="full_width top_tab">
<div class="border border_pad">
<h6>PROJECTS YOU ARE ASSIGNED TO</h6>
{% if assigned_projs %}
<form action="/intranet/shotgun/" method="post">{% csrf_token %}
<div class="border_pad">
{{ pas_form.projects.errors }}
{{ pas_form.projects }}
<input type="submit" value="Update" />
</div>
</form>
{% else %}
<p>You are not assigned to any projects.</p>
{% endif %}
</div>
</div>
我在settings.py中启用了csrf中间件
我期望这一切的行为是:
未绑定数据页面按预期显示,但只要单击表单上的“更新”按钮创建发布请求,就会显示空白页面。我尝试在if self.request.method == "POST"
语句之后抛出异常,但代码似乎没有达到这个目的。我真的不确定发生了什么。
答案 0 :(得分:-1)
views.py文件中存在语法错误。
if self.request.method == "POST":
form = ProjectAssignmentForm(projs, self.request.POST)
应该是
if self.request.method == "POST":
form = ProjectAssignmentForm(projs, self.request.POST)
您是否关闭了调试功能?这可以解释为什么你没有得到任何输出。因为存在致命错误,django会打印错误报告,但如果调试关闭,它会将错误报告通过电子邮件发送到settings.py中列出的管理员。
在settings.py中检查:
DEBUG = True