我有以下型號:NoReverseMatch錯誤的Django
class UserPlaceReview(models.Model):
place = models.ForeignKey(PlaceReview)
review = models.TextField()
def __str__(self):
return self.review
class PlaceReview(models.Model):
name = models.CharField(max_length=150)
location = models.CharField(max_length=250)
image = models.ImageField(upload_to = image_upload_location,null=True, blank=True)
category = models.ForeignKey(Categories)
slug = models.SlugField()
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("categories:detail", kwargs={"slug": self.slug})
在我的主要urls.py,我有以下網址:
url(r'^categories/', include('places.urls', namespace='categories')),
,並在我的應用程序urls.py,我有以下網址:
url(r'^(?P<slug>[\w-]+)/review/$', user_place_review, name='user_review'),
在模板中我試圖做這樣的事情,給了我一個沒有反向匹配錯誤:
<a class="btn btn-primary btn-lg" href="{% url 'categories:user_review' %}">Write A Review!</a>
鏈接到網址的觀點是:
def user_place_review(request,slug):
place = PlaceReview.objects.get(slug=slug)
form = UserPlaceReviewForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
review = form.cleaned_data['review']
instance.place = place
instance.review = review
instance.save()
return redirect('/')
context = {
"form": form,
}
return render(request,"places/user_review.html", context)
我無法找到錯誤的原因。
,因爲我有(塞的instance.get_absolute_url不起作用)/審查添加在我的網址。它會讓我在同一頁面,並不會帶我/查看 – indexOutOfBounds
非常感謝。我們通過從我們的實例slu works的其他選項工作 – indexOutOfBounds
好吧,創建一個更多的方法絕對網址的審查,它也會工作 – Aniket