最新の投稿タイトルのリストを含むブログ アプリがあります。これで、リスト アイテムがそのコンテンツにリンクされます。私の問題(同様の投稿)は、タイトルにスペースが含まれている場合、使用するとスペースのあるURLを取得することです:
<a href="{{ i.id }}/{{ i.title }}">{{ i.title }}
私のテンプレートで。追加のURLFieldを使用できますが、URL に適したタイトルを手動で作成したくありません。これを行う一般的な方法は何ですか?
私のモデル.py
class Post(models.Model):
title = models.CharField(max_length=100)
...
def __unicode__(self):
return self.title
私のview.py
def recentlyBlogged(request):
lastPosts = Post.objects.filter(publication__gt = datetime.now() - timedelta(days=30))
return render(request, "blog/blog.html", {'Posts': lastPosts})
私のテンプレート
{% for i in Posts %}
<ul id="latestPostsList">
<li class="latestPostsListItem"><a href="{{ i.id }}/{{ i.title }}">{{ i }}</a></li>
{% endfor %}
</ul>