本文介绍了Django管理员不允许保存unicode slugs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Category(models.Model):
name = models.CharField('name',max_length = 100)
slug = models.SlugField('slug',unique = True)
description = models.TextField('description')

class Meta:
verbose_name ='category'
verbose_name_plural ='categories'

@permalink
def get_absolute_url(self):
return('category_detail',None,{
'slug':self.slug
})

def __unicode __(self):
return u'%s '%self.name

但Django不保存页面并投诉:

 输入由字母,数字,下划线或连字符组成的有效slug

我也尝试使用这种保存方式

  def save(self ,* args,** kwargs): 
self.name = slugify_unicode(self.name)
super(Category,self).save(* args,** kwargs)

但它没有解决问题。所以我得到了股票,并感谢你的帮助来解决这个问题。

解决方案

你不能。 。



如果您必须在类似字符的字段中使用非ASCII字符您可以使用正常的 CharField 并添加一个 db_index = True


I'm trying to save a Persian slug for this model:

class Category(models.Model):
    name = models.CharField('name', max_length=100)
    slug = models.SlugField('slug', unique=True)
    description = models.TextField('description')

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    @permalink
    def get_absolute_url(self):
        return ('category_detail', None, {
            'slug': self.slug
        })

    def __unicode__(self):
        return u'%s' % self.name

But Django does not save the page and complaint that:

Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens

I also tried to use this save method instead

    def save(self, *args, **kwargs):
      self.name = slugify_unicode(self.name)
      super(Category, self).save(*args, **kwargs)

but it did not solve the problem. So I got stock on this and appreciate your help to resolve this.

解决方案

You can't. Slug fields can only contain ASCII letters, numbers, dashses and underlines.

If you must use non-ASCII chars in a slug-like field, you can use a normal CharField and add a db_index = True to it.

这篇关于Django管理员不允许保存unicode slugs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:16