[英]Submitting a Comment form in Product Page not working
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这是 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 -尝试更改按钮类型以像这样
submit
-
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.