Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

python - TemplateDoesNotExist at / at templates/index.html

Non the the solutions already posted seem to be working for me.

This is my project directory.

.
|-- auto
|   |-- admin.py
|   |-- apps.py
|   |-- __init__.py
|   |-- migrations
|   |-- models.py
|   |-- views.py
|-- db.sqlite3
|-- manage.py
|-- mysite
|   |-- __init__.py
|   |-- settings.py
|   |-- test.py
|   |-- urls.py
|   |-- wsgi.py
|-- static
|   |-- index.html
`-- templates
    `-- index.html

This is my settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
   os.path.join(os.path.dirname(__file__), '../static/'),
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, '../templates'),
)

I've also tried,

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

This is my views.py

def render_landing_page(request, template="templates/index.html"):
    return render(request, template, context_instance=RequestContext(request))

And this is my urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.render_landing_page),

]

At 127.0.0.1:8000/ I get a "TemplateDoesNotExist at /"

Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.9.4
Exception Type: TemplateDoesNotExist
Exception Value:    
templates/index.html

But, when I go to 127.0.0.1:8000/static/index.html. The page is rendered.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not need the templates/ in your template path, since your telling Django to look inside the dir already through your settings. Also, context_instance=RequestContext(request) is deprecated, you just need to return a dict containing your context (an empty dict in your case?):

def render_landing_page(request, template="index.html"):
    return render(request, template, {})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...