Hello,
I'm trying to learn Django from the book "Practical Django Projects 2nd Edition"
I'm using Django 1.3.1 and Python 2.7.2 on Windows 7
Also I'm using Eclipse Indigo with pydev
The current project I'm working on is to create a Weblog called coltrane.
coltrane is an application that exists in a project called "cmswithpythonanddjango"
Inside the models.py is a class called Entry which holds the fields used to Enter a weblog entry.
When I go to localhost:8000/admin/ I get the option to add and Entry.
I can select this option fill out sample data, but when I try to save it.
I get the following error.
AttributeError at /admin/coltrane/entry/add/
'Entry' object has no attribute 'pubdate'
There is a field called pub_date in Entry
It's as if I have made a typing error somewhere and typed pubdate instead of pub_date.
Below is the models.py, views.py , url.py , and admin.py files
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is my models.py
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from django.db import models
import datetime
from tagging.fields import TagField
from django.contrib.auth.models import User
from markdown import markdown
class Category(models.Model):
title = models.CharField(max_length=250, help_text="Maximum 250 characters.")
slug = models.SlugField(help_text="Suggested value automatically generated from title. Must be unique.")
description = models.TextField()
class Meta:
ordering = ['title']
verbose_name_plural = "Categories"
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/categories/%s/" % self.slug
class Entry(models.Model):
LIVE_STATUS = 1
DRAFT_STATUS = 2
HIDDEN_STATUS = 3
STATUS_CHOICES = (
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS, 'Draft'),
(HIDDEN_STATUS, 'Hidden')
)
# Core fields
title = models.CharField(max_length=250, help_text = "Maximum 250 characters. ")
excerpt = models.TextField(blank=True, help_text = "A short summary of the entry. Optional.")
body = models.TextField()
pub_date = models.DateTimeField(default = datetime.datetime.now)
# Fields to store generated HTML
excerpt_html = models.TextField(editable=False, blank=True)
body_html = models.TextField(editable=False, blank=True)
# Metadata
author = models.ForeignKey(User)
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False)
slug = models.SlugField(unique_for_date = 'pub_date', help_text = "Suggested value automatically generated from title. Must be unique.")
status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS, help_text = "Only entries with live status will be publicly displayed.")
# Categorization.
categories = models.ManyToManyField(Category)
tags = TagField(help_text = "Separate tags with spaces.")
class Meta:
verbose_name_plural = "Entries"
ordering = ['-pub_date']
def __unicode__(self):
return self.title
def save(self, force_insert=False, force_update=False):
self.body_html = markdown(self.body)
if self.excerpt:
self.excerpt_html = markdown(self.excerpt)
super(Entry, self).save(force_insert, force_update)
def get_absolute_url(self):
return "/weblog/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the views.py
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from django.shortcuts import render_to_response
from cmswithpythonanddjango.coltrane.models import Entry
def entries_index(request):
return render_to_response('coltrane/entry_index.html', { 'entry_list': Entry.objects.all() })
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the url.py
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from django.conf.urls.defaults import patterns, include, url
from cmswithpythonanddjango.coltrane.models import Entry
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
entry_info_dict = {
'queryset' : Entry.objects.all(),
'data_field' : 'pub_date'
}
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^tiny_mce/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': 'C:/users/lau/documents/tinymce/jscripts/tiny_mce/' }),
(r'^search/$', 'cmswithpythonanddjango.search.views.search'),
(r'^weblog/$', 'cmswithpythonanddjango.coltrane.views.entries_index'),
(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 'django.views.generic.date_based.object_detail', entry_info_dict),
(r'', include('django.contrib.flatpages.urls')),
# (r'^(?P<url>.*)$', 'flatpage'),
)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the admin.py
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from django.contrib import admin
from cmswithpythonanddjango.coltrane.models import Entry, Category
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = { 'slug': ['title'] }
class EntryAdmin(admin.ModelAdmin):
prepopulated_fields = { 'slug': ['title'] }
admin.site.register(Entry, EntryAdmin)
admin.site.register(Category, CategoryAdmin)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I can't think of anywhere else the problem could be other than in one of the above files.
If somebody knows what the problem is I would be very grateful if they could tell me what it is.
Failing that, I would appreciate any suggestion of where else I could look.