简体   繁体   English

如何在 Django 的 views.py 中从 class 调用“模态”?

[英]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.

相关问题 如何使用模态调用views.py的function<form action="“" ”> ?</form> - how to call function of views.py using modal <form action=“ ”>? 如何在 Django 中的 views.py 中调用 models.py 中的特定字段 - How to call specific fields from models.py in the views.py in Django 如何从 views.py 继承 django class 方法到 tasks.py 来安排 Celery 的作业? - How to inherit a django class method from views.py into tasks.py to schedule jobs with Celery? 从命令行调用views.py中的函数(django) - Call function in views.py from command line (django) 如何将视图从一个django views.py导入到另一个 - How to import views from one django views.py to another 如何在Django中从views.py重复调用函数? - How can I repeatedly call a function from a views.py in Django? 如何从模板文件夹中的ajax-html调用Django中views.py文件中的函数? - How make a call to a function in views.py file in Django from ajax-html in template folder? 如何在views.py 中从我的数据库中获取内容? [姜戈] - How to get the content from my database in views.py? [Django] 如何从views.py文件查询Django模型? - How to query Django model from views.py file? 如何从views.py中的Django模板获取变量的值? - How to get value in variable from Django template in views.py?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM