django中的Slug字段错误

时间:2011-04-29 07:05:41

标签: python django django-models django-templates django-views

我正在尝试将以下Blog应用程序集成到我的网站,我得到了slug字段的以下错误,如何解决此问题

     TypeError at /login/

     __init__() got an unexpected keyword argument 'prepopulate_from'

     Request Method:   GET
     Request URL:   http://192.168.254.35/accounts/login/new_wind/
     Django Version:   1.2.1 SVN-1957
     Exception Type:   TypeError
     Exception Value:

     __init__() got an unexpected keyword argument 'prepopulate_from'

这是我的models.py

from django.db import models
from datetime import datetime

class BlogPost(models.Model):
  title = models.CharField(max_length=128)
  slug = models.SlugField(prepopulate_from=('title',))
  body = models.TextField()
  published = models.BooleanField(default=True)
  date_posted = models.DateTimeField(default=datetime.now)

def __unicode__(self):
    return self.title

class Admin:
    pass

2 个答案:

答案 0 :(得分:4)

错误说明了一切:SlugField不接受关键字参数prepopulate_fromThe docs支持这一点 - 除标准参数外,它只需要参数max_length。这应该改为in the admin class

修改:所以你的模型文件是:

from django.db import models
from datetime import datetime

class BlogPost(models.Model):
  title = models.CharField(max_length=128)
  slug = models.SlugField()
  body = models.TextField()
  published = models.BooleanField(default=True)
  date_posted = models.DateTimeField(default=datetime.now)

def __unicode__(self):
    return self.title

和同一应用程序的admin.py文件将是:

from django.contrib import admin
from myapp.models import BlogPost

class BlogPostAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}

admin.site.register(BlogPost, BlogPostAdmin)

如果您不了解管理网站的工作原理,请参阅Part 2 of the official Django tutorial

答案 1 :(得分:1)

似乎预先填充的方式已在django中更新,请参阅此问题:Is there an easy way to populate SlugField from CharField?