- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试通过遵循此 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/
我正在尝试添加我单独制作的用户应用程序,然后尝试将其添加到我的博客中。但问题是每次我尝试添加这个东西都会搞砸,就像我现在遇到的这个错误一样。 raise NoReverseMatch(msg) dja
我有以下设置: /landing_pages views.py urls.py 在urls.py中,当我尝试访问/competition时,我有以下内容: from django.conf.u
我在浏览器中遇到此异常,我已经看到多达 20 篇与此错误相关的帖子,但我找不到任何解决方案。我是 Django 的新手,请帮助我,在此先感谢。 我的项目名/urls.py urlpatterns =
我有这个问题,被困了几个小时。 NoReverseMatch位于/ 找不到参数“('',)”的“hotovo”。尝试了1个模式:['hotovo/(?P [^/] +)$']。 网址: urlpatt
所以我做了很多博客教程中的一个,现在我正在开发自己的应用程序。我遇到了这个错误: Reverse for '' with arguments '()' and keyword arguments '{
使用 Django 1.10.4。我有一个模型 Stream,我为其创建了 CreateView。当通过管理面板创建对象时,一切正常,但是当我使用 CreateView 表单时,创建了一个对象(在管理
为什么我会得到 Reverse for 'explorer_js' not found. 'explorer_js' is not a valid view function or pattern n
我正在构建一个函数,以在用户单击链接时增加模型,但即使我传递了所有参数,我仍然无法解决此问题。 Reverse for 'karma' with keyword arguments '{'token'
我正在尝试向 View 发送 AJAX 发布请求,但由于某种原因我收到了 Http500 错误。 这是 AJAX 函数: function update_coins() { $.ajax({
我不明白这是怎么回事?我试图制作一个项目的详细信息页面,例如 http://tutorial.djangogirls.org/en/extend_your_application/index.html
我正在 django 1.6(和 python 2.7)中制作一个简单的登录应用程序,但我在开始时遇到错误,无法继续。 这是网站的 url.py from django.conf.urls impor
调试了一段时间后,我发现了错误是什么,但我不知道如何修复它。 我有一个名为“ver_caja”的 urlConf ' 谁接收一个 caja 对象的 id 作为参数,然后调用泛型 object_deta
刚开始使用 Django,但遇到了一些困难——我决定尝试编写一个简单的博客引擎,同时引用 django-basic-apps 库。 在 blog/urls.py 中,我有这个条目按日期映射到实际的帖子
我正在开发一个简单的应用程序,宠物用户可以在其中创建关于他们宠物的板并在板上显示图片。 我正在尝试创建一个功能,用户可以点击他们的图板,将他们重定向到他们的图板,该图板将显示他们所有的宠物图片。 当我
我在 View 中有两种方法 create 和 update ,其中 update 接受一个参数,而 create 不接受任何参数。我决定将它们变成只有一个函数 update_create 因为它们没
我遇到了这个错误,但似乎无法弄清楚。我直接从以前的 Django 项目复制它,因此造成了部分困惑。 TemplateSyntaxError at Caught NoReverseMatch while
我在让 password_Reset_confirm 位工作时遇到问题。 网址: (r'^password_reset/$', 'django.contrib.auth.views.password_
目前 Django URL 标记出现 NoReverseMatch 错误。一直在关注 Django 权威指南、Django 文档,并在此处和互联网上进行搜索 网址: url(r'^test/', Se
我又花了一个晚上在这上面...有人可以帮忙吗? 我正在尝试在 http://lightbird.net/dbe/todo_list.html 复制 kickstart 示例这似乎是为早期版本的 Dja
我在尝试使用 url 标记链接到 View 时遇到了这个错误。错误发生在这一行: {% for algorithim in algorithims %} 在模板中。 不太确定我哪里出错了。我想我已经附
我是一名优秀的程序员,十分优秀!