Django表单 - get_context_data超出了最大递归深度

问题描述 投票:0回答:1

我的一个Django表单上的最大递归深度超出错误。

跟踪指向get_context_data,但我不确定为什么或底层问题是什么/可能是什么。我在所有其他表单上使用相同的代码而没有问题。

我读过这通常是由于URL,但我似乎无法找到它的错误。添加视图也可以使用几乎相同的代码

追溯是:

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/itapp/itapp/circuits/views.py" in dispatch
  317.         return super(EditFile, self).dispatch(*args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/edit.py" in get
  236.         return super(BaseUpdateView, self).get(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/edit.py" in get
  174.         return self.render_to_response(self.get_context_data())
  ...
File "/itapp/itapp/circuits/views.py" in get_context_data
  332.         context = EditFile().get_context_data(**kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in __init__
  43.         for key, value in six.iteritems(kwargs):

Exception Type: RecursionError at /circuits/file/edit/5/7/site_cl
Exception Value: maximum recursion depth exceeded

我的观点如下:

class EditFile(UpdateView):
    model = CircuitFiles
    form_class = FileForm
    template_name = "circuits/file_form.html"

    def dispatch(self, *args, **kwargs):
        self.site_id = self.kwargs['site_id']
        self.refer = self.kwargs['refer']
        self.circuit_id = self.kwargs['pk']
        self.site = get_object_or_404(SiteData, pk=self.site_id) 
        return super(EditFile, self).dispatch(*args, **kwargs)

    def get_form_kwargs(self, *args, **kwargs):
        kwargs = super().get_form_kwargs()
        kwargs['circuit_id'] = self.circuit_id        
        return kwargs

    def get_success_url(self, **kwargs):         
        return reverse_lazy("circuits:edit_circuit", args=(self.circuit_id, self.site_id, self.refer))

    def form_valid(self, form):
        form.instance.circuit = self.object.circuit
        return super(EditFile, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = EditFile().get_context_data(**kwargs)
        context['SiteID']=self.site_id
        context['circuit_id']=self.circuit_id
        context['SiteName']=self.site.location
        context['refer']=self.refer
        context['FormType']='Edit'
        context['active_circuits']='class="active"'

        return context

我的表格如下:

class FileForm(forms.ModelForm):
    class Meta:
        model = CircuitFiles
        fields = ['circuit', 'circuit_file','file_name',]
    def __init__(self, *args, **kwargs):
        site_id = kwargs.pop('site_id', None)
        refer = kwargs.pop('refer', None)
        self.is_add = kwargs.pop("is_add", False)
        super(FileForm, self).__init__(*args, **kwargs)
        self.fields['circuit'].required = False
        self.helper = FormHelper(self)
        self.helper.form_id = 'fileform'
        self.helper.form_method = 'POST'
        self.helper.layout = Layout(
            Div(    
                Div(
                    Div(
                        Div(HTML('<i class="fa fa-upload fa-fw"></i> Add File'), css_class='panel-heading'),
                        Div(
                            Field('circuit_file', placeholder='Circuit File'),
                            Field('file_name', placeholder='File Name'),
                            css_class='panel-body'
                        ),
                    css_class='panel panel-default',
                    ),
                css_class='col-lg-4'
                ),
            css_class='row'
            ),
            Div(
                Div(
                    HTML("""<input type="submit" name="submit" value="""),
                    HTML('"Add File"' if self.is_add else '"Update File"' ),
                    HTML(""" class="btn btn-primary"/>"""),
                    HTML("""<a href="{% url 'circuits:edit_circuit' circuit_id SiteID refer  %}" class="btn btn-primary">Cancel</a>"""),
                    HTML("""{% if object %}
                            <a href="{% url "circuits:delete_file" object.id SiteID %}"
                            class="btn btn-danger">
                            Delete <i class="fa fa-trash-o" aria-hidden="true"></i></button></a>
                            {% endif %}"""),
                    css_class='col-lg-12'
                    ),
                css_class='row'
                ),
        )

如果相关的是url模式:

url(r'^file/edit/(?P<pk>\d+)/(?P<site_id>[0-9]+)/(?P<refer>.+)$', views.EditFile.as_view(), name='edit_file'),
python django python-3.x django-forms
1个回答
4
投票
context = EditFile().get_context_data(**kwargs)

这条线不正确。你应该使用super()

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    ...