TemplateDoesNotExist at / at templates / index.html [英] TemplateDoesNotExist at / at templates/index.html

查看:1255
本文介绍了TemplateDoesNotExist at / at templates / index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有已经发布的解决方案似乎在为我工作。



这是我的项目目录。

 
| - auto
| | - admin.py
| | - apps.py
| | - __init__.py
| | - 迁移
| | - models.py
| | - views.py
| - db.sqlite3
| - manage.py
| - mysite
| | - __init__.py
| | - settings.py
| | - test.py
| | - urls.py
| | - wsgi.py
| - static
| | - index.html
` - 模板
` - index.html

这是我的settings.py

  STATIC_URL ='/ static /'

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


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

我我也尝试过,

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

这是我的views.py


$ b $
return render(request,template,context_instance = RequestContext(request))$ b $($)
$ b

这是我的urls.py

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

]

在127.0.0.1:8000/我得到一个TemplateDoesNotExist at /

 请求方法:GET 
请求URL:http ://127.0.0.1:8000 /
Django版本:1.9.4
异常类型:TemplateDoesNotExist
异常值:
templates / index.html

但是,当我去127.0.0.1:8000/static/index.html。页面呈现。

解决方案

您不需要模板路径中的 templates / 因为你告诉Django已经通过你的设置来查看目录。此外, context_instance = RequestContext(request)已被弃用,您只需要返回一个包含您的上下文的dict(在您的情况下为空的dict):

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


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.

解决方案

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, {})

这篇关于TemplateDoesNotExist at / at templates / index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆