此文记录我在学习Django时遇到的一些问题以及解决方案。
1.需要一个方法把它传递到视图函数里去,这样 我们只用一个视图函数就可以处理所有的时间段了。我们使用圆括号把参数在URL模式里标识 出来
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/\d+/$', hours_ahead),
)
hours_ahead() takes exactly 2 arguments (1 given)
缺少参数,应该用(\d+),括号表示参数:
urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/(\d+)/$', hours_ahead),
)
from django.template import Context, Template
t = Template('My name is {{ name }}.')
Traceback (most recent call last):
File ““, line 1, in
File “/Library/Python/2.7/site-packages/django/template/base.py”, line 123, in init
if settings.TEMPLATE_DEBUG and origin is None:
File “/Library/Python/2.7/site-packages/django/conf/init.py”, line 54, in getattr
self._setup(name)
File “/Library/Python/2.7/site-packages/django/conf/init.py”, line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
设置没有配置,需要配置环境变量。
可以用以下方法来解决:
在import之前加入一下代码导入settings:
from django.conf import settings
settings.configure()在启动解释器之前,它告诉Django使用 哪个设置文件:
python manage.py shell
在你的配置文 件.bash_profile中手动添加 DJANGO_SETTINGS_MODULE这个环境变量