簡體   English   中英

urls.py 有效,但是當我使用<int:id>更新特定項目然后我得到“當前路徑不匹配任何這些”錯誤</int:id>

[英]urls.py works but when I use <int:id> to update a particular item then I get "Current path didn't match any of these" error

我的 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 ,但它仍然給出相同的錯誤。

# You just need to change url in the html file content

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM