65๐
You can add your apps
folder to your python path by inserting the following in your settings.py
:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
Then you can use all the apps in this folder just in the same way as they were in your project root!
25๐
You can do this very easily, but you need to change the settings.py
to look like this:
INSTALLED_APPS = (
'apps.app1',
'apps.app2',
# ...
)
And your urls.py
to look like this:
urlpatterns = patterns('',
(r'^app1/',include('apps.app1')),
(r'^app2/',include('apps.app2')),
)
.. and modify any import
s to point to the app location
- [Django]-Running a specific test case in Django when your app has a tests directory
- [Django]-Django: Create fixtures without specifying a primary key?
- [Django]-Django Rest framework, how to include '__all__' fields and a related field in ModelSerializer ?
21๐
How about you utilize the BASE_DIR
variable already present in the settings.py
.
Just add the following:
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
Hope this helps.
- [Django]-Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?
- [Django]-The number of GET/POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS
- [Django]-Cron and virtualenv
7๐
As a slight variant to Berhard Vallant
โs or Anshuman
โs answers, here is an alternative snippet to place in settings.py
import os
import sys # Insert this line
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Insert the two lines below
APPS_DIR = os.path.join(BASE_DIR, '<your_project_dir_name>/apps/')
sys.path.insert(0, APPS_DIR)
Doing it in this way has the added benefit that your template directories are cleaner as they will look like below. Without the APPS_DIR
variable, there will be a lot of repitition of <your_project_dir_name>/apps/
within the DIRS
list of the TEMPLATES
list.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(APPS_DIR, '<app_name>/templates/<app_name>'),
os.path.join(APPS_DIR, '<app_name>/templates/<app_name>'),
...
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
You can list the apps within the INSTALLED_APPS
list as normal with either the short-form name given in apps.py
or by using the long-form syntax of appname.apps.AppnameConfig
replacing appname
with your appโs name.
- [Django]-How to filter empty or NULL names in a QuerySet?
- [Django]-Homepage login form Django
- [Django]-How to customize activate_url on django-allauth?
4๐
Itโs easy and simple you need to add to settings.py
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
and edit your app config for example
old app config:
class MyappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
to new app config:
class MyappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
label='myapp'
name = 'apps.myapp'
than installed apps example:
INSTALLED_APPS = [
...
'apps.myapp.apps.MyappConfig'
...
]
I think itโs very usefull and helpfull.Good luck ๐
- [Django]-Logging in Django and gunicorn
- [Django]-Copy a database column into another in Django
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
1๐
If youโre using virtualenv/virtualenvwrapper (which is a bit dated but still valid), you can use the included add2virtualenv
command to augment your python path:
mkdir apps
cd apps
pwd
[/path/to/apps/dir]
Copy that path to clipboard, then:
add2virtualenv /path/to/apps/dir
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-Django Admin app or roll my own?
- [Django]-Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4
1๐
Just add __init__.py
(4 underscores in total) in your apps folder. Now you can just do
urlpatterns = [
path('polls/',include('apps.polls.urls')),
path('admin/', admin.site.urls)
]
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Generating file to download with Django
- [Django]-Linking to the django admin site
0๐
In my case, my project folder structure is the following:
/project/
/apps/
/app1/
/app2/
/src/
/settings.py
...
So Iโve solved it with these two lines on my settings.py
:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(BASE_DIR, '../apps'))
No need to alter urls.py
.
- [Django]-Django 1.4 timezone.now() vs datetime.datetime.now()
- [Django]-Django 1.11 TypeError context must be a dict rather than Context
- [Django]-Django admin TabularInline โ is there a good way of adding a custom html column?
0๐
The following worked for me.
This is the folder structure:
/project1
/apps
/myapp
inside apps.py under /myapp:
class MyAppConfig(AppConfig):
name = "apps.myapp"
In the settings.py:
INSTALLED_APPS = [
...
"apps.myapp.apps.MyAppConfig",
]
- [Django]-Django get objects not referenced by foreign key
- [Django]-How to redirect with post data (Django)
- [Django]-Django, Models & Forms: replace "This field is required" message