Reputation: 21
I'm using my own project using django. I'm trying to download file using django. I finished upload file to using django. but i don't know how to create file download link in django. example) test.java , and my domain is example.com, port is 9001 and media folder is /media i just want to down https://example.com:9001/media/test.java just like this. I google all method but there's no clue.. here is my code. view.py -> upload part
@csrf_exempt
def index(request):
return render(request, 'useraccount/index.html', {})
@csrf_exempt
def file_list(request):
return render(request, 'useraccount/list.html', {})
@csrf_exempt
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('file_list')
else:
form = UploadFileForm()
return render(request, 'useraccount/upload.html', {'form': form})
upload.html
<html>
<head><title>Upload Test</title></head>
<body>
<form action="upload/"
method="post"
enctype="multipart/form-data">
File:
<input type="file"
name="file"
id="id_file" />
<input type="submit" value="UPLOAD" />
</form>
</body>
</html>
upload.html
{% extends 'useraccount/index.html' %}
{% block content %}
<h2>Upload file</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button type="submit">Upload file</button>
</form>
{% endblock %}
list.html
{% extends 'useraccount/index.html' %}
{% block content %}
<h2>The image has been uploaded!!</h2>
{% endblock %}
forms.py
from django import forms
from .models import UploadFileModel
class UploadFileForm(forms.ModelForm):
class Meta:
model = UploadFileModel
fields = {'title', 'file'}
url.py
from django.urls import path, include
from django.conf.urls import url
from . import views
from django.conf import settings
from django.conf.urls.static import static
path('upload/', views.upload_file, name='upload_file'),
path('list/', views.file_list, name='file_list'),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Upvotes: 0
Views: 5703
Reputation: 2798
To download an image stored in database, it would be :
Simple Example
views.py
def download_image(self, imageid):
image = UploadFileModel.objects.get(pk=imageid)
image_buffer = open(image.file.path, "rb").read()
content_type = magic.from_buffer(image_buffer, mime=True)
response = HttpResponse(image_buffer, content_type=content_type);
response['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(image.file.path)
return response
urls.py
path('downloadimage/<str:imageid>/$', views.download_image, name='download_image'),
template.html
<a href="{% url 'useraccount:download_image' imageid=<id_of_image> %}" type="button">Download image</a>
Note: Please replace
<id_of_image>
in template with image id required.
Upvotes: 1
Reputation: 708
you can generate url
file by url
methods like that:
in .py:
my_file_field.url
in template:
<a href="{% my_file_field.url %}"> file link ! </a>
see django's doc https://docs.djangoproject.com/en/dev/topics/files/
Upvotes: 0