I am trying to add a comment section in products page, I am now trying to test the template to see if it directing correctly
After I press the submit button nothing is happening it doesnt return any errors
Here is the Models.py
class Comment(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="ItemComments")
subject = models.CharField(max_length=50, blank=True)
comment = models.CharField(max_length=250, blank=True)
def __str__(self):
return '{} by {}'.format(self.subject, str(self.user.username))
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['subject', 'comment']
here is the urls
app_name = 'core'
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('<slug>/addcomment/', views.addcomment, name='addcomment'),
Here is the views
def addcomment(request):
return HttpResponse("My product Page")
Here is the template
<form class="review-form" action={% url 'core:addcomment' item.slug %} method= "post">
{% csrf_token %}
<div class="md-form md-outline">
<input name="subject" type="text" id="form75" class="form-control pr-6">
<label for="form75">Your Subject</label>
</div>
<div class="md-form md-outline">
<textarea name="comment" id="form76" class="md-textarea form-control pr-6" rows="4"></textarea>
<label for="form76">Your review</label>
</div>
{% if request.user.is_authenticated %}
<div class="text-right pb-2">
<button type="button" class="btn btn-primary waves-effect waves-light">Add a review</button>
</div>
{% else %}
You must be Logged in to Comment
{% endif %}
</form>
There isn't actually a submit button in the form. Try changing the button type to submit
like this -
<button type="submit" class="btn btn-primary waves-effect waves-light">Add a review</button>
<form class="review-form" action={% url 'core:addcomment' item.slug %} method= "post">
{% csrf_token %}
<div class="md-form md-outline">
<input name="subject" type="text" id="form75" class="form-control pr-6">
<label for="form75">Your Subject</label>
</div>
<div class="md-form md-outline">
<textarea name="comment" id="form76" class="md-textarea form-control pr-6" rows="4"></textarea>
<label for="form76">Your review</label>
</div>
{% if request.user.is_authenticated %}
<div class="text-right pb-2">
<button type="submit" class="btn btn-primary waves-effect waves-light">Add a review</button>
</div>
{% else %}
You must be Logged in to Comment
{% endif %}
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.