4👍
✅
If you form class is a modelform: class ImageForm(forms.ModelForm):
you can pass the instance to the form and save it.
def display_update_image(request, pk):
if request.method == 'POST':
display = Display.objects.get(pk = pk)
form = ImageForm(request.POST, request.FILES, instance=display)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('displays'))
else :
form = ImageForm()
return render(request, 'catalog/update_display_image.html', {'form': form})
0👍
In forms.py
check that you have a Django Form like this one:
class ImageForm(forms.ModelForm):
class Meta:
model = Display
fields = ['image',]
In template
add {{ form }}
instead of manually using input
, like this one:
<form method='post' enctype='multipart-formdata>
{% csrf_token %}
{{ form }}
<input type='submit' value='Update Image'>
- [Django]-Is sqlite bundled into Django?
- [Django]-TypeError: at / 'module' object is not callable
- [Django]-Why doesn't a django sqlite3 database work the same on one machine vs the other?
- [Django]-Django cors headers and server error
- [Django]-How can I use RESTful services with Django + Mongoengine?
Source:stackexchange.com