产生错误的可能原因:用到了reverse这个函数
from django.urls import reverse
def xxx(request):
return redirect(reverse('goods:index'))
报错如下:
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported.
Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
摘自django 官网的论述:
项目与应用
项目和应用程序之间有什么区别?应用程序是执行某些操作的Web应用程序 - 例如,Weblog系统,公共记录数据库或简单的轮询应用程序。项目是特定网站的配置和应用程序的集合。项目可以包含多个应用程序。一个应用程序可以在多个项目中。
假设我们的项目ShopWeb,那么我们 goods 就是一个应用程序,在goods这个应用程序的目录下的 urls.py 中注册一个app_name,即可解决问题
代码如下:
from django.urls import path
from . import views
app_name = 'goods'
urlpatterns = [
path('', views.index, name='index')
]