我可以使用自定义Django模板标签将值传递给另一个视图吗?

时间:2018-08-04 03:50:58

标签: python django django-templates django-class-based-views

我目前正在使用modelformset_factory在基于更新类的视图中显示多个表单。对于一个字段,我正在使用django-popup-view-field 0.3.0显示一个弹出窗口,该弹出窗口将允许用户选择要在该字段中输入的值。

弹出窗口的创建者: popup.py

class SelectionReasonPopUpViews(DetailView):
    template_name = 'popups/selectionReason.html'
    model = AttackPattern
    context_object_name = 'attackpattern' 

然后通过以下方式将弹出窗口传递到表单中: form.py

class APRForm(ModelForm):
        class Meta:
            model=APR
            fields=['rating', 'selectionReason', ]

        selectionReason=PopupViewField(
            view_class=SelectionReasonPopUpViews,
            popup_dialog_title="Please select a reason for",
            required=True,    
            )
        def __init__(self, *args, **kwargs):
            super(APReviewForm, self).__init__(*args, **kwargs)
            self.helper = FormHelper()

    APRFormset = modelformset_factory(APR, form=APRForm, extra=0)

这是我创建更新视图的方式:views.py

class APRUpdate(UpdateView):
    ...

    def get_context_data(self, **kwargs):
        context = super(APRUpdate, self).get_context_data(**kwargs)
        context['formset'] = APRFormset(queryset=APR.objects.get(pk=< get the pk from url when editing>))
        return context

    def post(self, request, *args, **kwargs):
        ...

    def form_valid(self, formset):
        ...

    def form_invalid(self, formset):
        ...

然后我在此模板中呈现APRFormset(仅显示表单):APRUpdate.html

<form method="POST" enctype="multipart/form-data" style="margin-left: 40px; margin-right: 40px">
                            {% csrf_token %}            
                            {{ formset.management_form }}
                            {{ formset.non_form_errors }}           
                            {% for forma in formset.forms %}
                                <div class = "panel panel-primary">
                                    <div class = "panel-heading">
                                        <h3 class = "panel-title">{{forma.instance.atPat.APid}} : {{forma.instance.atPat.name}}</h3>
                                    </div>   
                                    <div class = "panel-body">
                                        {{ forma}}

                                    </div>
                                    <div class = "panel-footer">
                                        {{ forma.instance.suggestReason }}</div>
                                        <a href="#{#% url 'RecSys:apid' forma.instance.atPat.APid %#}"></a>
                                </div>  
                            {% endfor %}            
                            <input class="btn" type="submit" value="Update" />
                        </form>

我的问题是,如何将模板文件中的值传递给popup.py中的SelectionReasonPopUpViews类?我想在SelectionReasonPopUpViews类中使用该值进行处理。我想知道是否可以使用Django海关模板标签将 forma.instance.atPat.APid 值从APRUpdate.html传递到SelectionReasonPopUpViews。

另一个注意事项是,由于将modelformset_factory与不同的 forma.instance.atPat.APid 一起使用,我将在APRUpdate.html中显示相同的APR表单。当用户单击带有弹出窗口的字段时,该表单中相应的 forma.instance.atPat.APid 应该发送到SelectionReasonPopUpViews。

例如,如果我在APRUpdate.html中显示3个APR表单:

表格1:         forma.instance.atPat.APid = 1        弹出的字段 表格2:         forma.instance.atPat.APid = 4        弹出的字段 表格3:         forma.instance.atPat.APid = 7        弹出窗口

如果用户单击格式为1的具有弹出窗口的字段,则应传递给SelectionReasonPopUpViews的值应为1,并且如果格式为3的具有弹出窗口的字段,则应传递给SelectionReasonPopUpViews的值应为7。

0 个答案:

没有答案