我有一个未解决的 URL 请求,不太确定是什么导致了这个问题。
我正在我的应用程序上设置一个主页以获取上下文
我对 views.py 的看法:
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, "homepage template.html")
我在 urls.py 的网址:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
url(r'^$', 'homepage.views.home'),
enter code here
浏览器中给出的错误:
我No module named 'homepage' Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 1.7.5 Exception Type: ImportError Exception Value: No module named 'homepage' Exception Location: C:Python34libimportlib__init__.py in import_module, line 109 Python Executable: C:Python34python.exe Python Version: 3.4.3 Python Path: ['C:\Users\jodie\Desktop\NtokloMH', 'C:\Windows\SYSTEM32\python34.zip', 'C:\Python34\DLLs', 'C:\Python34\lib', 'C:\Python34', 'C:\Python34\lib\site-packages'] Server time: Sat, 7 Mar 2015 19:10:33 +0000
以为我通过 views.py 和 urls.py 代码定义了模块,但 pycharm 告诉我它无法解析 URL。
根据要求:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicsearch',
)
如果musicsearch
是你在 django 中自己制作的唯一已安装的应用程序,并且 views.py 文件在该目录中,则
urlpatterns = patterns('',
# Examples:
url(r'^$', 'homepage.views.home'),
enter code here
应该是
urlpatterns = patterns('',
# Examples:
url(r'^$', 'musicsearch.views.home'),
enter code here
否则,如果homepage
是确实存在的应用程序,则需要将其添加到INSTALLED_APPS
中,如下所示:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicsearch',
'homepage',
)