gpt4 book ai didi

python - 组合表单和 model_formset 出现 NoReverseMatch 错误

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

我一直在尝试通过遵循此 tutorial 中的用例三来创建一个使用 model_formsets 组合父模型和子模型的 for 。 .

型号

class Shoppinglist(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(max_length=2000)
created = models.DateField(auto_now_add=True)
created_by = models.ForeignKey(User, related_name='lists', on_delete=models.CASCADE)
last_updated = models.DateTimeField(auto_now=True)

def __str__(self):
return self.name

class Item(models.Model):
name = models.CharField(max_length=80, unique=True)
amount = models.IntegerField(default=1)
shoppinglist = models.ForeignKey(Shoppinglist, on_delete=models.CASCADE)

def __str__(self):
return self.name

网址

urlpatterns = [
url(r'^shoppinglists/(?P<pk>\d+)/$', views.shoppinglist_list, name='shoppinglist_list'),
url(r'^shoppinglists/new/$', views.create_shoppinglist_with_items, name='shoppinglist_new'),
]

表单

class ShoppingListForm(forms.ModelForm):
description = forms.CharField(
widget=forms.Textarea(
attrs={'rows': 5, 'placeholder': 'Tell us about your list?'}
),
max_length=4000,
help_text='The max length of the text is 4000 characters.'
)

class Meta:
model = Shoppinglist
fields = ['name', 'description']

ItemFormset = modelformset_factory(
Item,
fields=('name', 'amount'),
extra=1,
widgets={
'name': forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Enter Item Name here'
}
)
},
can_delete=True

)

查看

@login_required
def create_shoppinglist_with_items(request):
template_name = 'list_with_items.html'
if request.method == 'GET':
listform = ShoppinglistForm(request.GET or None)
formset = ItemFormset(queryset=Item.objects.none())
elif request.method == 'POST':
listform = ShoppinglistForm(request.POST)
formset = ItemFormset(request.POST)
if listform.is_valid() and formset.is_valid():
shoppinglist = listform.save(commit=False)
shoppinglist.created_by = request.user
shoppinglist = listform.save()
for form in formset:
item = form.save(commit=False)
item.shoppinglist = shoppinglist
item.save()
return redirect('shoppinglist_list', pk=shoppinglist.pk)
return render(request, template_name, {
'listform': listform,
'formset': formset,
})

模板

{% block content %}
<form method="post">
{% csrf_token %}

<label>List Name</label>
{{ listform.name }}
{% if listform.first_name.errors %}
{% for error in listform.first_name.errors %}
{{ error|escape }}
{% endfor %}
{% endif %}

<label>Description</label>
{{ listform.description }}
{% if listform.description.errors %}
{% for error in listform.description.errors %}
{{ error|escape }}
{% endfor %}
{% endif %}

{{ formset.management_form }}

{% for form in formset %}
<div class="item-formset">
{{ form.amount }}
{% if form.amount.errors %}
{% for error in form.amount.errors %}
{{ error|escape }}
{% endfor %}
{% endif %}

{{ form.name }}
{% if form.name.errors %}
{% for error in form.name.errors %}
{{ error|escape }}
{% endfor %}
{% endif %}
</div>
{% endfor %}

{% if formset.non_form_errors %}
{% for error in formset.non_form_errors %}
{{ error|escape }}
{% endfor %}
{% endif %}

<div class="row spacer">
<button type="submit" class="btn btn-block btn-primary">Create</button>
</div>
</form>
{% endblock %}

{% block extra_js %}
<script>
$('.item-formset').formset({
addText: 'add item',
deleteText: 'remove'
});
</script>
{% endblock %}

我修改了代码,以便用户将被带到新创建的父模型,但当我转到该页面时,我收到 NoReverseMatch 错误。

Reverse for 'shoppinglist_list' with arguments '('',)' not found. 1 pattern(s) tried: ['shoppinglists/(?P<pk>\\d+)/$']

最佳答案

来自docs :

If you call save() with commit=False, then it will return an object that hasn’t yet been saved to the database. In this case, it’s up to you to call save() on the resulting model instance.

执行shoppinglist = listform.save(commit=False)后,您在shoppinglist变量中获得了模型实例。

所以你应该做 shoppinglist = shoppinglist.save() 而不是 shoppinglist = listform.save()

保存模型实例后,您可以访问shoppinglist.pk

关于python - 组合表单和 model_formset 出现 NoReverseMatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456544/

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