【问题标题】:Django 1.6 TypeError render_to_string() got multiple values for keyword argument 'context_instance'Django 1.6 TypeError render_to_string() 为关键字参数“context_instance”获取了多个值
【发布时间】:2014-06-11 08:08:48
【问题描述】:

我正在尝试在我的模板中显示与医生相关的所有等待时间。但我得到这个错误。我对 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)

【问题讨论】:

    标签: python django django-1.6


    【解决方案1】:

    在您对render() 的调用中,您传递了 2 个字典,而不是您需要传递 1 个包含两个项目的字典,这将作为模板的上下文。

    更新你的一切

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

    【讨论】:

    • 即使您从“settings.py”传递一个常量,您也必须将它包含在同一个字典中,而不是创建两个。示例:return render(request,'meddy1/docprofile.html', {'doctor': profile, 'debugMod': settings.DEBUG })。在您的模板中:{{debugMod}}
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多