网上稍微找了一下,然后自己看了一些文档,在《python入门到实践》这本书中原来的
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
换成
from django.urls import path
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', "learning_logs"), namespace="learning_logs")),
]
就可以正确运行了。
顺手把后面扽代码给出来(urls.py views.py)其实和书上是一样的方便大家复制粘贴。。。
#定义learning_logs的URL模式
from django.conf.urls import url
from . import views
urlpatterns=[
#主页
url(r'^$',views.index,name='index')
]
from django.shortcuts import render
# Create your views here.
def index(request):
'''学习笔记的主页'''
return render(request,'learning_logs/index.html')
再把html写一些
<p>Learning Log</p>
<p>Learning log helps you keep track of your learning, for any topic you're
learning about!</p>
然后再服务器重启一下
再次输入
就可以看到
小筑:我贴一下貌似的源代码吧,从第6行看出来,起码arg也是一个元组,这一点就可以给出足够多的信息了
def include(arg, namespace=None):
app_name = None
if isinstance(arg, tuple):
# Callable returning a namespace hint.
try:
urlconf_module, app_name = arg
except ValueError:
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that '
'provides a namespace.'
)
raise ImproperlyConfigured(
'Passing a %d-tuple to include() is not supported. Pass a '
'2-tuple containing the list of patterns and app_name, and '
'provide the namespace argument to include() instead.' % len(arg)
)
else:
# No namespace hint - use manually provided namespace.
urlconf_module = arg
if isinstance(urlconf_module, str):
urlconf_module = import_module(urlconf_module)
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
app_name = getattr(urlconf_module, 'app_name', app_name)
if namespace and not app_name:
raise 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.',
)
namespace = namespace or app_name
# Make sure the patterns can be iterated through (without this, some
# testcases will break).
if isinstance(patterns, (list, tuple)):
for url_pattern in patterns:
pattern = getattr(url_pattern, 'pattern', None)
if isinstance(pattern, LocalePrefixPattern):
raise ImproperlyConfigured(
'Using i18n_patterns in an included URLconf is not allowed.'
)
return (urlconf_module, app_name, namespace)
祝大家好运!