Windows下django 1.7 +python3.4.2構築記録2

8475 ワード

1、カスタムページ
現在の時刻を表示するページを書く
views.pyファイルにテンプレートを使用するモジュールを追加します.全体の内容は次のとおりです.
#coding=utf-8

from django.shortcuts import render

from django.template.loader import get_template

from django.template import Context

import datetime

#import sys 

#reload(sys) 

#sys.setdefaultencoding('cp936')

# Create your views here.

from django.shortcuts import render,render_to_response

from django.http import HttpResponse

# Create your views here.

a="  "

def hello(request):

        return HttpResponse(a)

def current_datetime(request):

        now = datetime.datetime.now()

        t = get_template('current_datetime.html') 

        html = t.render(Context({'current_date': now}))

        return HttpResponse(html)

ここでcurrent_datetime.htmlは私达が使うテンプレートで、自分で多くの効果を追加することができて、基本的な内容は
<!doctype html>

<html lang="en">

 <head>

  <meta charset="UTF-8">

  <meta name="Generator" content="EditPlus®">

  <meta name="Author" content="">

  <meta name="Keywords" content="">

  <meta name="Description" content="">

  <title>Document</title>

 </head>

 <body>

  it is now {{ current_date }}

 </body>

</html>

ここでcurrent_dateはデータ変換に相当する正確なスイッチです
urls.pyコンテンツはもちろんモジュールバインドの文も追加します
from django.conf.urls import patterns, include, url

from django.contrib import admin

from views.views import hello

from views.views import current_datetime

urlpatterns = patterns('',

    # Examples:

    # url(r'^$', 'MyDjango.views.home', name='home'),

    # url(r'^blog/', include('blog.urls')),

    #url(r'^admin/', include(admin.site.urls)),

        url(r'^hello/$', hello),

        url(r'^current_datetime/$', current_datetime),

)

これをやり終えてブラウザに入力します
http://127.0.0.1:8000/current_datetime/、エラーを報告します
TemplateDoesNotExist at/current_datetime/
その中にはUsing loader djangoという言葉がある.template.loaders.app_directories.Loader:
D:\Python34\lib\site-packages\django\contrib\admin\templates\current_datetime.html (File does not exist)
D:\Python34\lib\site-packages\django\contrib\auth\templates\current_datetime.html (File does not exist)
テンプレートファイルはこの2つのフォルダの1つの下に置くべきであることが明らかになり、ファイルを移動します.
そして私たちはsettingsでpyにこのような文を追加するにはbase_に置く必要があります.dir後方:
TEMPLATE_DIRS=( os.path.join(BASE_DIR,'templates'),)

問題解決basedirの位置決め方法俺のバカなやり方だpyには、次の設定があります.
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

dbをsqlite 3のディレクトリが見つかりました.他に方法があるはずです.検討を歓迎します.次回はデータベースやauth認証を研究します.