尽管已经被问过,为什么我会问问题?我在Stack Overflow上发布了很多问题,但我无法修复代码,因为我不熟悉Python语言。
我想做什么:只是尝试从用户那里获取输入并返回HttpResponse
(如果成功)。否则,将返回错误HttpResponse
消息。
问题: Forms.py 中的MyForm.is_valid()
始终返回False
!我在之前的问题上尝试了许多解决方案,并且还阅读了三次纪录片但却无法理解,我做错了什么?
from django.http import HttpResponse
from .forms import PostForm
.
.
. <<code here>>
def register(request):
if request.method == 'POST':
Myform = PostForm(request.POST)
if Myform.is_valid():
return HttpResponse("<h1>Is_Valid is TRUE.</h1>")
else:
return HttpResponse("<h1>Is_Valid is False.</h1>")
else:
return HttpResponse("<h1> GET REQUEST>>>> </h1>")
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model= Post
fields = ['Username']
from django.db import models
class Post(models.Model):
Username = models.CharField(max_length = 20)
def __str__(self):
return self.name
{% block body %}
<div class="container-fluid">
<form method="POST" class="post-form" action="{% url 'submit' %}">
{% csrf_token %}
<div class="form-group"> <!-- Full Name -->
<label for="Username" class="control-label">Full Name</label>
<input type="text" class="form-control" id="Username" name="full_name" placeholder="Enter the name of Patient here.">
</div>
<div class="form-group"> <!-- Submit Button -->
<button type="submit" class="btn btn-primary"> Submit!</button>
</div>
</form>
</div>
<hr/>
{% endblock %}
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^/submit$', views.register , name = 'submit'),
]
答案 0 :(得分:2)
您输入的名称应为username
:
这是您将此值发送到表单的方式。
注意:最好使用django表单,你已经使用ModelForm
<input type="text" class="form-control" id="username" name="username" placeholder="Enter the name of Patient here.">