My django application has a file uploader which uploads to a specific location in my local system.
It redirects to a new html page which shows successful message after upload is done.
Once the file is uploaded I need to do some processing of csv files.
I have a python code which does the processing.
Now my question is, where do I put the python file in the django project and how do i call it to be run once the upload is done?
Any help is appreciable
Thanks in advance
You can place it anywhere you like, it's just Python. Maybe in a csv_processing.py if it fits in a single module, or as a completely independent library if it's more. Django doesn't have an opinion on this.
The best way to run it is by doing it asynchronously using Celery.
Make sure the file is within a python package, you do this by adding init.py to the directory, documentation here. In accordance to Django convention; you would place the file within the app that needs to use it, or within another app you would name utils, documentation here.
Question:
I need the file to be run completely after the application uploads the file.
Answer:
new = Storage()
new.file = request.FILES['file']
new.save()
Now we have the database id. (When file object saved into database it emits the id).
originalObj = Storage.objects.get(pk=new.id)
Now you can import the csv file and do modification here.
Related
I am new to Flask and web dev in general, so bear with me.
I've been working on a Flask application where the user submits a file to a form, the server takes it and manipulates it, then returns the edited file back to the user. I am aware that the options are to manipulate the file in memory or store it on the server. I want to be able to return the file to the user, preferably using , but Im not sure which way to go. I already have the file object in my routes code, but am not sure where to go from there. Any help is appreciated!
Use flask.send_from_directory.
Noob here with Django. I have the following folder structure for a Django app, which is inside the main project folder.
my_app/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
I have a command line python script I wrote to fetch a JSON file and parse it to displays very specific information. it's using requests library for JSON and data parsing.
My question is how do I integrate my script into Django app. specifically how to bring the logic of it and to place under which file? My thinking is to create another file and import them into views. and pass them into render function - this maybe not the right and Django way, but kinda stuck there. Oh and I don't use any DB, the script uses a text file and writes to it as well.
I place a folder called services in my Django app and for each non-django stuff I add a folder. But that is basically just convenience, Django is not posing restrictions on you here.
Since you are including your script in the views, it is supposed that:
Your JSON file does not change frequently
It is not an issue to have multiple requests if your Django app is spawned many times (e.g. if you use uwsgi or gunicorn)
If such is the case, any Pythonic solution will be fine.
If 1 does not apply, it may be the case that you should implement your own middleware that, upon request,
fetches the JSON file if it is expired (e.g. if the last fetch happened long time ago)
adds the JSON file content in the request: this way you do not have to disclosure where you are keeping your JSON file
If you have multiple instances, you can do more or less the same as 1, but you might decide to store the JSON value in a separate storage, together with its expiration. You might also configure uwsgi/guincorn to fetch the JSON file at startup: this way you will not fetch the JSON file multiple times at startup. Then your middleware will do the work to keep it up-to-date, if necessary.
I am building a Django web interface (overkill - I know!) for a few small python functions. One of these transforms a txt file stored in Django root. Well it aims to.
I have the following setup in a few places:
with open('file.csv','r') as source:
...
However, without setting the entire directory on my machine (e.g. /home/...), it cannot find the file. I have tried putting this in the Static directory (as ideally I would like people to be able to download the file at a later stage) but same problem.
How do you work with files within Django? What is best practice to solve the above allowing someone to download it later?
If you only need the path:
import os
from django.conf import settings
file_path = os.path.join(settings.STATIC_ROOT, 'file.txt')
with open(file_path, 'r') as source:
# do stuff
Please notice that you need to put your file in a directory like this: my_app/static/my_app/file.txt
for more information you can refer to Django docs.
I am creating an application in django where I want to upload multiple file from django admin. I also want these files to be associated with a particular user in my database. for e.g a pdf file will have a file names as 'john.pdf', 'matt.pdf', 'alice.pdf' and I want to upload all these files at once from django admin and each file should be associated with particular user, so if user john logs in he can see pdf 'john.pdf' in his profile page.
I am new to django and web programming and I have been banging my head for a couple of days but I just cannot find the right logic to implement such a code.
I have looked in these resources but I still cannot really find an answer
How to upload multiple file in django admin models
How to upload through django admin.
I am using django with mysql database. I highly appreciate and thank anyone in advance who could help me out with this problem. (hoping not to get downvoted too much)
Ok so I found out a solution but I still don't know if thats the best solution there. What I did was to add path of my pdf files in MySQL database and add attributes to those paths. I wrote a script in python to add those files to MySQL database. My script listed all the files in the directory, where the files were stored and added them to the database. From these attributes I could retrieve those paths. As these are just paths to static files I had to save them in the static folder for django to access and display them and the rest was simple.
I currently have a django app which generates PDFs and saves them to a specific directory. From the admin interface I want to have some way to view the list of files within that directory (similar to models.FilePathField()), but also be able to download them. I realize that django was never intended to actually serve files, and I have been tinkering with django-sendfile, as a possible option. It just doesn't seem like there is any way to create a dynamic list of files other than with FilePathField (which I don't believe can suite my purposes).
Would this project fit your needs? http://code.google.com/p/django-filebrowser/
I ended up realizing that I was going about the problem in a more complicated manner than was necessary. Using two separate views trivializes the issue. I was just under the impression that the admin interface would include such a basic feature.
What I did was create a download_list view to display the files in the directory, and a download_file view which uses django-sendfile to serve the file to the end-user. Download_file simply parses through the directory with listdir(), checks if the extension is valid and sends the complete file path to the download_file function (after the user selects one).
Are the files in a directory that is served by your webserver? If all you want to do is list and download the files, it may be easier just to have a link to the directory with all the files in it and let the webserver take care of listing and serving the files. I understand this may not be the ideal solution for you, but it does avoid having Django serve static files, which is a task best left to the webserver.