2016-12-02 13 views
0

장고에서 시작합니다.장고 - 템플릿에 내 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/). templates/test/index.html이 있는지 확인하십시오.

0

Django의 URL 라우팅은 정규식을 사용하여 경로를 일치시킵니다. 이 경우

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

당신은 빈 문자열 r'^$' 단지 하나의 유효한 경로를 보유하고 있습니다. 예를 들어 http://localhost:8000과 같은 방문을 통해서만 답변을 얻을 수 있습니다. 다른 모든 URL은 실패합니다.

Django의 URL 라우팅은 파일 시스템의 템플릿 파일 위치와는 완전히 독립적입니다. 따라서 http://localhost/test/index.html은 해당 이름의 템플릿 파일이 있더라도 유효하지 않습니다.

URL 경로와 일치하는 패턴을 사용하여 포괄 경로를 만들 수 있습니다.

url(r'', index, name='index'), 
관련 문제