V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
garygay
V2EX  ›  Python

请教一个 tango with django 1.9 版本的 templates 路径问题,感激不尽

  •  
  •   garygay · 2017-06-22 11:49:29 +08:00 · 2036 次点击
    这是一个创建于 2500 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照最新版,一步一步做,但是最后发现模板不存在。书里面建立的 templates 位置和官方不太一样,需要配置,但是我打印路径是对的,很奇怪。rango 是 APP 的名字,tango_with_django_project 是项目名字。

    ├─rango
    │ │ admin.py
    │ │ apps.py
    │ │ models.py
    │ │ tests.py
    │ │ urls.py
    │ │ views.py
    │ │ __init__.py
    │ │
    │ ├─migrations
    │ │ │ __init__.py



    └─tango_with_django_project
    │ settings.py
    │ urls.py
    │ wsgi.py
    │ __init__.py

    ├─templates
    │ └─rango
    │ index.html

    settings 根据文档是这么配置的
    TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_PATH],
    'APP_DIRS': True,
    'OPTIONS': {
    'context_processors': [
    'django.template.context_processors.debug',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    ],
    },
    },
    ]

    打印 TEMPLATE_PATH 路径如下:
    C:\Users\GARRY\myenv\tango_with_django_project\templates
    和创建目录路径是符合的

    views.py 里是这么写的:
    def index(request):
    context_dict = {'boldmessage': 'Tango with Django,hahaha'}
    return render(request, 'rango/index.html', context=context_dict)

    最后报错,说找不到
    TemplateDoesNotExist at /
    rango/index.html
    Request Method: GET
    Request URL: http://localhost:8000/
    Django Version: 1.11.2
    Exception Type: TemplateDoesNotExist
    Exception Value: rango/index.html

    看下了国外类似问题,与我写的一样,所以最后求救各位。谢谢
    https://stackoverflow.com/questions/29987619/templatedoesnotexist-at-rango
    4 条回复    2017-06-22 12:28:32 +08:00
    garygay
        1
    garygay  
    OP
       2017-06-22 11:52:43 +08:00
    树图看不清楚,templates 是建立在项目 tango_with_django_project 下的,然后下面是 rango-index.html
    garygay
        2
    garygay  
    OP
       2017-06-22 12:19:55 +08:00
    问题找到了 ,是配置路径的问题 。谢谢大家。
    garygay
        3
    garygay  
    OP
       2017-06-22 12:27:51 +08:00
    最后发现可能是版本问题,还是按照官方最新的 1.11 的,把 templates 建立在 APP 目录下。
    zsdostar
        4
    zsdostar  
       2017-06-22 12:28:32 +08:00
    可以试试用继承 View 类的类来写 view, 这种
    from django.views.generic.base import View

    class index(View):
    def get(self, request):
    context_dict = {'boldmessage': 'Tango with Django,hahaha'}
    return render(request, 'rango/index.html', context=context_dict)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2982 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:14 · PVG 21:14 · LAX 06:14 · JFK 09:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.