1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the feedback. if nothing good comes up, then yours is a way of doing this :)
0

Since you are including your script in the views, it is supposed that:

  1. Your JSON file does not change frequently
  2. 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,

  1. fetches the JSON file if it is expired (e.g. if the last fetch happened long time ago)
  2. 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.

3 Comments

ah-ha middleware!! now that's interesting. The JSON file does change frequently, more specifically its updated every 5 min interval. My thinking is, when the middleware is initiated or called, it fetches the JSON data and the information is then processed and passed to the view which can be passed onto the render function for action by the user. Does this make sense?
Please keep in mind that, for my understanding of middleware, you can only manage requests (other things as well, but may not be relevant for your issue). So it would be appropriate to decorate your request with the JSON file. This way you can freely inspect the request in your render function.
I had some time to read through the docs and it seems that a middleware just might not be the right place for this. middlewares are used for completely different purpose and having the code which fetches and parses JSON there doesn't seem to add up.. yet. thanks for your input. appreciate the feedback :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.