我想创建一个将在应用程序的不同部分中使用的函数。这是一个发送电子邮件通知的功能,类似于:
def mail_notification():
subject = 'New Product'
from_email = 'master.aligabra@gmail.com'
to = 'ali.corretor.imoveis@gmail.com'
html_content = render_to_string('email/new_message_product_client_email.html')
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
msg.content_subtype = "html"
msg.send()
我的怀疑是,在View, signals.py, forms.py
中,Django有一个";右";地点还是我喜欢的地方?
我建议创建单独的地方来存储常用函数。这里是Django 的默认项目结构
django-app/
django-app/
users/ # django application
migrations/
models.py
views.py
tests.py
utils.py # Here I place helper functions related to User application
common/ # folder with common functions for all django applications
email.py # functions to work with email
string.py # hepler functions to parse and modify strings
...
因此,我建议将您的函数放在common/email.py
中,并在模型和视图中导入它。