Home > Enterprise >  Reverse for 'edit_review' with arguments '('',)' not found. 1 pattern(
Reverse for 'edit_review' with arguments '('',)' not found. 1 pattern(

Time:06-28

When i try to click on my edit comment button in product_detail i get this error and i cant figure out how to fix it. would really appreciate some help thanks in advance. error when clicking edit comment button below: error when clicking edit comment

Am only writing out the needed code not the full code if there is anything u need tell me and i add.

views.py

from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db.models.functions import Lower

from .models import Product, Category, Review
from .forms import ProductForm, ReviewForm


@login_required
def edit_review(request, review_id):
    review = get_object_or_404(Review, pk=review_id)
    product = Product.objects.get(name=review.product)

    if request.method == 'POST':
        review_form = ReviewForm(request.POST or None, instance=review)
        if review_form.is_valid():
            review_form.save()
            messages.success(request, 'Successfully updated Review!')
            return redirect(reverse('product_detail', args=[product.id]))
        else:
            messages.error(request, 'Failed to update product. Please ensure the form is valid.')
    else:
        review_form = ReviewForm(instance=review)
        messages.info(request, f'You are editing {edit_review}')

    template = 'products/edit_review.html'
    context = {
        'review_form': review_form,
        'Review': Review,
    }

    return render(request, template, context)

Models.py

class Review(models.Model):
    product = models.ForeignKey(Product, related_name='reviews', on_delete=models.CASCADE)
    rating = models.IntegerField(default=3)
    content = models.TextField()
    created_by = models.ForeignKey(User, related_name='reviews', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s - %s' % (self.product.name, self.created_by)

forms.py

from django import forms
from .widgets import CustomClearableFileInput
from .models import Product, Category, Review


class ReviewForm(forms.ModelForm):
    class Meta:
        model = Review
        fields = ('content', 'rating')
        widgets = {
            'content': forms.Textarea(attrs={'class': 'form-control'}),
            'rating': forms.Select(attrs={'class': 'form-control',}),
        }

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.all_products, name='products'),
    path('<int:product_id>/', views.product_detail, name='product_detail'),
    path('add/', views.add_product, name='add_product'),
    path('edit/<int:product_id>/', views.edit_product, name='edit_product'),
    path('delete/<int:product_id>/', views.delete_product, name='delete_product'),
    path('delete_review/<int:review_id>/delete_review', views.delete_review, name='delete-review'),
    path('edit_review/<review_id>', views.edit_review, name="edit_review"),
]

edit_review.html

{% extends "base.html" %}
{% load static %}


{% block content %}
    <div ></div>
    <div >
        <div >
            <div >
                <hr>
                <h2 >Reviews</h2>
                <h5 >Edit you're Review</h5>
                <hr>
            </div>
        </div>

        <div >
            <div >
                <form method="POST" action="{% url 'edit_review' review.id %}"  enctype="multipart/form-data">
                    {% csrf_token %}
                    {% for field in form %}
                        {% if field.name != 'image' %}
                            {{ field | as_crispy_field }}
                        {% else %}
                            {{ field }}
                        {% endif %}
                    {% endfor %}
                    <div >
                        <a  href="{% url 'reviews' %}">Cancel</a>
                        <button  type="submit">Update Review</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
{% endblock %}

product_detail.html

{% for review in reviews %}
            <div >
                <div >
                    <div >
                        <div >
                            <div >
                                <img src="https://image.ibb.co/jw55Ex/def_face.jpg"  alt="Profile picture"/>
                                <p >{{ review.created_at|date:"Y-m-d" }}</p>
                            </div>
                            <div >
                                <p>
                                    <a  style="color:blue"><strong>{{ review.created_by }}</a>
                                    <span ><i >{{ review.rating }}/5</i></span>
                               </p>
                               <div ></div>
                                <p>{{ review.content }}</p>
                                {% if user.is_authenticated %}
                                {% if user.id == review.created_by.id %}
                                <p>
                                    <a a href="{% url 'edit_review' review.id %}" > <i ></i> Edit</a>
                                    <a href="{% url 'delete-review' review.id %}" > <i ></i> Delete</a>
                               </p>
                            </div>
                        </div>
                            
                                </div>
                            </div>
            </div>
        </div>
            {% endif %}
        {% endif %}
    {% endfor %}

CodePudding user response:

In the context, you used 'Review' and in the template you are using 'review.id' which is None, unify the keys and it shall work.

CodePudding user response:

Look at your edit_review. You named object lowercase review. So in context you are not passing an object, but actually Review class. So Review.id == None and it cannot create proper {% url 'edit_review' ... %} from that. Another thing, in context you are also naming it uppercase, which you shouldn't. So basically you don't pass 'review' context at all.

@login_required
def edit_review(request, review_id):
    review = get_object_or_404(Review, pk=review_id)   
    (...)
    context = {
        'review_form': review_form,
        'Review': Review,
    }

So the thing you need to change, is this:

    context = {
        ...
        'Review': Review,  ===>   'review': review,
    }
  • Related