我正在练习数据的CRUDS,当我尝试编写删除功能时遇到一些错误。
在 timesheet.html 中:
{% for data in TR %}
<tr>
<th>{{data.month}}</th>
<th>{{data.worktime}}</th>
<th>{{data.email}}</th>
<th></th>
<th><a href="{% url 'data:delete' data.0 %}"><button type="button">刪除</button></a></th>
</tr>
{% endfor %}
在views.py中:
def delete(request, id):
with connection.cursor() as cursor:
sqldelete = """
delete from timesheet where idtimesheet = %s
"""
cursor.execute(sqldelete, (id,))
return redirect("/timesheet")
在 urls.py 中
urlpatterns = [
path('', views.timesheet, name='timesheet'),
# path('index/', views.index, name='index')
path('data_db/', views.data_db, name='data_db'),
path('delete/<int:idtimesheet>', views.delete, name="delete")
]
最佳答案
问题出在这里:
url“数据:删除”
问题是 Django 无法识别第一个“数据” - 它是您的应用程序的名称,还是您 include()
时在项目级 urls.py 中定义的命名空间应用程序 urls.py?
如果您引用的 urls.py 包含在更高级别的 urls.py 中,您应该能够通过包含一个元组来命名它,该元组定位包含 url_patterns 列表的模块和一个命名空间字符串。我的民意调查教程的完整条目:
re_path(r'^polls/', include(('polls.urls', 'polls'))),
如果你没有命名空间,那么你不需要data:
,只需将其url删除
关于python - Django -- 'data' 不是注册的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56436041/