有人可以請我解釋一下這個錯誤的含義嗎?什麼是任務? context_dict應該在哪裏?或者我犯了一些其他錯誤?我是Django的新手。非常感謝。分配前引用的局部變量'context_dict'
def add_page(request, category_name_slug):
try:
cat = Category.objects.get(slug=category_name_slug)
except Category.DoesNotExist:
cat = None
if request.method == 'POST':
form = PageForm(request.POST)
if form.is_valid():
if cat:
page = form.save(commit=False)
page.category = cat
page.views = 0
page.save()
# probably better to use a redirect here.
return category(request, category_name_slug)
else:
print form.errors
else:
form = PageForm()
context_dict = {'form':form, 'category': cat}
return render(request, 'rango/add_page.html', context_dict)
我得到這個錯誤:
local variable 'context_dict' referenced before assignment
和異常指向我在符合收益渲染(reqyest, '蘭戈/ add_page.html',context_dict)
相同回溯
如果請求方法不是「POST」,那麼您不要定義'context_dict',而是嘗試在最後一行使用它。 – Matthias 2014-12-13 18:17:00