2016-12-02 23 views
0

私はDjangoを開始しています。Django - テンプレートに私のvarを表示していません

私のテンプレートに私のvarを渡そうとしていますが、私のブラウザには表示されますが動作しません。

from django.conf.urls import * 
from django.contrib import admin 
from django.contrib.auth.views import login 
from preguntasyrespuestas.views import index 

urlpatterns = [ 
    url(r'^$', index, name='index'), 
] 

私のhtml:

<!DOCTYPE html> 
<html> 
<head> 
    <title> Preguntas </title> 
</head> 
<body> 
    <p>{{ string }}</p> 
</body> 
</html> 

Basicaly私は私のテンプレートでstringに何があるか見せたい

は、ここに私のviews.py

from django.shortcuts import render 
from django.http import HttpResponse 
from preguntasyrespuestas.models import Pregunta 
from django.shortcuts import render_to_response 

# Create your views here. 
def index(request): 
    string = 'hi world' 
    return render_to_response('test/index.html', 
           {'string': string}) 

ここにある私のURLです。しかし、作業..

私のエラーではありません:

Using the URLconf defined in django_examples.urls, Django tried these URL patterns, in this order: 

    ^$ [name='index'] 

The current URL, test/index.html, didn't match any of these. 

私が間違って何をしているのですか?ありがとう..

答えて

1

URLの末尾にhttp://127.0.0.1:8000/などのURLを追加しないでください。templates/test/index.htmlが存在することを確認してください。

0

DjangoのURLルーティングでは、正規表現を使用して経路を照合します。この場合

url(r'^$', index, name='index'), 

あなたは空の文字列r'^$'で1つだけ有効なルートを持っています。たとえば、http://localhost:8000のような訪問をするだけで回答を得ることができます。その他のURLはすべて失敗します。

DjangoのURLルーティングは、ファイルシステム上のテンプレートファイルの場所から完全に独立しています。したがって、http://localhost/test/index.htmlは、その名前のテンプレートファイルがあっても有効ではありません。

任意のURLパスに一致するこのパターンを使用してキャッチオールルートを作成できます。

url(r'', index, name='index'), 
関連する問題