posts - 431,  comments - 344,  trackbacks - 0

          It's a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. I'd advise to generate the slug, using a function, given a title (or other piece of data), rather than setting it manually.

          An example:

          <title> The 46 Year Old Virgin </title>
          <content> LOL!!! What a loser heh </content>
          <slug> the-46-year-old-virgin </slug>

          Now let's pretend that we have a Django model such as:

          class Article():
              title
          = models.CharField(max_length=100)
              content
          = models.TextField(max_length=1000)
              slug
          = models.SlugField(max_length=40)

          How would you reference this object with a URL, with a meaningful name? You could use Article.id so the URL would look like this:

          www.example.com/Article/23

          Or, you could reference the title like so:

          www.example.com/article/The 46 Year Old Virgin

          Problem is, spaces aren't valid in URLs, they need to be replaced by %20 which is ugly, making it the following:

          www.example.com/article/The%2046%20Year%20Old%20Virgin

          That's not solving our meaningful URL. Wouldn't this be better:

          www.example.com/article/the-46-year-old-virgin

          That's a slug. 'the-46-year-old-virgin'. All letters are downcased and spaces are replaced by hyphens '-'. See the URL of this very webpage for an example!

          posted on 2009-04-28 14:07 周銳 閱讀(225) 評(píng)論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 晋州市| 广丰县| 揭阳市| 司法| 萨嘎县| 铅山县| 清水县| 上饶县| 福泉市| 西藏| 通化县| 高唐县| 金川县| 称多县| 延寿县| 武安市| 镶黄旗| 靖江市| 奉贤区| 阿城市| 乌鲁木齐市| 天全县| 郸城县| 靖江市| 屯门区| 巩义市| 邢台县| 宣恩县| 泽州县| 东莞市| 彩票| 冷水江市| 凌海市| 化州市| 承德市| 临安市| 登封市| 昭觉县| 德阳市| 大关县| 屏东县|