Reputation: 301
I'm trying to create a view that would add a review to a specific product in my Django app but I keep getting a 405 error Method Not Allowed
.
2 of us tried to solve this but couldn't achieve anything..
Here's what we've been trying so far:
Models.py;
class Review(models.Model):
user = models.ForeignKey(UserProfile)
product = models.ForeignKey(Product)
review = models.TextField()
is_positive = models.BooleanField()
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = (('user', 'product'),)
def __str__(self):
return '{}'.format(self.review)
Views.py;
class ReviewCreate(LoginRequiredMixin, UserOnlyMixin, CreateView):
model = Review
fields = ['user', 'product', 'review', 'is_positive']
template = "product.html"
def get_success_url(self):
kwargs = {'slug': self.object.product.slug}
url = reverse_lazy("experience", kwargs=kwargs)
return url
Urls.py (no duplicates found);
url(r'^experience/(?P<slug>[\w-]+)/$', ProductView.as_view(), name='experience'),
url(r'^experience/create-review/$', ReviewCreate.as_view(), name='add-review'),
url(r'^reservation/(?P<slug>[\w-]+)/$', BookingView.as_view(), name='booking'),
and in the template product.html;
<form action="{% url 'add-review' %}" class="writearev-form" method="post">
{% csrf_token %}
<input type="hidden" name="product" value="{{ object.pk }}">
<input type="hidden" name="user" value="{{ user.userprofile.pk }}">
<label class="control control--radio control-one">
<input value="true" id="chkTrue" type="radio" name="is_positive">Avis positif
<div class="control__indicator"></div>
</label>
<label class="control control--radio control-two">
<input value="false" id="chkFalse" type="radio" name="is_positive">Avis négatif
<div class="control__indicator"></div>
</label>
<textarea name="review" for="writearev-label" type="textarea" class="input-writearev" placeholder="Rédiger votre avis.."></textarea>
<button type="submit" class="btn-writearev">Publier</button>
</form>
How can this be fixed?
Please help
Upvotes: 0
Views: 255
Reputation: 88619
The add-review
view must be placed in the top of experience
. In your case, the create-review
will match with [\w-]+
and Django will try to send a POST request to experience
view.
url(r'^experience/create-review/$', ReviewCreate.as_view(), name='add-review'),
url(r'^experience/(?P<slug>[\w-]+)/$', ProductView.as_view(), name='experience'),
url(r'^reservation/(?P<slug>[\w-]+)/$', experienceBookingView.as_view(), name='booking'),
Upvotes: 1