• <legend id='Rft5E'><style id='Rft5E'><dir id='Rft5E'><q id='Rft5E'></q></dir></style></legend>
  • <tfoot id='Rft5E'></tfoot>
      <i id='Rft5E'><tr id='Rft5E'><dt id='Rft5E'><q id='Rft5E'><span id='Rft5E'><b id='Rft5E'><form id='Rft5E'><ins id='Rft5E'></ins><ul id='Rft5E'></ul><sub id='Rft5E'></sub></form><legend id='Rft5E'></legend><bdo id='Rft5E'><pre id='Rft5E'><center id='Rft5E'></center></pre></bdo></b><th id='Rft5E'></th></span></q></dt></tr></i><div id='Rft5E'><tfoot id='Rft5E'></tfoot><dl id='Rft5E'><fieldset id='Rft5E'></fieldset></dl></div>
          <bdo id='Rft5E'></bdo><ul id='Rft5E'></ul>

      1. <small id='Rft5E'></small><noframes id='Rft5E'>

        如何在 Django 中创建 slug?

        时间:2024-04-21
      2. <i id='6uy6a'><tr id='6uy6a'><dt id='6uy6a'><q id='6uy6a'><span id='6uy6a'><b id='6uy6a'><form id='6uy6a'><ins id='6uy6a'></ins><ul id='6uy6a'></ul><sub id='6uy6a'></sub></form><legend id='6uy6a'></legend><bdo id='6uy6a'><pre id='6uy6a'><center id='6uy6a'></center></pre></bdo></b><th id='6uy6a'></th></span></q></dt></tr></i><div id='6uy6a'><tfoot id='6uy6a'></tfoot><dl id='6uy6a'><fieldset id='6uy6a'></fieldset></dl></div>
        <tfoot id='6uy6a'></tfoot>

          <legend id='6uy6a'><style id='6uy6a'><dir id='6uy6a'><q id='6uy6a'></q></dir></style></legend>

              <tbody id='6uy6a'></tbody>

          1. <small id='6uy6a'></small><noframes id='6uy6a'>

              • <bdo id='6uy6a'></bdo><ul id='6uy6a'></ul>
                  本文介绍了如何在 Django 中创建 slug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am trying to create a SlugField in Django.

                  I created this simple model:

                  from django.db import models
                  
                  class Test(models.Model):
                      q = models.CharField(max_length=30)
                      s = models.SlugField()
                  

                  I then do this:

                  >>> from mysite.books.models import Test
                  >>> t=Test(q="aa a a a", s="b b b b")
                  >>> t.s
                  'b b b b'
                  >>> t.save()
                  >>> t.s
                  'b b b b'
                  

                  I was expecting b-b-b-b.

                  解决方案

                  You will need to use the slugify function.

                  >>> from django.template.defaultfilters import slugify
                  >>> slugify("b b b b")
                  u'b-b-b-b'
                  >>>
                  

                  You can call slugify automatically by overriding the save method:

                  class Test(models.Model):
                      q = models.CharField(max_length=30)
                      s = models.SlugField()
                      
                      def save(self, *args, **kwargs):
                          self.s = slugify(self.q)
                          super(Test, self).save(*args, **kwargs)
                  

                  Be aware that the above will cause your URL to change when the q field is edited, which can cause broken links. It may be preferable to generate the slug only once when you create a new object:

                  class Test(models.Model):
                      q = models.CharField(max_length=30)
                      s = models.SlugField()
                      
                      def save(self, *args, **kwargs):
                          if not self.id:
                              # Newly created object, so set slug
                              self.s = slugify(self.q)
                  
                          super(Test, self).save(*args, **kwargs)
                  

                  这篇关于如何在 Django 中创建 slug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 django 模板中的字典中遍历字典? 下一篇:你如何在 django 网站上记录服务器错误

                  相关文章

                  1. <small id='Cgfyz'></small><noframes id='Cgfyz'>

                  2. <tfoot id='Cgfyz'></tfoot>
                    <legend id='Cgfyz'><style id='Cgfyz'><dir id='Cgfyz'><q id='Cgfyz'></q></dir></style></legend>
                        <bdo id='Cgfyz'></bdo><ul id='Cgfyz'></ul>

                    1. <i id='Cgfyz'><tr id='Cgfyz'><dt id='Cgfyz'><q id='Cgfyz'><span id='Cgfyz'><b id='Cgfyz'><form id='Cgfyz'><ins id='Cgfyz'></ins><ul id='Cgfyz'></ul><sub id='Cgfyz'></sub></form><legend id='Cgfyz'></legend><bdo id='Cgfyz'><pre id='Cgfyz'><center id='Cgfyz'></center></pre></bdo></b><th id='Cgfyz'></th></span></q></dt></tr></i><div id='Cgfyz'><tfoot id='Cgfyz'></tfoot><dl id='Cgfyz'><fieldset id='Cgfyz'></fieldset></dl></div>