[英]How to call a “modal” from class within views.py in Django?
I have a page with a form that is used to track entry/leave times.我有一个带有用于跟踪进入/离开时间的表单的页面。 Currently, entries with errors (missing enter or exit) get flagged from views.py.目前,有错误的条目(缺少输入或退出)会从views.py 中标记出来。 I created a modal based on some templates I was given, but I have no idea how to call it from views.py.我根据给定的一些模板创建了一个模式,但我不知道如何从 views.py 中调用它。
Basically, after a submission gets flagged with an 'N', the modal would need to appear, asking the user to give a value.基本上,在提交被标记为“N”后,模式将需要出现,要求用户给出一个值。 Right now the modal just says "Hi" because I'm trying to get it to appear first, before diving into the functionality of it.现在,模态只是说“嗨”,因为我试图让它首先出现,然后再深入研究它的功能。
Below is my views.py, with a comment of where the modal call would need to happen.下面是我的views.py,附有关于模态调用需要发生的地方的注释。 I'm not sure how to render it within the logic in this part.我不确定如何在这部分的逻辑中呈现它。
views.py视图.py
class EnterExitArea(CreateView):
model = EmployeeWorkAreaLog
template_name = "operations/enter_exit_area.html"
form_class = WarehouseForm
def form_valid(self, form):
emp_num = form.cleaned_data['employee_number']
area = form.cleaned_data['work_area']
station = form.cleaned_data['station_number']
if 'enter_area' in self.request.POST:
form.save()
EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True) & Q(time_in__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_in=datetime.now())
# If employee has an entry without an exit and attempts to enter a new area, mark as an exception 'N'
if EmployeeWorkAreaLog.objects.filter(Q(employee_number=emp_num) & Q(time_out__isnull=True) & Q(time_exceptions="")).count() > 1:
first = EmployeeWorkAreaLog.objects.filter(employee_number=emp_num, time_out__isnull=True).order_by('-time_in').first()
EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(time_out__isnull=True))).exclude(pk=first.pk).update(time_exceptions='N')
# Here's where the modal would need to be called because the record was flagged
return HttpResponseRedirect(self.request.path_info)
class UpdateTimestampModal(CreateUpdateModalView):
"""
Modal to request estimated entry/exit time for manager approval
"""
main_model = EmployeeWorkAreaLog
model_name = "EmployeeWorkAreaLog"
form_class = WarehouseForm
template = 'operations/modals/update_timestamp_modal.html'
modal_title = 'Update Timestamp'
urls.py网址.py
urlpatterns = [
url(r'enter-exit-area/$', EnterExitArea.as_view(), name='enter_exit_area'),
url(r'update-timestamp-modal/(?P<main_pk>\d+)/$', UpdateTimestampModal.as_view(), name='update_timestamp_modal'),
]
enter_exit_area.html enter_exit_area.html
{% extends "base.html" %}
{% load core_tags %}
{% block main %}
<form id="warehouseForm" action="" method="POST" novalidate >
{% csrf_token %}
<div>
<div>
{{ form.employee_number }}
</div>
<div>
{{ form.work_area }}
</div>
<div>
{{ form.station_number }}
</div>
</div>
<div>
<div>
<button type="submit" name="enter_area" value="Enter">Enter Area</button>
<button type="submit" name="leave_area" value="Leave">Leave Area</button>
</div>
</div>
</form>
{% modal id="create-update-modal" title="Update Timestamp" primary_btn="Submit" default_submit=True %}
{% endblock main %}
update_timestamp_modal.html update_timestamp_modal.html
{% load core_tags %}
<form id="create-update-form" method="post" action="{% url "operations:update_timestamp_modal" main_object.id %}">
{% csrf_token %}
<label>UPDATE TIMESTAMP</label>
<div class="row">
<div class="form-group col-xs-6">
<h1>Hi!!</h1>
</div>
</div>
</form>
I found this on some documentation about the modals that seemed relevant to calling it to open, adding just in case:我在一些关于似乎与调用它打开相关的模态的文档中发现了这一点,以防万一:
so the modal template tag will basically render an empty modal, and then when you call ajaxOpen you can then specify a URL that will render the dynamic HTML that will go into the empty modal.所以模态模板标签基本上会呈现一个空模态,然后当你调用 ajaxOpen 时,你可以指定一个 URL ,它将呈现动态 HTML ,它将 Z34D1F91FB2E514B8576FAB1A75A89 变为空模态。 So in this case you'd probably want something like this in the call:因此,在这种情况下,您可能希望在通话中出现这样的情况:
modals.ajaxOpen($('#create-update-modal'), undefined, $('#some-hidden-div').data('url'), function() {alert("it worked yay");});
Assuming that you put the url in a hidden div in the html somewhere since you wouldn't want to just hard-code it in the JS, but the url could also be returned from other ajax calls or whatever Assuming that you put the url in a hidden div in the html somewhere since you wouldn't want to just hard-code it in the JS, but the url could also be returned from other ajax calls or whatever
Had to switch to function based views.不得不切换到基于 function 的视图。 In order to render modal, created a variable rendered in views.py that, if it exits, then it calls the modal from the main html.为了呈现模态,在views.py中创建了一个呈现的变量,如果它退出,那么它从主html调用模态。
return render(request, "operations/enter_exit_area.html", {
'form': form,
'enter_without_exit': enter_without_exit,
'exit_without_enter': exit_without_enter,
})
The enter_without_exit and exit_without_enter render two types of modals if they return anything other than None from views.py logic enter_without_exit 和 exit_without_enter 呈现两种类型的模式,如果它们从 views.py 逻辑中返回 None 以外的任何内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.