2010-07-08 5 views
16

내 앱의 기본 페이지 또는 색인 페이지를 설정하고 싶습니다. 내가 settings.py에서 MAIN_PAGE를 추가 한 후 main_page 객체를 반환하는 main_page보기를 생성하지만,이 또한 작동하지 않습니다 시도, 내가Django 기본 페이지 설정 방법

(r'^$', index), 

indexshould 같은 urls.py에 선언을 추가하려고 루트에있는 index.html 파일의 이름이되어야합니다 (그러나 분명히 작동하지 않습니다).

장고 웹 사이트에서 기본 페이지를 설정하는 가장 좋은 방법은 무엇입니까?

감사합니다.

답변

12

, 당신은 django.views.generic.simple에서 direct_to_template보기 기능을 사용할 수 있습니다. 당신의 URL의 conf에서 :

from django.views.generic.simple import direct_to_template 
urlpatterns += patterns("", 
    (r"^$", direct_to_template, {"template": "index.html"}) 
) 

(index.html는 템플릿 디렉토리 중 하나의 루트에 가정.)

+1

이 솔루션은 더 이상 사용되지 않습니다. http://stackoverflow.com/questions/11428427/no-module-named-simple-error-in-django –

1

당신은 일반 direct_to_template보기 기능을 사용할 수 있습니다 정적 페이지를 참조하십시오 (이 동적 처리를 통해 갈 필요가 없습니다)

# in your urls.py ... 
... 
url(r'^faq/$', 
    'django.views.generic.simple.direct_to_template', 
    { 'template': 'faq.html' }, name='faq'), 
... 
+0

정말 고마워요! – dana

10

TemplateView 클래스를 사용하는 것이 일의 새로운 선호하는 방법입니다. direct_to_template에서 이동하려면 SO answer을 참조하십시오. 주 urls.py 파일에서

:

from django.conf.urls import url 
from django.contrib import admin 
from django.views.generic.base import TemplateView 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    # the regex ^$ matches empty 
    url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), 
     name='home'), 
] 

주, 나는 templates/ 디렉토리에 자체 디렉토리 static_pages/index.html LINKE 정적 페이지를 넣어 선택합니다.

+0

에서 선택한 답변을 참조하십시오.이 솔루션은 완벽하게 작동합니다! – Deadpool