16

I want to have SEO-friendly URL,my current url in urls.py :

(ur'^company/news/(?P<news_title>.*)/(?P<news_id>\d+)/$','CompanyHub.views.getNews')

I use it in template:

{% for n in news %}
     <a href="{% url CompanyHub.views.getNews n.title,n.pk %}" >{{n.description}}</a>
{% endfor %}

I use news_id to get news object with that PK . I want to convert this url:

../company/news/tile of news,with comma/11

to:

../company/news/tile-of-news-with-comma/11

by doing some thing like this in template:

{% for n in news %}
      <a href="{% url CompanyHub.views.getNews slugify(n.title),n.pk %}" >{{n.description}}</a>
{% endfor %}

I checked out these questions: question1 question2 question3 and this article but they save an slugify field in database while I wanna generate it on demand.in addition I want to run a query by news_id.

I think this question is good,but I don't know how to use news_id to fetch my news object

Community
  • 1
  • 1
Asma Gheisari
  • 5,794
  • 9
  • 30
  • 51
  • 1
    You may instead want to implement a `permalink()` method on your `news` objects. You can call slugify from there without having to worry about template syntax. – Amber Jul 12 '12 at 17:04
  • can U give me some direction about how to use it in template? – Asma Gheisari Jul 12 '12 at 18:45

2 Answers2

16

This will generate the needed url:

{% for n in news %}
      <a href="{% url CompanyHub.views.getNews n.title|slugify n.pk %}" >{{n.description}}</a>
{% endfor %}

The examples above save slugify_field in database, as they later search for it. Otherwise in database you'll have a normal title, and slugified title in code for searching.. No easy way to compare them. But the way you've explained is simpler. You will have this kind of view:

def news(request, slug, news_id):
    news = News.objects.filter(pk=news_id)

UPDATE: To use unicode symbols in slugify, you'll need a conversion first. Look at this: How to make Django slugify work properly with Unicode strings?. It uses the Unidecode library

Then add a custom filter:

from unidecode import unidecode
from django.template.defaultfilters import slugify

def slug(value):
    return slugify(unidecode(value))

register.filter('slug', slug)

then in your template use this:

{% load mytags %}
<a href="{% url CompanyHub.views.getNews n.title|slug n.pk %}

Here is an example:

{{ "影師嗎 1 2 3"|slug}}

renders as:

ying-shi-ma-1-2-3
Community
  • 1
  • 1
Tisho
  • 8,320
  • 6
  • 44
  • 52
  • as I said in before answer,title contains unicode string,so slugify doesn't work on it! – Asma Gheisari Jul 12 '12 at 18:44
  • tnx,I tryed it out.but I don't want to change my unicode characters,like your example that make those chinese characters to it's equal in english,I want when people enter those chinese words they can find it in search engine. – Asma Gheisari Jul 14 '12 at 08:32
  • Then you might need to write your own slug function, or to use urlencode() instead of unidecode() ... But you might have problems with non-ascii symbols in URL. – Tisho Jul 14 '12 at 08:42
9

Have you tried n.title|slugify and see if that works for you.

ref: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slugify

Note: although this is possible, just make sure the 'slugified' element is never used for any part of routing... (ie, purely for display only)

Jon Clements
  • 138,671
  • 33
  • 247
  • 280