• <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?

        How do I create a slug in Django?(如何在 Django 中创建 slug?)
      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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  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>

                              <tbody id='Cgfyz'></tbody>
                            <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>