如何在Django项目中使用外部python实用工具文件? [英] How do I use external python utility files in a Django project?

查看:48
本文介绍了如何在Django项目中使用外部python实用工具文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的Django项目的名称为"django_project",其中包含一个名为"main"的应用程序,并且我创建了一个名为"utilities_dir"的目录,其中包含一些python文件(例如"utility_1.py","utility_2").py'et-cetera)具有我要使用的功能.我在哪里放置"utilities_dir",以及如何在主"应用程序目录中的"views.py"文件中使用这些文件?谢谢!

Suppose the name of my Django project is 'django_project' which contains an app named 'main', and I have created a directory called 'utilities_dir' that contains a few python files (like 'utility_1.py', 'utility_2.py' et-cetera) having the functions I want to use. Where do I place the 'utilities_dir' and how do I use those files in, say, my 'views.py' file in the 'main' app directory? Thanks!

推荐答案

将实用程序dir放置在项目根文件夹中,将 __ init __.py 文件添加到该文件夹​​中,然后在您要使用实用程序功能时,只需这样做:

Place utilities dir in your project root folder, add __init__.py file to the folder then when you want use your utilities function, just do that:

from utilities_dir.utility_1 import  *
from utilities_dir.utility_2 import  a_specific_function

结构

- django_project
    - main
        - views.py
    - utilities_dir
        - __init__.py
        - utility_1.py
        - utility_2.py

这篇关于如何在Django项目中使用外部python实用工具文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆