0

私のviews.pyは

def editbook(request,book_id):
    log.debug("test....")
    if request.POST:
        book_name =request.POST['book_name']
        publisher_name =request.POST['publisher_name']
    books=Book.objects.filter(book_id=book_id).update(book_name=book_name, publisher_name=publisher_name)
    first_name = request.POST('first_name')
        last_name = request.POST('last_name')
        email = request.POST('email')
        age = request.POST('age')

    author_info = Author.objects.latest('author_id')
    log.debug("test:%s",author_info.author_id)
    author = Author.objects.filter(author_id=author_info.author_id).update(first_name = first_name,last_name = last_name,email=email,age=age)
        return redirect('/index/')
    else:
        books = Book.objects.get(pk=book_id)
        return render_to_response('editbook.html',{'books':books},{'author':author},context_instance=RequestContext(request))

私はエラーが発生しています

"Traceback (most recent call last):
  File "/usr/local/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/root/Samples/DemoApp/DemoApp/views.py", line 70, in editbook
    return render_to_response('editbook.html',{'books':books},{'author':author},context_instance=RequestContext(request))
UnboundLocalError: local variable 'author' referenced before assignment.
4

3 に答える 3

3

else ブロックの代わりに呼び出すときに、if 句内で author 値を割り当てたようです。エラーは、else ブロックが実行されたことを示しているだけです (たとえば、request.POST は None です)。必要なのは、デフォルト値を追加するか、if ステートメントの前に割り当てを移動することだけです。たとえば、次のことができます。

def editbook(request, book_id):
    log.debug("test....")
    author = Author.objects.filter(author_id=author_info.author_id)
    books=Book.objects.filter(book_id=book_id)
    if request.POST:
        book_name =request.POST['book_name']
        publisher_name =request.POST['publisher_name']
        books=Book.objects.filter(book_id=book_id).update(book_name=book_name, publisher_name=publisher_name)
        first_name = request.POST('first_name')
        last_name = request.POST('last_name')
        email = request.POST('email')
        age = request.POST('age')

        author_info = Author.objects.latest('author_id')
        log.debug("test:%s",author_info.author_id)
        author = Author.objects.filter(author_id=author_info.author_id).update(first_name = first_name,last_name = last_name,email=email,age=age)
        return redirect('/index/')
    else:
        books = Book.objects.get(pk=book_id)
        return render_to_response('editbook.html',{'books':books},{'author':author},context_instance=RequestContext(request))
于 2013-03-05T06:27:32.513 に答える
1

コードのフォーマットは少しずれていますが、ステートメントが真authorの場合にのみ値が割り当てられているようです。false の場合は、値が設定されていないときに値Ifを返そうとしています。author

于 2013-03-05T06:26:45.737 に答える
0

あなたの問題は、条件が真authorの場合にのみ定義されることですが、条件が失敗したときに実行されるブロックで使用しています。これと同じです:ifelseif

def foo(z=None):
    if z:
        i = 'hello'
    else:
        print i


foo()

上記を実行すると、zisNoneであるため、if条件が失敗し、i値が割り当てられません。if条件が失敗したため、節elseが実行され、i定義されていないものを印刷しようとします。

于 2013-03-05T06:33:42.020 に答える