Practical Django Projects - Pages 71 and 80
I am reading the book "Practical Django Projects". It is a nice book. I have a few questions though :
On page开发者_开发技巧 71, there is the following code :
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from coltrane.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
...
However no variable 'pub_date' has yet been defined in that file !
On page 80, I am being told that I should place two variables DELICIOUS_USER and DELICIOUS_PASSWORD in the Django settings file. I should then call that file with
from django.conf import settings
Where is that Django settings file ? In C:\Python27\Lib\site-packages\django\conf ?
pub_date
refers tocoltrane.models.Entry
attributepub_date
see the sourcefrom django.conf import settings
imports your projectsettings.py
so you have to define your settings inside yourproject/settings.py
file. Here are some docs on the official docs about using settings in python code
pub_date
is referencing a field defined in the Entry
model. Django will look up the field by name later, which is why it's in quotes (otherwise it would trigger an NameError
).
In models.py, you should have something like:
class Entry(models.Model):
...
pub_date = models.DateField(...)
The settings file is typically called settings.py
, and is located in your project's root folder (next to manage.py
, etc.).
精彩评论