2017-07-04 10 views
0

Django Tables2でクリックイベントを作成しようとしているので、行の削除リンクをクリックするたびにその行を削除する前にダイアログボックスが表示されます。ここに私のコードは次のとおりです。Django Tables2でダイアログボックスのクリックイベントを入力する

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で書かれていると仮定しますが、私はその場合正しく書いているか分かりません。あるいは何か他のことをする必要がありますか?

答えて

0

ブラウザのコンテキストメニューオプションを使用して、レンダリングされたHTMLを見てみましょう。あなたが使用している二重引用符に問題があることがわかりました。

-adributeは二重引用符で囲まれていますが、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>''' 
関連する問題