0

我有这个片段

form = TripManagementForm(request.POST)
alert=dict()
if form.is_valid():
    trip_id = form.cleaned_data['trip_id']
    trip = Trip.objects.get(pk=trip_id)
    trip.name = form.cleaned_data['name']
    trip.advance_payed = form.cleaned_data['advance']
    trip.rent = form.cleaned_data['rent']
    trip.toll_fee = form.cleaned_data['toll_fee']
    trip.checkpost_charges = form.cleaned_data['checkpost']
    trip.police_checking = form.cleaned_data['police_checking']
    trip.accidents_renumeration = form.cleaned_data['accidents_renumeration']
    trip.fuel_expense = form.cleaned_data['fuel_expense']
    trip.maintainance.trip_maintainance = form.cleaned_data['maintainance']
    trip.driver = form.cleaned_data['driver']
    trip.payment_status = form.cleaned_data['payment_status']
    if not form.cleaned_data['payment_mode'] == 0:
        trip.payment_mode = form.cleaned_data['payment_mode']
    trip.cheque_no = form.cleaned_data['cheque_no']
    trip.bata = form.cleaned_data['bata']
    trip.others = form.cleaned_data['others']
    trip.setbalance()
    trip.save()
    alert['success'] = 'Saved successfully'
    context = dict(context.items() + ({'form':form, 'trip': trip, 'alert': alert}).items())
    return render_to_response('portal_trip_management.html', context, context_instance=RequestContext(request))
else:
    trip_id = form.data['trip_id']
    trip = Trip.objects.get(pk=trip_id)
    alert['error'] = 'Encountered some error'
    context = dict(context.items() + ({'form':form, 'trip': trip, 'alert':alert}).items())
    return render_to_response('portal_trip_management.html', context, context_instance=RequestContext(request))

我得到了错误

local variable 'trip' referenced before assignment

好吧,它显示在之后的 return 声明中alert['success'] = 'Saved successfully'。无法找出发生了什么。我之前已经提到过旅行。它仍然显示错误。请帮忙。我在 stackoverflow 中看到了类似的问题,但同样没有一个解决方案能够奏效。

编辑添加回溯

Environment:


Request Method: POST
Request URL: http://beta.track24.in/management/tripdetails?trip_id=3

Django Version: 1.4.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'accounts',
 'managements',
 'locations')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/track24/managements/views.py" in trip_management
  443.          return render_to_response('portal_trip_management.html', context, context_instance=RequestContext(request))

Exception Type: UnboundLocalError at /management/tripdetails
Exception Value: local variable 'trip' referenced before assignment

这是 POST 请求的详细信息

advance                 u'0.00'
maintainance            u'0.00'
toll_fee                u'0.00'
bata                    u'200.00'
payment_status          u'on'
driver                  u'1'
checkpost               u'0.00'
rent                    u'0.00'
others                  u'0.00'
payment_mode            u'check'
csrfmiddlewaretoken     u'PMBlzlrNRUyiWH9UzJIrONsDRNU2MOhj'
trip_id                 u'3'
accidents_renumeration  u'0.00'
police_checking         u'100.00'
fuel_expense            u'0.00'
4

0 回答 0