Myurls.py在尝试读取或创建新项目时工作。但是,当尝试更新现有项目时,我会执行以下操作:app/uls.py:
...
path('update_goals/<int:id>', update_goals, name='update_goals'),
在视图中.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})
在模型中.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>
和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 %}