在赋值之前引用的局部变量'context_dict'

时间:2014-12-13 18:14:42

标签: python django-views

有人可以解释一下这个错误的含义吗?什么是作业? context_dict应该在哪里?或者我犯了其他一些错误?我是Django的新手。非常感谢你。

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,' rango / add_page.html',context_dict)

相同的Traceback

3 个答案:

答案 0 :(得分: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')

答案 1 :(得分:0)

请记住,在Python中缩进很重要。问题是你的变量context_dict在if语句之外没有引用,因为它是如何缩进的。因此,在if语句之外声明变量context_dict为:

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

答案 2 :(得分:0)

我重新考虑了我的代码,这种方式有效。我的context_dict现在在if外部语句之外,而且: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)