使用Django Tables2为对话框放置click事件 [英] Putting a click event for a dialogue box with Django Tables2
问题描述
我正在尝试使用Django Tables2创建click事件,以便每当有人单击行中的删除链接时,它都会创建一个对话框以供确认,然后再删除该行。这是我的代码:
I am trying to make a click event with Django Tables2 so that whenever someone clicks on a delete link in a row it will create a dialogue box for confirmation before deleting the row. Here is my code:
models.py
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
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
schedule_list.html
<div id="schedule_list_table">
{% if table %}
{% render_table table %}
{% endif %}
</div>
由于某种原因,我无法使显示确认对话框的onclick事件出现,并且它只是直接删除。我假设它在table.py中的书写方式不正确,但是在这种情况下,我不知道如何正确书写。还是我需要做其他事情?
For some reason, I can't make the onclick event that makes the confirmation dialogue box appear, and it just goes straight to deleting. I assume that it's written incorrectly in tables.py, but I don't know how to write it correctly in that case. Or do I need to do something else?
推荐答案
看看渲染的html,例如使用浏览器检查上下文菜单选项。我认为您可能会看到使用的双引号存在问题。
Have a look at the rendered html, for example using your browsers inspect context menu option. I think you could see there that there is a problem with the double quotes you use.
onclick
属性用双引号括起来,但是作为参数传递给 confirm()
的消息也用双引号括起来。这导致您的浏览器将属性解释为 onclick = return Confirm(),并且忽略了无法理解是您的消息的胡言乱语。
The onclick
-attribute is enclosed by double quotes, but the message passed as argument to confirm()
is also enclosed by double quotes. This results in your browser interpreting the attribute as `onclick="return confirm(" and ignores the gibberish it cannot understand which is your message.
单引号将消息参数括在 confirm()
中,方法是使用您使用的语法将其转义( \'
),或使用像这样的三引号:
You can fix that by using single quotes to enclose the message argument to confirm()
, either by escaping them in the syntax you used (\'
), or by using triple quotes like this:
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为对话框放置click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!