オブジェクトのリストから詳細ビューを作成するためのスラッグの適切な使用について多くのことを読んだ後。しかし、私はまだそれを機能させるのに問題があります。テンプレートに次のようなオブジェクトのリストを表示しています。
{% for thing in thing_list %}
<div class='thing-detail'><a href='{% url detail %}'><img src='theimage.png' />
{% endfor %}
しかし、でNoReverseMatch
エラーが発生していますdetail
。
正規表現に何か問題があるか、これを行うためのより良い方法が欠けていると思います。
正規表現:
url(r'^thing/(?P<slug>[\w-]+)/$', 'views.detail', name='detail'),
意見:
def detail(request, slug):
thing = get_object_or_404(Thing, slug=slug)
return render(request, 'detail.html', {'thing': thing})
モデル:
class Thing(models.Model):
user = models.ForeignKey(User)
created_on = models.DateTimeField(auto_now_add=True)
slug = models.SlugField()
def save(self, **kwargs):
slug = '%s' % (self.user)
unique_slugify(self, slug) ## from http://djangosnippets.org/snippets/1321/
super(Thing, self).save()
助けてくれてありがとう!