第一次Django设置importError

时间:2013-04-26 21:31:32

标签: python django

请保持温和 - 这是我第一次使用Python和Django

我认为这就是一切,但如果我还能提供其他任何内容,请告诉我。

我收到了这个错误:

ImportError at /
No module named models
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.5.1
Exception Type: ImportError
Exception Value:    
No module named models
Exception Location: /Users/drewwyatt/Sites/Python/FirstBlog/FirstBlog/blog/views.py in <module>, line 3
Python Executable:  /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.4
Python Path:    
['/Users/drewwyatt/Sites/Python/FirstBlog',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-macosx-10.6-intel.egg',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 '/Library/Python/2.7/site-packages']
Server time:    Fri, 26 Apr 2013 16:22:15 -0500

代码......

/FirstBlog/FirstBlog/blog/views.py

from django.shortcuts import render_to_response

from blog.models import posts

def home(request):
    return render_to_response('index.html')

/FirstBlog/FirstBlog/blog/models.py

from django.db import models

# Create your models here.

class posts(models.Model):

    author = models.CharField(max_length = 30)
    title = models.CharField(max_length = 100)
    bodytext = models.TextField()
    timestamp = models.DateTimeField()

/FirstBlog/FirstBlog/settings.py(第110-115行)

TEMPLATE_DIRS = (
    "blog/templates",
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

/FirstBlog/FirstBlog/urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'FirstBlog.blog.views.home', name='home'),
    # url(r'^FirstBlog/', include('FirstBlog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

2 个答案:

答案 0 :(得分:1)

urls.py 看来,正确的导入路径为FirstBlog.blog.<...>,请尝试并替换:

from blog.models import posts

from FirstBlog.blog.models import posts
views.py 中的

答案 1 :(得分:1)

django错误非常有用!

Exception Location: /Users/drewwyatt/Sites/Python/FirstBlog/FirstBlog/blog/views.py in <module>, line 3

尝试:

<强> FirstBlog / FirstBlog /博客/ views.py

from django.shortcuts import render_to_response

from FirstBlog.blog.models import posts  

def home(request):
    return render_to_response('index.html')