oz19
oz19

Reputation: 1876

Good practice for helper functions in Django views

I am working on a project in Django. On my views.py I need to make use of multiple helper functions. In order to keep my code clean, I am going to create another file to wrap all these functions. I was planning to call the file just functions.py or helpers.py.

Which is the good practice to add helper functions for Django views? Is there any kind of convention, rule or anything?

UPDATE: These functions are closely related to the app itself. They have no sense out of their app.

Thanks!

Upvotes: 6

Views: 2334

Answers (1)

Hyster
Hyster

Reputation: 726

The cleanest way would be to create multiple files with only functions related to each other. Ideally, if they are app-agnostic, put them in a python package outside of the django app you are using.

Ie. All functions related to users go to view_helpers/users.py, All function related to json go to view_helpers/json.py

The directory structure would be like that

django_project/
    main_django_app/
        __init__.py
        views.py
        settings.py
        ...
    view_helpers/
        __init__.py
        json.py
    manage.py

Upvotes: 2

Related Questions