这几乎可以肯定是一个新手错误,但我无法弄清楚出了什么问题。我已经阅读了此处的其他类似问题,但似乎并非完全相同。
我有一个发布数据的简单表格,但我总是得到一个完全空白的回复。
这是代码:
网址.py
urlpatterns = patterns('shotgun.views',
# shotgun App URL patterns
url(r'^$', ControlPanelView.as_view(), name='index'),
)
表格.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
视图.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
模板.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"
但代码似乎并没有走到这一步。我真的不确定发生了什么。