Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use slugify in template

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

like image 746
Asma Gheisari Avatar asked Jul 12 '12 15:07

Asma Gheisari


People also ask

Which characters are illegal in template variable names in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.

How do I pass Slugs in Django?

Using The Captured Slug Now in the post_detail() function in the views.py file, we can accept the captured slug from the URL using post_detail(request, slug). From there, we then use the Django ORM to query the database using that slug with the Post. objects. get(slug=slug) code.

How do you use slugs in Python?

Start up the local server, python manage.py runserver , and go to the Article page in the Admin. The Slug field is empty. Manually add in our desired value a-day-in-the-life and click "Save." Ok, last step is to update articles/urls.py so that we display the slug keyword argument in the URL itself.

Which Django template syntax allows to use the for loop?

To create and use for loop in Django, we generally use the “for” template tag. This tag helps to loop over the items in the given array, and the item is made available in the context variable.


1 Answers

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
like image 128
Tisho Avatar answered Oct 05 '22 22:10

Tisho