Django模板不显示模型数据,显示空白页面

时间:2016-06-14 20:35:18

标签: django django-models django-templates django-views

Django模板不显示模型数据,显示空白页面 ......看看吧:

models.py

class appointment(models.Model):

patient_name1= models.ForeignKey('identity')
appoint_date= models.DateTimeField('Appoinment time and date')
patient_info=  models.TextField()
fees= models.CharField('Fees',max_length=100,blank=True)

class Meta:
    verbose_name = 'Appointment Detail'
    verbose_name_plural = 'Appoinment Details'
    ordering = ['appoint_date']

def __str__(self):
    return '%s (%s)' % (self. patient_name1, self.appoint_date)

views.py

from django.shortcuts import render
from .models import identity, appointment 


def index(request):
    return render(request, 'appoint/index.html')

def appointment_list(request):
    Appointments = appointment.objects.all()
    context = {'Appointments': Appointments}
    return render(request, 'appoint/appointment_list.html', context)

appointment_list.html

<p>{{Appointments.patient_name1}}</p>

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^appointment_list/$', views.appointment_list,  name='appointment_list'),
url(r'^aboutme/$', views.aboutme, name='about_us'),
url(r'^contact/$', views.contact, name='contact_us'),
url(r'^apply_appoint/$', views.apply_appoint, name='apply_appoint'),
]

请帮助我,我是Django 1.9的新手

2 个答案:

答案 0 :(得分:0)

您需要迭代查询集,然后访问对象的属性:

<p>
  {% for appointment in Appointments %}
      {{ appointment.patient_name1 }}
  {% endfor %}
</p>

Appointments是一个查询集,它是Appointment类的实例列表。

你需要用大写字母btw来命名你的课程。通常,对象为小写,类名以大写字母开头。

答案 1 :(得分:0)

Appointments是您需要在模板中循环它们的模型对象列表 像这样:

<p>
{% for object in Appointments %}
  {{ object.patient_name1 }} , {{ object.appoint_date }}
{% endfor %}
</p>