我正在与django合作,我试图让一个简单的应用程序正常运行。
该应用应该提供一个简单的“Hello world”视图,但my_app/urls.py
无法从my_app/views.py
导入方法。
我的应用名为 SocialUrl
这是my-project/urls.py
。
from django.conf.urls import patterns, include, url
# from SocialUrl.views import test
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/su/', include('SocialUrl.urls')),
# url(r'^$', test),
)
以下是my-app/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^test$', views.test()),
)
以下是my-app/views.py
from django.shortcuts import render
from django.template import RequestContext
def test(request):
return render(request, 'test.html', RequestContext(request, {}))
注意: - my-project/urls.py
中的测试行正常运行。
我的文件夹结构如下所示:
my-project
|- my-project
|- my-app
我已在my-project/settings.py
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/'),
os.path.join(BASE_DIR, 'SocialUrl', 'templates').replace('\\', '/'),
)
我收到以下错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.6.2
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'SocialUrl')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\core\handlers\base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "C:\Users\Sverker\Dropbox\Coomba\SbrgCoomba\SbrgCoomba\urls.py" in <module>
13. url(r'^/su/', include('SocialUrl.urls')),
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\conf\urls\__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "C:\Users\Sverker\Dropbox\Coomba\SbrgCoomba\SocialUrl\urls.py" in <module>
7. url(r'^$', test),
Exception Type: NameError at /
Exception Value: name 'test' is not defined
我在from . import views
尝试了my-app/urls.py
行的各种组合
例如。 from views import test
编辑:
仍然失效my-app/urls.py
from django.conf.urls import patterns, url
from views import test
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', test),
)
答案 0 :(得分:1)
问题是你的应用urls.py
确实有这一行:
url(r'^$', test)
错误表明test
未定义。只需定义(导入)它:
from views import test
答案 1 :(得分:0)
我认为这可能会导致错误:
url(r'^/su/', include('SocialUrl.urls')),
删除前导斜杠,您将只有:
url(r'^su/', include('SocialUrl.urls')),
请告诉我这是否适合您
答案 2 :(得分:0)
我不确定这是否是问题 - 但通常我会按照此模式查看我的应用级网址文件。
主要项目urls.py文件
from django.conf.urls import patterns, include, url
urlpatterns = patterns('apps.main.views',
url(r'', include('apps.main.urls')),
)
应用级别urls.py文件
from django.conf.urls import patterns, url
urlpatterns = patterns('',
# ex: /polls/
url(r'^test$', 'views.tests'),
)
请注意,您应该使用urlpatterns +=
或urlpatterns =
,具体取决于之前是否已定义。