Python Django 1.6:多值错误

Python Django 1.6:多值错误,python,django,views,Python,Django,Views,我正在尝试实现一个平均时间特性,允许用户输入等待医生的时间。但我不断地遇到这个错误,我不知道如何修复它。我已经试过研究了,但我还是很困惑 Traceback: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 114. response = wrapped_callback(request, *callback_args, **ca

我正在尝试实现一个平均时间特性,允许用户输入等待医生的时间。但我不断地遇到这个错误,我不知道如何修复它。我已经试过研究了,但我还是很困惑

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "views.py" in addWaitingTime
  111.     time = escape(post['time'])
File "/Library/Python/2.7/site-packages/django/utils/datastructures.py" in __getitem__
  301.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /waiting_time/
Exception Value: "'time'"
views.py

@login_required
def addWaitingTime(request):
    post = request.POST
    time = escape(post['time'])
    doctor_seeker = post['userId']
    doctor = post['doctorId']

    if len(time) > 1:
        newTime = WaitingTime(time=time, doctor_seeker_id = userId, doctor_id = doctorId)
        newTime.save()

    url = "/docprofile/"+str(doctor_id)+"/"
    return HttpResponseRedirect(url)
models.py

class WaitingTime(models.Model):
    time_choices = ( (10, 'Less than 10 Minutes'), (20, 'Less than 20 Minutes'), (30, 'Less than 30 Minutes'))
    time = models.IntegerField(choices = time_choices)
    doctor_id = models.IntegerField()
    doctor_seeker_id = models.IntegerField()

    def __unicode__(self):
        return u"%s %s" % (self.time, self.doctor_id)

class Doctor(models.Model):
    name = models.CharField(max_length=30)
    specialization = models.ForeignKey(Specialization)
    clinic = models.ForeignKey(Clinic)
class DoctorSeeker(models.Model):
    name = models.CharField(max_length=30)
    email = models.EmailField()
    user = models.OneToOneField(User, unique=True)

问题是post对象中没有变量“time”

我的建议是:

1) 通过打印或写入文件,查看post对象的外观。(我想您可能正在使用get而不是post。)

2) 当您处理用户输入的数据时,总是希望它可能与预期的不同。(恶意用户或爬行器可能正在抓取您的站点。)而是使用:

time = post.get('time')
if time:
    time = escape()
else:
    # No time value was submitted ....

正如cchristelis所说,错误是因为您的POST数据中没有
time


真的,对于这类事情,你应该使用Django的。这是验证用户输入并对其进行处理的适当工具

如果我使用您上面所述的内容,我得到的全局名称“post”没有定义。我想您会意识到它必须在您的
post=请求之后发生。post