indexOutOfBounds
indexOutOfBounds

Reputation: 571

NoReverseMatch Error Django

I have the following model:

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})

In my main urls.py I have the following url:

url(r'^categories/', include('places.urls', namespace='categories')),

and in my app urls.py I have the following url:

url(r'^(?P<slug>[\w-]+)/review/$', user_place_review, name='user_review'),

In a template I am trying to do something like this which gives me a no reverse match error:

 <a class="btn btn-primary btn-lg" href="{% url 'categories:user_review' %}">Write A Review!</a>

The view linked to the url is :

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)

I am unable to find the cause of the error.

Upvotes: 2

Views: 53

Answers (2)

Aniket Pawar
Aniket Pawar

Reputation: 2721

Change this line

 <a class="btn btn-primary btn-lg" href="{% url 'categories:user_review' %}">Write A Review!</a>

to

 <a class="btn btn-primary btn-lg" href="{% url 'categories:user_review' slug=placereview_instance.slug %}">Write A Review!</a>

Where placereview_instance is your model object.You defined URL of user_review with slug keyword argument in the urls.py file. So Django is looking for keyword argument slug in that url in the template.It can not find the same, so it is throwing No reverse match found.

Also as you already defined get_absolute_url in models.py.In the same way you can directly use another method for review and then use in the templates ,

 <a class="btn btn-primary btn-lg" href="{{ placereview_instance.get_review_absolute_url }}">Write A Review!</a>

Upvotes: 2

Mauricio Cortazar
Mauricio Cortazar

Reputation: 4213

You must give a value to the args of the url. your template should look like this

<a class="btn btn-primary btn-lg" href="{% url 'categories:user_review' slug='your_parameter' %}">Write A Review!</a>

.

Upvotes: 0

Related Questions