Django新手启动服务器遇ImproperlyConfigured错误,移除include仍无效求助
Hey there! Let's sort out this Django URL issue you're hitting. That error message is pretty clear once you break it down: when you use namespace in the include() function, Django requires you to specify an app_name either in the included URL module or pass it directly as part of a 2-tuple. Removing include() altogether isn't the right fix here because that breaks the modular URL routing structure you're trying to build.
Here are two straightforward ways to fix this:
方法1:在次级urls.py中设置app_name
This is the most recommended approach since it keeps your URL module's responsibilities clear. Just add an app_name variable at the top of your learning_logs/urls.py:
# learning_logs/urls.py from django.conf.urls import url from . import views # Add this line to specify the app name app_name = 'learning_logs' urlpatterns = [ # Home page url(r'^$', views.index, name='index'), ]
Then your main urls.py can stay exactly as it was:
# 主urls.py from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'', include('learning_logs.urls', namespace='learning_logs')), ]
方法2:在主urls.py中传递包含app_name的元组
If you don't want to modify the secondary urls.py, you can pass a tuple directly to include() that includes both the URL module and the app name:
# 主urls.py from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), # Use a tuple (URL module, app name) instead of just the module name url(r'', include(('learning_logs.urls', 'learning_logs'), namespace='learning_logs')), ]
Either way, Django will correctly associate the namespace with its corresponding app name, and that error should disappear.
内容的提问来源于stack exchange,提问作者Yousef Khalid

