Reputation: 2387
The 'Submit' button in this form, in this Django project, does not seem to do anything. I cannot spot the logic error in the code or files.
sign.html (this is the page that displays). On clicking the submit button, it does nothing, but it should populate the database.
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{% static 'guestbook/styles.css' %}">
</head>
<body>
<h1>Tell the world how you're doing!</h1>
<h2>Sign the guestbook</h2>
<form class="form-signin" method="POST" action="{% url 'sign' %}">
{% csrf_token %}
Enter your name:<br>
<!--<input type="text" name="name" placeholder="Your Name here">-->
{{form.name}}
<br>
Enter your comment:<br>
<!--<textarea name="message" type="Textarea" placeholder="Your comment here" rows="10" cols="30"></textarea>-->
{{form.comment}}
<br><br>
<input type="button" value="Submit">
</form>
<p>Go to the <a href="{% url 'index' %}"> guestbook </a> itself</p>
</body>
</html>
I suspect the problem is in the code below or possibly in the views.py file, but as it is not throwing any exceptions I cannot find it.
It is the sign function below that is the relevant one to this question.
views.py
from django.shortcuts import render
from .models import Comment
from .forms import CommentForm
# Create your views here.
def index(request):
comments = Comment.objects.order_by('-date_added')
context ={'comments': comments}
#name=Name.objects.order_by('-date_added')
#return render(request,'guestbook/index.html')
return render(request,'guestbook/index.html', context)
def sign(request):
if request.method=='POST':
form = CommentForm(request.POST)
if form.is_valid():
new_comment=Comment(name=request.POST['name'],comment=request.POST['comment'])
new_comment.save()
return redirect('index')
else:
form = CommentForm()
context={'form' : form}
return render(request,'guestbook/sign.html',context)
The models file creates the model for the name and comment which is to be saved to the database.
And finally, models.py
from django.db import models
from django.utils import timezone
# Create your models here.
class Comment(models.Model):
name=models.CharField(max_length=20)
comments=models.TextField()
date_added=models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
"""
{% for c in comment %}
{% endfor %}
"""
Upvotes: 0
Views: 5462
Reputation: 6404
A form is submitted by a button which type is submit inside
<form>
<!-- button goes here and input fields also -->
</form>
Change this
<input type="button" value="Submit">
to
<input type="submit" value="Submit">
Then in views.py
Change this new_comment=Comment(name=request.POST['name'],comment=request.POST['comment'])
To
new_comment = Comment()
new_comment.name = request.POST.get("name")
new_comment.comments = request.POST.get("comment")
new_comment.save()
Upvotes: 3
Reputation: 1022
Your post method should be in this way:
def sign(request):
if request.method=='POST':
form = CommentForm(request.POST)
if form.is_valid():
new_comment=form.save()
return redirect('index')
else:
form = CommentForm()
context={'form' : form}
return render(request,'guestbook/sign.html',context)
Upvotes: 1