使用Javascript / Django

时间:2019-05-09 11:14:22

标签: javascript python ajax django

我目前有以下模板,该模板在表中的每一行都有一个下拉列表。只要选择/单击这些下拉菜单之一中的一个,它就应该开机自检。但是,我不明白在View类中需要做什么才能将其发送到数据库。

                        <div class="dropdown">
                        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"
                                aria-expanded="false">Change Status</button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <button onclick="status_update("In Session", {{a.patient_id}})" class="dropdown-item">In Session</button>
    <button onclick="status_update("Complete", {{a.patient_id}})" class="dropdown-item">Complete</button>
    <button onclick="status_update("Canceled", {{a.patient_id}})" class="dropdown-item">Canceled</button>
                </div>
                        </div>
                    </td>
            </div></tr>{% endfor %}
        </table>
            </div>
</div>
{% endblock body %}
{% block script %}
<script type="text/javascript">
    function update_status(status, id) {
        var patient_pid = id;
        $.ajax({
            type: "POST",
            url: "{% url 'dashboard' %}",
            data: {'patient_id': id, 'status': status, csrfmiddlewaretoken:'{{csrf_token}}'},
            success: function() {
                $.ajax({
                    type: "POST",
                    url: "{% url 'dashboard' %}",
                    data: {'patient_id': id, 'status': status, csrfmiddlewaretoken:'{{csrf_token}}'}
                });
            }
        });
    }
    </script>

与该模板匹配的视图定义如下:

class DashboardView(TemplateView):
    template_name = 'dashboard.html'

    def get_context_data(self, **kwargs):
        kwargs = super(DashboardView, self).get_context_data(**kwargs)
        # Hit the API using one of the endpoints just to prove that we can
        # If this works, then your oAuth setup is working correctly.
        # appointments = self.make_api_request()

        #get all the appointments/update database if neccessary
        Appointment.objects.all()
        #only get todays from database
        appts = Appointment.objects.get_today()
        kwargs['appointments'] = appts
        return kwargs

如何获取此信息以实际更新数据库?

0 个答案:

没有答案