對django很新穎。我使用的是1.5.2版本,我只是做了全新的安裝。我正在使用django開發服務器;我會在路上轉向Apache,但是我希望在採取這一步之前瞭解django的MVC方法的特殊風格。在django中創建一個視圖
所以我通過終端在我的項目目錄(django_books)中用`python manage.py runserver 0.0.0.0:8000'啓動django服務器。我得到這個錯誤:
ViewDoesNotExist at/
Could not import django_books.views.home. Parent module django_books.views does not exist.
所以我的看法不存在。我的view.py文件是空的,因爲我下面的教程沒有包含一個。我不確定這是否是問題。如果是這樣,我該如何創建這個文件(裏面包含什麼內容)?
目錄結構:
django_books
beer (from the tutorial lol)
migrations
__init__.py
models.py
views.py
random_book
(same as beer above)
django_books (this is my actual django project, beer and random_book are apps)
__init__.py
settings.py
urls.py
wsgi.py
media
.gitignore
manage.py
requirements.txt (output from pip freeze command)
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'^$', 'django_books.views.home', name='home'),
# url(r'^django_books/', include('django_books.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)),
)
問題是教程沒有說我需要創建這個視圖文件。而且你預計我會問在哪個目錄中,項目或應用程序。幹得好,謝謝。 – smilebomb
沒問題。對我來說理解urls.py中的url()函數中的第二個參數直接映射到一個python函數是有幫助的。 HTH。 – Jordan
教程說:https://docs.djangoproject.com/en/dev/intro/tutorial03/ :) – alecxe