我想要SEO-friendly URL,我现在的网址urls.py:
(ur'^company/news/(?P<news_title>.*)/(?P<news_id>\d+)/$','CompanyHub.views.getNews')
Run Code Online (Sandbox Code Playgroud)
我在模板中使用它:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews n.title,n.pk %}" >{{n.description}}</a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我用它news_id来对get新闻对象PK.我想转换这个网址:
../company/news/tile of news,with comma/11
Run Code Online (Sandbox Code Playgroud)
至:
../company/news/tile-of-news-with-comma/11
Run Code Online (Sandbox Code Playgroud)
通过在模板中做这样的事情:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews slugify(n.title),n.pk %}" >{{n.description}}</a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我检查了这些问题:
question1
question2
question3和这篇文章,但他们保存slugify field在数据库中,而我想要按需生成它.另外我想运行查询news_id.
我认为这个问题很好,但我不知道怎么news_id用来取我的news object