Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to create a reusable function in Django

I am new to django and have been working on a project where i need to send regular mails to my clients from different modules in my django project.

I wanted to know if there is any provision in django using which i can create a reusable component which can be used globally in my project.

The purpose is to create a mail sending function using a third party api and to call it from anywhere in my application just by passing the required parameters.

like image 987
Vivek Cu Avatar asked Jan 04 '23 10:01

Vivek Cu


1 Answers

Assuming that you have a single django app in your project, you could define all your reusable methods inside a methods.py file in your app folder and then import it to use the functions.

If you have multiple apps, Then define create this methods.py in one of your app. Now lets say your project name is coolprojectname and the name of app which has methods.py is appwithmethodsscript, then you can use

from coolprojectname.appwithmethodsscript import methods

Reference

like image 157
Anuj Avatar answered Jan 05 '23 23:01

Anuj