2013-08-28 139 views
2

對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)), 
    ) 

回答

2

如果你保持你的urls.py事情是這樣的,這意味着你需要創建views.py內/ django_books/django_books/

在該文件中,創建一個名爲home新功能。或者,如果您在/ django_books/beer /中有任何功能,您可以從urls.py中引用它們。

所有的urls.py都會暴露一個python路徑到一個函數並在那裏路由一個URL。所以你可以看到你沒有在django_books/django_books中有一個名爲views的模塊或文件,這就是你失敗的原因。

+0

問題是教程沒有說我需要創建這個視圖文件。而且你預計我會問在哪個目錄中,項目或應用程序。幹得好,謝謝。 – smilebomb

+0

沒問題。對我來說理解urls.py中的url()函數中的第二個參數直接映射到一個python函數是有幫助的。 HTH。 – Jordan

+1

教程說:https://docs.djangoproject.com/en/dev/intro/tutorial03/ :) – alecxe

1

查看基本上是接收HTTP Request並返回HTTP Response Python函數。從docs

報價:

A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no 「magic,」 so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

此行url(r'^$', 'django_books.views.home', name='home'),urls.py點索引你的網站的home鑑於/ - 你應該創建它。

views.py創建一個名爲home Python函數:

from django.http import HttpResponse 
import datetime 

def home(request): 
    now = datetime.datetime.now() 
    html = "<html><body>It is now %s.</body></html>" % now 
    return HttpResponse(html) 

重新啓動開發服務器,並參觀http://127.0.0.1:8000

僅供參考,請仔細閱讀教程,part 3是關於處理網址和觀點的。

+0

謝謝你的迴應。我怎麼知道我是否有一個存在或不存在的觀點?更重要的是我該如何創建一個? – smilebomb

+0

@jefffabiny當然,請看到更新的答案。 – alecxe