2014-12-13 38 views
0

有人可以請我解釋一下這個錯誤的含義嗎?什麼是任務? 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)

相同回溯

+0

如果請求方法不是「POST」,那麼您不要定義'context_dict',而是嘗試在最後一行使用它。 – Matthias 2014-12-13 18:17:00

回答

0

請記住,在Python中,縮進非常重要。問題是你的變量context_dict在if語句之外沒有任何引用,因爲它是如何縮進的。所以,你聲明的變量context_dict爲這樣的if語句外:

context_dict = {} 
    if request.method == 'POST': 
    <rest of code here> 
+0

是的,工作:)謝謝 – Anita 2014-12-14 13:21:20

0

試試此代碼只回復POST請求。

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} 
     # indented - only reply to post requests 
     return render(request, 'rango/add_page.html', context_dict) 
    # if this is not a post request, raise an exception (handle your way later..) 
    raise Exception('NOT A POST REQUEST') 
+0

這樣我得到的錯誤:不是POST請求,但實際上我宣佈,如果語句外變量context_dict和工程:) – Anita 2014-12-14 13:18:53

+0

錯誤是故意的。你需要決定你如何處理這個問題,並在錯誤沒有發生的情況下刪除錯誤。 – 2014-12-14 13:21:07

0

我重新再分解我的代碼和這樣的作品。我的context_dict現在在if語句之外,而else:form = PageForm()在外部if語句中,現在不像以前那樣在內部if語句中。謝謝你們幫助我。

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 = {'category': cat, 'form': form} 

    return render(request, 'rango/add_page.html', context_dict) 
相關問題