新手Django网址,视图和模板 - 这个难以置信的简单django项目怎么可能会失败? [英] Newbie Django urls, views, and templates - how can this incredibly simple django project possibly be failing?

查看:104
本文介绍了新手Django网址,视图和模板 - 这个难以置信的简单django项目怎么可能会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登陆 http://127.0.0.1:8000/ 时,我想显示一个html页面说欢迎。当用户进入 http://127.0.0.1:8000/time/ 时,我想显示当前时间。我已经按照指示t和点我的每一个。我的设置如下。为什么我继续得到一个TemplateDoesNotExist错误?

When the user lands at http://127.0.0.1:8000/ I would like to display an html page that says "welcome." When the user goes http://127.0.0.1:8000/time/ I would like to display the current time. I have followed instructions to the t and dotted every i. My settings are below. Why do I continue to get a TemplateDoesNotExist error?

views.py

from django.template.loader import get_template
from django.shortcuts import render
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    current_datetime_template = get_template('current_datetime.html')
    context_dict = {'current_date': now}
    return render(request, current_datetime_template, context_dict)

def welcome(request):
    welcome_template = get_template('welcome.html')
    context_dict = {'username' : 'Sally Jenkins'}
    return render(request, welcome_template, context_dict)







urls.py

from django.conf.urls.defaults import patterns, include, url
from simpletest.views import welcome, current_datetime

urlpatterns = patterns('',
    url(r'^time/$', current_datetime),
    url(r'^$', welcome),
)



< hr>


settings.py

... # all defaults ommitted here - I changed nothing.
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

在我的django项目目录中,我有一个名为 templates ,它包含 base.html current_datetime.html welcome.html

In my django project directory I have a directory called templates and it contains base.html, current_datetime.html, and welcome.html just as expected.

请告诉我我忽略了什么。

Please tell me what I have overlooked.

谢谢

更多信息:

我正在使用virtualenv。事实上,我在 / Users / quanda / dev / django-projects / 中有两个django项目有什么区别吗?我不能想像会的。一个被称为开花,是我正在开展的主要项目。另一个被称为简单测试,我使其非常简单,以便我可以隔离我在开花项目中遇到的问题。我正在为这两个项目使用相同的虚拟环境。从django-projects /运行 tree -L 2 提供以下结构:

I am using virtualenv. Does the fact that I have two django projects in the /Users/quanda/dev/django-projects/ make any difference? I can't imagine it would. One is called "blossom" and is the main project I am working on. The other is called "simpletest" and I made it extremely simple so that I could isolate the issue I was having in my blossom project. I am using the same virtual environment for both projects. Running tree -L 2 from django-projects/ gives the following structure:

.
├── Procfile
├── blossom
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── fixtures
│   ├── manage.py
│   ├── onora
│   ├── settings.py
│   ├── settings.pyc
│   ├── sqlite3-database
│   ├── templates
│   ├── test_stuff.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── requirements.txt
├── simpletest
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── manage.py
│   ├── settings.py
│   ├── settings.pyc
│   ├── templates
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
└── virtual_environment
    ├── bin
    ├── django-registration-0.8-alpha-1.tar
    ├── include
    └── lib


推荐答案

您正在传递模板对象而不是模板名称,如traceback中所示:

You're passing a template object instead of the template name, as shown here in the traceback:

/Users/quanda/dev/django-projects/simpletest/templates/<django.template.base.Template object at 0x102963910> (File does not exist)
...
File "/Users/quanda/dev/django-projects/simpletest/../simpletest/views.py" in current_datetime
  9.     return render(request, current_datetime_template, context_dict)

不要传递变量 current_datetime_template - 只需将'current_datetime.html'作为字符串传递,如下所示:

Don't pass the variable current_datetime_template - just pass 'current_datetime.html' as a string, like so:

def current_datetime(request):
    now = datetime.datetime.now()
    context_dict = {'current_date': now}
    return render(request, 'current_datetime.html', context_dict)

这篇关于新手Django网址,视图和模板 - 这个难以置信的简单django项目怎么可能会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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