I am pretty new to django and python. I have a number of different csv files with same format in my app in django project. I want to choose a particular file using the hyperlink of the project(website). How can I do that? Are there some other methods?
-
Please explain what you mean by " I want to choose a particular file using the hyperlink of the project(website)." Do you want to serve the files as static files or do you want somebody to be able to upload CSV files to your website? – Jonatan Oct 27 '20 at 10:08
-
I have 10 csv files (with different data but same metadata) in my project. I want to choose any of these files through a link. how can i do this? Will it require a website link ending with file name (which may sound absurd) or something else? – Ankit Jain Oct 27 '20 at 14:50
-
if the files are attached to models you can use the url ending with the pk of the model to access the model – nihilok Oct 27 '20 at 16:03
-
@nihilok they are not attached with models but i can do. Can you explain a bit more or some link. – Ankit Jain Oct 27 '20 at 16:12
2 Answers
You are asking how to serve static files. For details read the documentation.
If you are doing this on an offline server, you simply have to define where Django can find static assets (a folder name) on your projects settings page. You do this by putting STATIC_URL = '/static/'
in your settings and then put all your csv files in a folder call static
which should be in the root of your project. You can then access your files and get their urls by using the {%static%} tag. For deployment you also need to define STATIC_ROOT
in your settings.
If you need to serve static files in production you can use something like whitenoise for ease of use.

- 1,182
- 8
- 20
In addition to the static files method, and in response to the comment asking for more information,
if you create your model thusly:
class CSVFile(models.Model):
file = models.FileField(upload_to='csv_files/')
then you can upload the files through the django admin or programmatically and pass a model instance as context through your view e.g
def view(request):
files = CSVFile.objects.all()
return render(request, 'template.html, {'files': files})
you could then have a second view to download the file(s):
def download(request, pk):
csv = CSVFile.objects.filter(pk=pk).first()
filename = csv.file.name.split('/')[-1]
response = HttpResponse(csv.file, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
Then in urls.py:
urlpatterns = [
path('', views.view, name='home'),
path('file/<int:pk>', views.download, name='download'),
]
Then in template:
<body>
<h1>Download your CSVs!</h1>
{% for file in files %}
<p><a href="{% url 'download' pk=file.pk %}"><button type="button">Download {{ file.file.name }}</button></a></p>
{% endfor %}
</body>
Which would look something like this:
One last thing, before you save your files to the model, set the media root in your settings.py something like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
See more about fields here and scroll all the way down to/search for FileField, and here for managing files in models

- 1,325
- 8
- 12
-
hey @nihilok thanks for the help. Can you tell how to open this(file = CSVFile.objects.filter(id=?).first() ) csv file in views.py. I could not find this solution. – Ankit Jain Oct 30 '20 at 08:26
-
it depends what you want to do with it.. If it's within the python code, you may not need to use a 'django' method at all. If you just want to access the file from the template, I will add some more specific examples to my answer – nihilok Oct 30 '20 at 09:13