I am trying to update an instance from Author table, But form.is_valid returning false always.
I have added enctype="multipart/form-data" in the template form, In the views im getting files also, but form is not validating.
Am I missing anything?
This is views section.
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/')
This is a template.
<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>
Author Form:
class Author_form(ModelForm):
required_css_class='required'
class Meta:
model=Author_model
fields='__all__'
Author model:
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)
CodePudding user response:
You have customize your form inputs so they can not match with your form fields in server-side!
So form_valid() is always False.
I update your template:
<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>
Also take a look at your browser DevTools in order to make sure their values before you get a request.
CodePudding user response:
Since you are using custom html so you need to set name attributes of your html according to the field name of your model. So form.is_valid() always returns False if the name attribute is different or have not mentioned.
<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>
