请教一个 tango with django 1.9 版本的 templates 路径问题,感激不尽 - V2EX
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 2693 次点击
    这是一个创建于 3100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照最新版,一步一步做,但是最后发现模板不存在。书里面建立的 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     Solana     1266 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 23:54 PVG 07:54 LAX 15:54 JFK 18:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86