我正在尝试实施一个平均时间功能,允许用户输入医生的等待时间。但我一直得到这个错误,我不知道如何解决它。我尝试过研究,但我仍然很困惑。
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)
答案 0 :(得分:1)
问题是post对象中没有变量'time'。
我建议的是:
1)通过将其打印出来或将其写入文件,查看帖子对象的外观。 (我想你可能会使用get而不是post。)
2)当您使用用户输入的数据时,总是希望它可能与预期的不同。 (恶意用户或蜘蛛可能正在抓取您的网站。)而是使用:
time = post.get('time')
if time:
time = escape()
else:
# No time value was submitted ....
答案 1 :(得分:0)
正如cchristelis所说,错误是因为您的POST数据中没有time
值。
真的,你应该使用Django' forms framework。这是验证用户输入和使用它的适当工具。