Django 1.6 TypeError render_to_string()为关键字参数'context_instance'获取了多个值

时间:2014-06-11 08:08:49

标签: python django django-1.6

我正在尝试在模板中显示与医生相关的所有等待时间。但是我得到了这个错误。我对django很新,所以我不确定要改变什么。

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 "views.py" in showDocProfile
  69.     return render(request,'meddy1/docprofile.html',{'doctor': profile}, {'timeList': WaitingTime.objects.all()})
File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),

Exception Type: TypeError at /docprofile/1/
Exception Value: render_to_string() got multiple values for keyword argument 'context_instance'

这是我正在尝试保存所有等待时间对象的view.py

def showDocProfile(request, id):
    profile = Doctor.objects.get(id=id)
    return render(request,'meddy1/docprofile.html',{'doctor': profile}, {'timeList': WaitingTime.objects.all()})

这是我正在尝试显示等待时间的模板docprofile.html

{% for t in timeList %}
  <h4>{t.time}</h4>
{% endfor %}

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, blank = True )
    doctor = models.ForeignKey(Doctor)
    doctor_seeker = models.ForeignKey(DoctorSeeker)
    date = models.DateField()

class Doctor(models.Model):
    avg_times = models.ManyToManyField(DoctorSeeker, through="WaitingTime")
    name = models.CharField(max_length=30)
    specialization = models.ForeignKey(Specialization)
    clinic = models.ForeignKey(Clinic)
    language = models.ManyToManyField(Language)
    education1 = models.CharField(max_length=100)
    education2 = models.CharField(max_length=100, null = True)
    gender_choices = ( ('M', 'Male'), ('F','Female'),)
    gender = models.CharField(max_length=5, choices = gender_choices, null=True)
    profile_pic = models.ImageField(upload_to='/uploads/', null=True)
    statement = models.TextField(null=True)
    affiliation = models.CharField(max_length=100, null = True)

2 个答案:

答案 0 :(得分:3)

在你render()的电话中,你传递的是2个词,而你需要传递1个dict和两个项目,这被视为模板的上下文。

将您的全部更新为

return render(request,'meddy1/docprofile.html',
          {'doctor': profile, 
           'timeList': WaitingTime.objects.all()
          })

答案 1 :(得分:1)

doctortimelist需要在同一个词典中,而不是单独的词典。

return render(request,'meddy1/docprofile.html',{'doctor': profile, 'timeList': WaitingTime.objects.all()})
相关问题