使用 Django Tables2 为对话框添加点击事件
Putting a click event for a dialogue box with Django Tables2
我正在尝试使用 Django Tables2 创建一个点击事件,这样每当有人点击一行中的删除 link 时,它都会在删除该行之前创建一个确认对话框。这是我的代码:
models.py
class Schedules(models.Model):
course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
room = models.CharField(max_length=128, choices=ROOM_CHOICES, default='A')
start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
start_time = models.CharField(max_length=128, choices=START_TIME_CHOICES, default='eight-thirty am')
end_time = models.CharField(max_length=128, choices=END_TIME_CHOICES, default='eight-thirty am')
instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
total_hours = models.CharField(max_length=128, choices=TOTAL_HOURS_CHOICES, default='six')
hours_per_class = models.CharField(max_length=128, choices=HOURS_PER_CLASS_CHOICES, default='four_and_half')
frequency = models.CharField(max_length=128)
status = models.CharField(max_length=128, choices=STATUS_CHOICES)
interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='1 day')
initiated_by = models.CharField(max_length=128, null=True)
schedule_id = models.IntegerField(default=0)
tables.py
class ScheduleListTable(tables.Table):
change = tables.TemplateColumn('<a href="/schedule/update_schedule/{{ record.id }}">Update</a> / Cancel / Event / '
'<a href="/schedule/delete_schedule/{{ record.id }}"
onclick="return confirm("Are you sure you want to delete this?")">Delete</a>',
verbose_name=u'Change', )
class Meta:
model = Schedules
fields = ('id', 'course_name', 'start_date', 'start_time', 'hours_per_class', 'instructor', 'change',)
attrs = {"class": "paleblue"}
views.py
def schedule_List(request):
context_dict = {}
schedule_list = Schedules.objects.order_by('start_date')
table = ScheduleListTable(schedule_list)
context_dict['table'] = table
return render(request, "schedule/schedule_list.html", context_dict)
schedule_list.html
<div id="schedule_list_table">
{% if table %}
{% render_table table %}
{% endif %}
</div>
由于某些原因,我无法使出现确认对话框的onclick事件发生,直接删除。我假设它在 tables.py 中写错了,但我不知道在那种情况下如何正确地写它。或者我需要做其他事情吗?
查看呈现的 html,例如使用您的浏览器检查上下文菜单选项。我想你可以看到你使用的双引号有问题。
onclick
属性用双引号括起来,但作为参数传递给 confirm()
的消息也用双引号括起来。这会导致您的浏览器将该属性解释为`onclick="return confirm(" 并忽略它无法理解的乱码。
您可以通过使用单引号将消息参数括在 confirm()
中来解决此问题,方法是在您使用的语法 (\'
) 中转义它们,或者使用像这样的三重引号:
template_code = '''
<a href="/schedule/update_schedule/{{ record.id }}">Update</a> / Cancel / Event /
<a href="/schedule/delete_schedule/{{ record.id }}"
onclick="return confirm('Are you sure you want to delete this?')">Delete</a>'''
我正在尝试使用 Django Tables2 创建一个点击事件,这样每当有人点击一行中的删除 link 时,它都会在删除该行之前创建一个确认对话框。这是我的代码:
models.py
class Schedules(models.Model):
course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
room = models.CharField(max_length=128, choices=ROOM_CHOICES, default='A')
start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
start_time = models.CharField(max_length=128, choices=START_TIME_CHOICES, default='eight-thirty am')
end_time = models.CharField(max_length=128, choices=END_TIME_CHOICES, default='eight-thirty am')
instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
total_hours = models.CharField(max_length=128, choices=TOTAL_HOURS_CHOICES, default='six')
hours_per_class = models.CharField(max_length=128, choices=HOURS_PER_CLASS_CHOICES, default='four_and_half')
frequency = models.CharField(max_length=128)
status = models.CharField(max_length=128, choices=STATUS_CHOICES)
interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='1 day')
initiated_by = models.CharField(max_length=128, null=True)
schedule_id = models.IntegerField(default=0)
tables.py
class ScheduleListTable(tables.Table):
change = tables.TemplateColumn('<a href="/schedule/update_schedule/{{ record.id }}">Update</a> / Cancel / Event / '
'<a href="/schedule/delete_schedule/{{ record.id }}"
onclick="return confirm("Are you sure you want to delete this?")">Delete</a>',
verbose_name=u'Change', )
class Meta:
model = Schedules
fields = ('id', 'course_name', 'start_date', 'start_time', 'hours_per_class', 'instructor', 'change',)
attrs = {"class": "paleblue"}
views.py
def schedule_List(request):
context_dict = {}
schedule_list = Schedules.objects.order_by('start_date')
table = ScheduleListTable(schedule_list)
context_dict['table'] = table
return render(request, "schedule/schedule_list.html", context_dict)
schedule_list.html
<div id="schedule_list_table">
{% if table %}
{% render_table table %}
{% endif %}
</div>
由于某些原因,我无法使出现确认对话框的onclick事件发生,直接删除。我假设它在 tables.py 中写错了,但我不知道在那种情况下如何正确地写它。或者我需要做其他事情吗?
查看呈现的 html,例如使用您的浏览器检查上下文菜单选项。我想你可以看到你使用的双引号有问题。
onclick
属性用双引号括起来,但作为参数传递给 confirm()
的消息也用双引号括起来。这会导致您的浏览器将该属性解释为`onclick="return confirm(" 并忽略它无法理解的乱码。
您可以通过使用单引号将消息参数括在 confirm()
中来解决此问题,方法是在您使用的语法 (\'
) 中转义它们,或者使用像这样的三重引号:
template_code = '''
<a href="/schedule/update_schedule/{{ record.id }}">Update</a> / Cancel / Event /
<a href="/schedule/delete_schedule/{{ record.id }}"
onclick="return confirm('Are you sure you want to delete this?')">Delete</a>'''