urls.py有效,但是当我使用<int:id>更新特定项目时,出现“当前路径与任何这些都不匹配”错误

尝试读取或创建新项目时,我的urls.py有效。但是,当尝试更新现有项目时,请执行以下操作: 在app / urls.py中:

...
path('update_goals/<int:id>',update_goals,name='update_goals'),

在views.py中:

def update_goals(request,id):
    item = Items.objects.get(id=id)
    form = ItemFilterForm(request.POST or None,instance=item)
    if form.is_valid():  # validate the form
        form.save()
        return redirect(list_items)
    return render(request,'items-form.html',{'form': form,'item': item})

在models.py中:

class Items(models.Model):
    id = models.AutoField(db_column='ID',primary_key=True)
    company = models.CharField(null=True,db_column='Company',max_length=40,blank=True)  # Field name made lowercase.
    ...
    class Meta:
        managed = False
        db_table = 'Items'
        verbose_name_plural = 'Items'

html:

        {% for i in item %}
        <a href="{url 'update_goals i.id'}">
                <li>{{i.company}}</li>
                ...
        </a>
        {% endfor %}

我用来读取当前项目和创建新项目的其他路径有效,但仅更新无效。

错误:

Page Not Found(404)
Request URL:    http://127.0.0.1:8000/%7Burl%20'update_goals%20i.id'%7D
Using the urlconf defined in app1.urls,Django tried these URL patterns,in this order:

...
update_goals/<int:id> [name='update_goals']
admin/
The current path,{url 'update_goals i.id'},didn't match any of these.

它可能与ID列有关,但我也尝试使用update_goals/<str:company>和abd i.company,并且仍然会出现相同的错误。

napkinnn 回答:urls.py有效,但是当我使用<int:id>更新特定项目时,出现“当前路径与任何这些都不匹配”错误

# You just need to change url in html file content

{% for i in item %}
<a href="{% url 'update_goals' id=i.id %}">
        <li>{{i.company}}</li>
        ...
</a>
{% endfor %}
本文链接:https://www.f2er.com/3080815.html

大家都在问