如何通过ID从Django获取JavaScript/HTML中的对象?
我正在用Django做一个小诊所项目。在所有html页面中的一个页面中,我想创建一个大按钮,其中包含特定约会的数据(id、标题、医生、患者、开始时间、结束时间)。它看起来像: 以下是基本文件: 型号.py如何通过ID从Django获取JavaScript/HTML中的对象?,javascript,python,html,django,ajax,Javascript,Python,Html,Django,Ajax,我正在用Django做一个小诊所项目。在所有html页面中的一个页面中,我想创建一个大按钮,其中包含特定约会的数据(id、标题、医生、患者、开始时间、结束时间)。它看起来像: 以下是基本文件: 型号.py class Appointment(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=100, default=defaultTitle) d
class Appointment(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100, default=defaultTitle)
description = models.CharField(max_length=480, null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True, default=datetime.date.today)
end_date = models.DateTimeField(null=True, blank=True, default=datetime.date.today)
doctor = models.ForeignKey(User, on_delete=models.CASCADE)
patient = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='+', blank=True)
def __str__(self):
return self.title
def doctortoday(request):
today = datetime.date.today().strftime("%d.%m.%Y")
today_appointments = None
if request.method == 'GET':
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
try:
today_appointments = Appointment.objects.filter(doctor=request.user, start_date__range=(today_min, today_max))
except:
today_appointments = None
context = {
'today': today,
'appointments': today_appointments
}
return render(request, 'doctortoday.html', context)
视图.py
class Appointment(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100, default=defaultTitle)
description = models.CharField(max_length=480, null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True, default=datetime.date.today)
end_date = models.DateTimeField(null=True, blank=True, default=datetime.date.today)
doctor = models.ForeignKey(User, on_delete=models.CASCADE)
patient = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='+', blank=True)
def __str__(self):
return self.title
def doctortoday(request):
today = datetime.date.today().strftime("%d.%m.%Y")
today_appointments = None
if request.method == 'GET':
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
try:
today_appointments = Appointment.objects.filter(doctor=request.user, start_date__range=(today_min, today_max))
except:
today_appointments = None
context = {
'today': today,
'appointments': today_appointments
}
return render(request, 'doctortoday.html', context)
如果单击约会按钮,则将显示弹出模式。目前只有QuerySet-{{appointments}
我的主要问题是-如何在弹出模式中显示特定约会的数据而不刷新页面?我只是想,如果我们单击带有约会2的按钮,那么在弹出模式中将显示约会2的数据。约会3等也一样
index.html
<div class="today-appointments">
{% for appointment in appointments %}
{% if user.username == appointment.doctor.username %}
<button id="{{ appointment.id }}" class="btn btn-info" onclick="showProperties(this)">
<h1>{{ appointment.title }}</h1>
<p>Doctor: {{ appointment.doctor }}</p>
<p>Patient: {{ appointment.patient }}</p>
<p>Date: {{ appointment.start_date|date:'d.m.Y' }}</p>
<p>Hour: {{ appointment.start_date|date:'H:i' }} - {{ appointment.end_date|date:'H:i' }}</p>
</button>
{% endif %}
{% endfor %}
</div>
<div class="popup-properties center">
<div class="title">
<p>Properties</p>
</div>
<div class="properties">
<h1>{{ appointments }}</h1>
<h1></h1> <!-- I WOULD LIKE HERE TITLE -->
<p></p> <!-- I WOULD LIKE HERE DESCRIPTION -->
<p></p> <!-- I WOULD LIKE HERE DOCTOR -->
<p></p> <!-- I WOULD LIKE HERE PATIENT -->
<p></p> <!-- I WOULD LIKE HERE DATE -->
<p></p> <!-- I WOULD LIKE HERE HOURS -->
<div class="buttons">
<button id="close-properties">Close</button>
<button type="submit" id="done-properties">Done</button>
</div>
</div>
</div>
我在url.py中添加了一个新路径-路径('getAppointById/',views.getAppointById,name=“getAppointById”)。然后我可以像这样在浏览器中编写链接:它会显示带有在链接中键入的特定ID的约会
接下来,在每个按钮上都有参数onclick=“showProperties(this)”。此时的Javascript函数如下所示:
showProperties()
不幸的是,它不起作用。在浏览器的控制台中弹出一个错误-。
$.ajax结构肯定有问题
是否可以更正此$.ajax结构?或者可能存在其他方式来获取指定约会而不刷新页面
和平兄弟。为什么不在每次约会时弹出一个窗口?由于Django模板是在服务器端呈现的,因此每次打开约会时都会进行额外的查询,但此处已有数据。@WillemVanOnsem您的意思是将Django模板添加到弹出模式中吗?但是,如何仅显示单击按钮的约会?您可以为
约会中的每个约会添加id,例如
。然后,您应该使用onclick=“showProperties(this,{{appointment.pk}})”
并稍微修改javascript以查找具有给定主键的弹出窗口。@WillemVanOnsem非常感谢!我没想到。。。再一次谢谢你!