简体   繁体   中英

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. I created a modal based on some templates I was given, but I have no idea how to call it from views.py.

Basically, after a submission gets flagged with an 'N', the modal would need to appear, asking the user to give a value. 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. I'm not sure how to render it within the logic in this part.

views.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

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

{% 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

{% 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. 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

Had to switch to function based views. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM