我最近开始使用Django,而且我在渲染一个" hello world"页面,问题是这样的:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^pages/', include('pages.urls')),
]
Django没有检测到我的views.index函数
这是代码:
的src / urls.py
from django.conf.urls import url
urlpatterns = [
url(r'', 'index', name="index"), #error
]
信息页/ urls.py
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render('index.html', context)
信息页/ views.py
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044D70D0>
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\src\urls.py", line 26, in <module>
url(r'^pages/', include('pages.urls')),
File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\pages\urls.py", line 5, in <module>
url(r'^$', views.index, name="index"),
AttributeError: 'module' object has no attribute 'index'
我想渲染index.html,我该怎么做?
---------------更新---------------
CMD错误
from pages import views
在urls.py中添加public int index(String character)
{
String[] array = {"Apple", "Banana", "Apple", "Orange"};
return Integer.parseInt(array.indexOf(character));
}
答案 0 :(得分:8)
在Django 1.8+中,您应该导入视图并在网址模式中包含callable。
from .views import index
urlpatterns = [
...
url(r'^$', index, name="index"),
请注意,第二个参数现在是index
,即我们导入的视图,而不是字符串'index'
。我也改变了正则表达式,使它只匹配索引页面。使用''
会匹配每个网址。
另一种选择是导入views
模块而不是单个视图。这样可以更轻松地包含其他视图,例如views.about
或views.contact
。
from . import views
urlpatterns = [
...
url(r'^$', views.index, name="index"),