在此Django项目中,这种形式的“提交”按钮似乎没有任何作用。我无法在代码或文件中发现逻辑错误。
sign.html (这是显示的页面)。单击提交按钮后,它什么都不做,但是应该填充数据库。
data want;
set test;
_x=tranwrd(x,strip(prxchange('s/.*(?<=\_)((\d+)).*/$1/',1,x)),strip(prxchange('s/.*(?<=\_)((\d+)).*/$1/',1,x)+15));
run;
我怀疑问题出在下面的代码中,或者可能在views.py文件中,但是由于它没有引发任何异常,所以我找不到它。
下面的符号功能与此问题相关。
views.py
{% 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>
models文件为要保存到数据库的名称和注释创建模型。
最后, models.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)
答案 0 :(得分:2)
表单是通过按钮提交的,其类型是在内部提交
<form>
<!-- button goes here and input fields also -->
</form>
更改此
<input type="button" value="Submit">
到
<input type="submit" value="Submit">
然后在 views.py 中
更改此new_comment=Comment(name=request.POST['name'],comment=request.POST['comment'])
到
new_comment = Comment()
new_comment.name = request.POST.get("name")
new_comment.comments = request.POST.get("comment")
new_comment.save()
答案 1 :(得分:1)
您的发布方法应采用这种方式:
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)