gpt4 book ai didi

python - 呈现嵌入式表单集 management_form 时出现 django mongodbforms 异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:28 25 4
gpt4 key购买 nike

我在我的 django 项目中使用 mongodbforms 连接到 MongoDB。我想使用嵌入式表单集来填充嵌入式字段列表。当我只尝试在表单集中呈现一个表单时,一切正常。但是,当我设置 extra=3 并尝试为嵌入式表单集呈现 management_form 时,出现异常。这是我的 model.py:

from mongoengine import *
from MongoNotes.settings import DBNAME

connect(DBNAME)

class CollectionField(EmbeddedDocument):
name = StringField(max_length=120, required=True)

class Collection(Document):
name = StringField(max_length=120, required=True)
collection_fields = ListField(EmbeddedDocumentField(CollectionField))

这是我的表单和一个表单集(请注意 extra=3 - 这会导致执行:

# coding=utf-8
from crispy_forms.helper import FormHelper
from mongodbforms import embeddedformset_factory, EmbeddedDocumentForm
from MongoNotes.apps.Notes.models import CollectionField, Collection

class FieldForm(EmbeddedDocumentForm):
class Meta:
document = CollectionField
embedded_field_name = 'collection_fields'

fields = ['name',]

def __init__(self, *args, **kwargs):
super(FieldForm, self).__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.form_id = 'collection_field_form'
self.helper.form_tag = False

FieldFormSet = embeddedformset_factory(CollectionField, Collection, form=FieldForm, extra=3)

这是一个模板:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Add collection</h1>
<form method="post" class="form-horizontal">
{{ field_formset.management_form }}
</form>
</body>
</html>

为了完整起见,这是我的观点:

# coding=utf-8
from django.views.generic import TemplateView
from MongoNotes.apps.Notes.forms import FieldFormSet
from models import Collection

class AddCollectionView(TemplateView):
template_name = 'add_collection.html'

def get_context_data(self, **kwargs):
context = super(AddCollectionView, self).get_context_data(**kwargs)
context['field_formset'] = FieldFormSet(prefix='field', parent_document=Collection())
return context

运行这个给我一个异常(exception):

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'MongoNotes.apps.Notes',
'crispy_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

Template error:
In template C:\projects\mongodbforms_error\MongoNotes\apps\Notes\templates\add_collection.html, error at line 8
list index out of range
1 : <!DOCTYPE html>
2 : <html>
3 : <head>
4 : </head>
5 : <body>
6 : <h1>Add collection</h1>
7 : <form method="post" class="form-horizontal">
8 : {{ field_formset.management_form }}
9 : </form>
10 : </body>
11 : </html>

Traceback:
File "C:\(...)\mongodbforms_error\lib\site-packages\django\core\handlers\base.py" in get_response
137. response = response.render()
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\response.py" in render
105. self.content = self.rendered_content
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\response.py" in rendered_content
82. content = template.render(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\debug.py" in render
88. output = self.filter_expression.resolve(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in resolve
585. obj = self.var.resolve(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in resolve
735. value = self._resolve_lookup(context)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\template\base.py" in _resolve_lookup
765. current = current[bit]
File "C:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in __getitem__
69. return self.forms[index]
File "C:\(...)\mongodbforms_error\lib\site-packages\django\utils\functional.py" in __get__
49. res = instance.__dict__[self.func.__name__] = self.func(instance)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in forms
133. forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
File "C:\(...)\mongodbforms_error\lib\site-packages\mongodbforms\documents.py" in _construct_form
930. form = super(EmbeddedDocumentFormSet, self)._construct_form(i, **defaults)
File "C:\(...)\mongodbforms_error\lib\site-packages\django\forms\formsets.py" in _construct_form
157. form = self.form(**defaults)
File "C:\projects\mongodbforms_error\MongoNotes\apps\Notes\forms.py" in __init__
15. super(FieldForm, self).__init__(*args, **kwargs)
File "C:\(...)\mongodbforms_error\lib\site-packages\mongodbforms\documents.py" in __init__
548. instance = getattr(parent_document, self._meta.embedded_field)[position]
File "C:\(...)\mongodbforms_error\lib\site-packages\mongoengine\base\datastructures.py" in __getitem__
96. value = super(BaseList, self).__getitem__(*args, **kwargs)

Exception Type: IndexError at /
Exception Value: list index out of range

我尝试过自己调试,但我的 Python 不够熟练,无法找出问题的根源。

最佳答案

Django MongoDBForms 不再由它的原始创建者维护,现在已经 fork 成一堆乱七八糟的 fork 。我对其中一个的嵌入式文档格式集代码进行了一些小修复。我不确定他们是否会解决您的问题,但可能有用的是使用该项目最近更新的分支之一而不是原始项目。

我不能专门推荐一个来解决你的问题,但我注意到这个分支已经采用了我所做的更改并进一步推进了它们。 https://github.com/baffolobill/django-mongodbforms

关于python - 呈现嵌入式表单集 management_form 时出现 django mongodbforms 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25371679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com