django文件上传 - 隐藏编辑模板中当前显示的图像链接 [英] django file upload - hide the currently displayed image link in the edit template
问题描述
我有一个测试模板,允许用户将上传的文件图像以及图像标题作为字符字段添加到用户帐户。这是工作正常。
我现在试图允许用户编辑已经上传的文件图像和图像标题的细节。
有没有可能隐藏图片链接,而是将图片显示在其位置上?
。
您必须按顺序在模板中设置 标签的 src 属性
< img src ={{attachment_details.file_url}}/>
{{attachment_details.file_url}}当然必须替换为您正在检索文件的网址。
I have a test template that allows a user to add an uploaded file image as well as the image title as a char field to the users account. This is working OK.
I am now trying to allow the user to edit the already uploaded file image and the image title details.
Is it possible to hide the image link and instead display the image in its place?
I have read the django docs, searched SO & Google and worked on and off for 2 1/2 days but I am still unable to resolve this issue.
There seems to be plenty of information of how to upload a new file image but not how to edit the already uploaded file image.
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())
class AttachmentDetails(models.Model, FillableModelWithLanguageVersion):
user = models.ForeignKey(User)
language_version = models.ForeignKey('LanguageVersion')
attachment_document = models.FileField(upload_to=_get_document_upload_location)
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 file 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=False,
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:
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)
EDIT
Here is an example of what I am trying to hide from the user - in place of the link, display the actual uploaded file image. The image below is of the django admin, but I want to hide the link and display the uploaded image in the edit template:
I have a related post here.
You will have to set the src attribute of the img tag in your template in order to display the image successfully.
<img src="{{attachment_details.file_url}}"/>
{{attachment_details.file_url}} of course has to be replaced with however you are retrieving your file url.
这篇关于django文件上传 - 隐藏编辑模板中当前显示的图像链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!