python - Django -- 'data' 不是注册的命名空间

标签 python django

我正在练习数据的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/

相关文章:

python - GDAL Python 创建轮廓线

python - Django 应用程序 SSL 套接字连接到固件

python - Django prefetch_related 优化查询但仍然很慢

python - 在 Wagtail 管理员中更改页面所有者

python - 如何阻止Python的Sys模块的 "stdout"和 "stderr"write方法返回写入的字符数

python - Python 中的数据框 - 匹配两个数据框之间的多列行

python - 在 Python/GeoPandas 中组合 shapefile

python - Django oscar 添加标准运输方式

社交网络的 Java 框架

python - Django:计算与查询匹配的内容标签