未解析的url引用(python django)

时间:2015-03-07 19:51:52

标签: python django 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:\Python34\lib\importlib\__init__.py in import_module, line 109
Python Executable:    C:\Python34\python.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',
)

1 个答案:

答案 0 :(得分:1)

如果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',
)