I am using django to allow a user to upload images along with text to describe the image title.
The user should be able to edit the image title and change the image file using the edit template. This does work - to some degree.
I can display the image file and the image title in the edit template for editing. The issue I have is when I do not include an image file in the file upload input (the user only wants to change the image title and not the image file), the code I have does change the image title but the image file is deleted from the database (the physical image file is not deleted from the file system).
I can successfully update the image details but the user must include a new image file or the same image file in the file upload input field for the image title the image details to be successfully updated.
It seems the user must include an image file in the file upload input field. I am trying to get my code to work just like the django admin where the user does not have to include an image file each time the user changes the image title.
How do I allow a user to update the image details without having to re-upload the image file each time the user wants to change the image title?
Here is my models.py code:
def _get_document_upload_location(instance, filename):
"""
Using a function instead of a lambda to make migrations happy. DO NOT remove or rename this function in the future, as it will break migrations.
@param instance: model instance that owns the FileField we're generating the upload filename for.
@param filename: Original file name assigned by django.
"""
return 'attachments/%d/%s' % (instance.user.id, uuid.uuid4())
user = models.ForeignKey(User)
language_version = models.ForeignKey('LanguageVersion')
attachment_document = models.FileField(upload_to=_get_document_upload_location, null=True, blank=True)
attachment_title = models.CharField(null=False, blank=False, max_length=250)
attachment_timestamp_added = models.DateTimeField(auto_now_add=True, auto_now=False)
attachment_timestamp_updated = models.DateTimeField(auto_now=True, auto_now_add=False)
Here is my views.py code:
def attachment_details_edit(request, attachment_details_id):
try:
attachment_details = AttachmentDetails.objects.get(pk=attachment_details_id, user=request.user)
except AttachmentDetails.DoesNotExist:
return redirect(settings.MENU_DETAIL_LINK_ATTACHMENT_DETAILS)
language_versions = LanguageVersion.objects.filter(user=request.user).select_related('language_version')
available_languages = get_available_language_details(language_versions, request.user.userprofile.language_preference)
attachment_details_num = request.user.attachmentdetails_set.count()
language_code = attachment_details.language_version.language_code
language_code_disabled = attachment_details.language_version.language_code_disabled
language_preference = request.user.userprofile.language_preference
if language_code_disabled:
return redirect(settings.MENU_DETAIL_LINK_ATTACHMENT_DETAILS)
if request.method == 'GET':
language_code = attachment_details.language_version.language_code
form = AttachmentDetailsForm(
available_languages,
language_preference=request.user.userprofile.language_preference,
file_required=True,
initial=dict(
model_to_dict(attachment_details),
language_code=language_code
)
)
elif request.method == 'POST':
form = AttachmentDetailsForm(
available_languages,
language_preference,
False, # file_required
request.POST,
request.FILES
)
if form.is_valid():
cd = form.cleaned_data
if cd['attachment_document'] is not None:
# delete the existing uploaded attachment when user updates the existing attachment with a replacement attachment.
print 'removing previously uploaded file'
attachment_details.attachment_document.delete(save=False)
attachment_details.fill(cd)
attachment_details.save()
messages.success(request, _('successfully updated.'))
return redirect(settings.MENU_DETAIL_LINK_ATTACHMENT_DETAILS)
I have a related post here.
Aucun commentaire:
Enregistrer un commentaire