Newbie Django urls, views, and templates - how can this incredibly simple django project possibly be failing?

我是研究僧i 提交于 2019-12-23 05:32:09

问题


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

settings.py

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

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.

Thanks.

MORE INFO:

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

回答1:


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)

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)



回答2:


Try something like this in settings.py:

CURRENT_PATH = os.path.abspath(os.path.dirname(__file__) # for linux
# or
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).replace('\\', '/') # for windows

TEMPLATE_DIRS = (os.path.join(CURRENT_PATH, 'templates'),) # for template dirs



回答3:


Suppose foobar is your django project. Then welcome.html should be resides in /foobar/templates/welcome.html and In settings:

TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__),"templates"), 
)  #for linux and windows


来源:https://stackoverflow.com/questions/8885107/newbie-django-urls-views-and-templates-how-can-this-incredibly-simple-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!