未加星标

Django project, apps structure and folders

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In this blog post I’ll talk about Django folders structure inside a project.

After developing a few projects with Django 1.11 and Django 2.0, I’ve stumbled with somewhat an issue that’s been bothering me. When you create projects and apps in Django, as the tutorial shows you, apps will be created inside the main project folder, at the same level of the project’s settings folder (usually it’s the folder that has the same name as your project).

So basically, what you end up with is a container folder and inside has the project config folder, and many other folders with the app names.

So as a result, you get different ordering in the folders for every new project, as folders will usually order by name inside your IDE, i.e PyCharm.

So, for instance, if your app is called ‘apples’ and your project is called ‘mysite’, then the order will be:

- apples
- mysite

Let’s also assume you’re using docker, so you probably have a docker folder, now we have:

- apples
- docker
- mysite

But if you create an app called ‘oranges’, now your project folder is:

- apples
- docker
- mysite
- oranges

This is unconfortable because you never know which is the project’s config folder and which ones are the different app folders, so you keep opening the wrong folders all the time!! This is awful, as programers, we tend strive for efficiency!!

So my strategy for now is to bundle all apps inside an apps folder, so my project will always look like:

- apps
- docker
- mysite

And there you have it, nicely ordered apps inside your project!!

To include them in your settings, you just have to remember to include the namespace

'apps.your_app_name'

, so if your app is called apples

INSTALLED_APPS += ['apps.apples']

Also, when importing classes in different files, just use the namespace. So imagine, I need to import the Apple class defined in my models inside my apples app, would do something like so:

from apps.apples.models import Apple

If you’ve read until here, it means you REALLY CARE about your project folder structure, so here’s a handy script I’ve created to start apps. It’s just a wrapper from the django startapp command, but it takes care of the apps folder, etc.

#!/usr/bin/env bash
if [ "$1" == "-h" ]; then
echo "This script will create an app inside the apps folder"
echo "To use type the following line:"
echo "bash start-app.sh app_name"
echo "Replace app_name with the actual name for your app"
elif [ "$1" != "" ]; then
if [ ! -d "apps" ]; then
mkdir apps
touch apps/__init__.py
fi
mkdir apps/$1
if [ -f /.dockerenv ]; then
python manage.py startapp $1 apps/$1
else
docker-compose run django python manage.py startapp $1 apps/$1
fi
echo "Success! The app $1 has been aded, don't forget to add INSTALLED_APPS += ['apps.$1'] in your project's settings.py"
else
echo "Error! One parameter is expected: app_name"
fi

I call the file “start-app.sh”, so to use it just type

bash start-app.sh

Happy coding!!

Also published on Medium .

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Django project, apps structure and folders
本站链接:https://www.codesec.net/view/620769.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(12)