Django form.is_valid() 返回 false
Django form.is_valid() returning false
我正在尝试更新来自作者 table 的实例,
但是 form.is_valid 总是返回 false。
我在模板表单中添加了enctype="multipart/form-data",
在我也正在获取文件的视图中,
但表单未通过验证。
我错过了什么吗?
这是浏览量部分。
def update(request,something,id):
something=str(something).lower()
if something=='author':
author=Author_model.objects.get(id=id)
if request.method=='GET':
return render(request,'update_author.html',{'author':author})
else:
form = Author_form(request.POST, request.FILES,instance=author)
print('form.is_valid()',form.is_valid())
if form.is_valid():
image_path = author.image_document.path
if os.path.exists(image_path):
os.remove(image_path)
form.save()
return redirect('http://127.0.0.1:8000/bookdb/author/')
这是一个模板。
<form action="" method="post" enctype="multipart/form-data">
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="" id="">
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" value="{{author.name}}"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" value="{{author.email}}"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>
作者表:
class Author_form(ModelForm):
required_css_class='required'
class Meta:
model=Author_model
fields='__all__'
作者模型:
class Author_model(models.Model):
solutaion=models.CharField(max_length=10)
name=models.CharField(max_length=30)
email=models.EmailField(blank=True)
headshot=models.ImageField(upload_to='imgs')
def __str__(self):
return self.name
Urls.py
from django.contrib import admin
from django.urls import path
from model_app import views as v
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('bookdb/<something>/',v.show_something),
path('publishers/<name>/',v.a_publisher),
path('publishers/<name>/<id>/',v.req_cols),
path('books/',v.books),
path('books/<name>/',v.book),
path('book_one/<title>/',v.book_one),
path('book_with_publisher/<title>/<pub>/',v.book_with_publisher),
path('Add/<someone>/',v.user_form),
path('delete/<something>/<id>/',v.delete_item),
path('update/<something>/<id>/',v.update)
]
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
您已自定义 form inputs
,因此它们无法与 server-side 中的 form fields
匹配!
所以 form_valid()
总是 False
.
我更新了你的模板:
<form action="" method="post" enctype="multipart/form-data">
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="solutaion" id="" >
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" value="{{author.name}}" name="name"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" value="{{author.email}}" name="email"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file" name="headshot"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>
还请查看您的浏览器 DevTools
,以便在收到请求之前确保它们的值。
由于您使用的是自定义 html,因此您需要根据模型的 field
名称设置 html
的 name
属性。所以 form.is_valid()
总是 returns False
如果 name
属性不同或没有提到。
<form action="/update/author/{{author.id}}" method="post" enctype="multipart/form-data"> #<--- Here I set `author` in place of `something` add values accordingly
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="solutaion" id="">
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" name="name" value="{{author.name}}"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" name="email" value="{{author.email}}"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file" name="headshot"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>
我正在尝试更新来自作者 table 的实例, 但是 form.is_valid 总是返回 false。
我在模板表单中添加了enctype="multipart/form-data", 在我也正在获取文件的视图中, 但表单未通过验证。
我错过了什么吗?
这是浏览量部分。
def update(request,something,id):
something=str(something).lower()
if something=='author':
author=Author_model.objects.get(id=id)
if request.method=='GET':
return render(request,'update_author.html',{'author':author})
else:
form = Author_form(request.POST, request.FILES,instance=author)
print('form.is_valid()',form.is_valid())
if form.is_valid():
image_path = author.image_document.path
if os.path.exists(image_path):
os.remove(image_path)
form.save()
return redirect('http://127.0.0.1:8000/bookdb/author/')
这是一个模板。
<form action="" method="post" enctype="multipart/form-data">
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="" id="">
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" value="{{author.name}}"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" value="{{author.email}}"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>
作者表:
class Author_form(ModelForm):
required_css_class='required'
class Meta:
model=Author_model
fields='__all__'
作者模型:
class Author_model(models.Model):
solutaion=models.CharField(max_length=10)
name=models.CharField(max_length=30)
email=models.EmailField(blank=True)
headshot=models.ImageField(upload_to='imgs')
def __str__(self):
return self.name
Urls.py
from django.contrib import admin
from django.urls import path
from model_app import views as v
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('bookdb/<something>/',v.show_something),
path('publishers/<name>/',v.a_publisher),
path('publishers/<name>/<id>/',v.req_cols),
path('books/',v.books),
path('books/<name>/',v.book),
path('book_one/<title>/',v.book_one),
path('book_with_publisher/<title>/<pub>/',v.book_with_publisher),
path('Add/<someone>/',v.user_form),
path('delete/<something>/<id>/',v.delete_item),
path('update/<something>/<id>/',v.update)
]
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
您已自定义 form inputs
,因此它们无法与 server-side 中的 form fields
匹配!
所以 form_valid()
总是 False
.
我更新了你的模板:
<form action="" method="post" enctype="multipart/form-data">
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="solutaion" id="" >
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" value="{{author.name}}" name="name"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" value="{{author.email}}" name="email"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file" name="headshot"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>
还请查看您的浏览器 DevTools
,以便在收到请求之前确保它们的值。
由于您使用的是自定义 html,因此您需要根据模型的 field
名称设置 html
的 name
属性。所以 form.is_valid()
总是 returns False
如果 name
属性不同或没有提到。
<form action="/update/author/{{author.id}}" method="post" enctype="multipart/form-data"> #<--- Here I set `author` in place of `something` add values accordingly
<center>
<h2>Update Author</h2><hr>
<table>
<tr>
<td>Solutation </td>
<td>: <select name="solutaion" id="">
<option value="{{author.solutaion}}">{{author.solutaion}}</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select></td>
</tr>
<tr><td >Name </td>
<td>: <input type="text" name="name" value="{{author.name}}"></td></tr>
<tr>
<td>Email </td>
<td>: <input type="email" name="email" value="{{author.email}}"></td>
</tr>
<tr>
<td>Headshot {{author.headshot}}</td>
<td><img src="{{author.headshot.url}}" alt="Image not found" width="100" height="60"></td>
<td>: <input type="file" name="headshot"></td>
</tr>
</table><br>
<button type="submit">Submit</button>
</center>
{%csrf_token%}
</form>