in my django project structure, i want all my django apps in a separate Apps Folder, but when i include it in settings.py it raises an error,
raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'TestApp'. Check that 'Apps.TestApp.apps.TestappConfig.name' is correct.
INSTALLED_APPS = [
...
'Apps.TestApp'
]
But
when i only include TestApp, i raises no module named 'TestApp' Error.
INSTALLED_APPS = [
...
'TestApp'
]
If you are using django version < or = 2 then you should register your app like
INSTALLED_APPS = [
...
'testapp.apps.TestappConfig'
]
the app name should not be in 'UPPER' case otherwise you will get errors.
if you are using django > or = 3 then you can register your app with it's original name too.
You are registering your app in 'Title' style which is not permitted.
You could do the following in your settings.py file:
INSTALLED_APPS = [
... # other necessary apps here
# include your local apps you are creating for your project here
'testapp.apps.app_name', # assuming app_name is one of your apps
'testapp.apps.another_app',
'testapp.apps.third_custom_app'
]
Then in each of your app folders (where your models.py, views.py, urls.py, etc. are) include an apps.py file that follows the following pattern:
from django.apps import AppConfig
class AppNameConfig(AppConfig): # note the formatting of this class name
default_auto_field = "django.db.models.BigAutoField"
name = "apps.app_name" # apps is the name of the directory where all your apps are located.
# app_name is the name of the individual directory within your apps directory where this apps.py file is saved
Related
I am using Django 3.2, and Django REST Framework. Main project name is ECOM and api is app. Inside api there are multiple apps like category, migrations, order, payment, product, user. Now I want to inform ecom.settings about installed api. HOW I should do it ?
settings.py of ECOM :
INSTALLED_APPS = [
#other basic install
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'api',
'api.category',
]
but getting error.
My category apps.py file looks like
class CategoryConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'category'
Try to change name in your category apps.py like this
class CategoryConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.category'
AppConfig.name is a full python path to the application.
in your case your app category is inside another app called api so try to change your apps.py file to this :
class CategoryConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.category' // full path to your app category
the source from django documentation : https://docs.djangoproject.com/en/4.0/ref/applications/#django.apps.AppConfig.name
You will have to make api directory a package by creating a __init__.py file in it. Then instead of adding just api in INSTALLED_APPS list just add api.category.
Like this -
INSTALLED_APPS = [
#other basic install
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'api.category',
]
my project structure is like below:
./apps
./apps/bizusers
./apps/bizusers/admin.py
./apps/bizusers/apps.py
./apps/bizusers/models.py
./apps/bizusers/serializers.py
./apps/bizusers/tests.py
./apps/bizusers/views.py
./apps/bizusers/__init__.py
./apps/__init__.py
./config
./config/asgi.py
./config/settings.py
./config/urls.py
./config/wsgi.py
./config/__init__.py
./manage.py
./requirements.txt
My custom user model is in
./apps/bizusers/models.py
I have this in settings:
INSTALLED_APPS = [
'apps', ]
I added AUTH_USER_MODEL = "bizusers.User" in settings.py
I have tried to edit ./apps/__init__.py and ./apps/bizusers/apps.py but I cannot get it to work.
I have tried these solutions below:
having models directory and AUTH_USER_MODEL
Model in sub-directory via app_label?
'MyAppConfig' must supply a name attribute
Thanks.
You can have your apps as subpackages of another package, but then you do need to add your app to the INSTALLED_APPS list, i.e. instead of adding apps you need to be adding the subpackages of it in the list:
INSTALLED_APPS = [
...
'apps.bizusers',
]
Next your setting for AUTH_USER_MODEL is correct:
AUTH_USER_MODEL = "bizusers.User"
This is my Django settings file:
my modules:
apps/users/apps.py
from django.apps import AppConfig
class UserConfig(AppConfig):
name = 'apps.users'
Error I get:
I scanned Django 1.11.6 doc, could not find INSTALLED_APPS change , and I don't know how to resolve this issue?
The reason of write apps.users.apps.UserConfig is that's the direction of the class that contain the name 'apps.users' , to avoid that you can add apps.users inside INSTALLED_APPS or rename name=users, put user inside INSTALLED_APPS, and add this line after BASE_DIR var sys.path.insert(0, os.path.join(BASE_DIR, 'apps')), with this in the future you wont need to include in the imports from apps.user... just from user...
I'm trying to deploy django project to webfaction and stuck with the problem that the server does not see my templates folder
PROJECT_DIR = os.path.dirname((os.path.dirname((os.path.dirname(__file__)))))
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'templates'),
)
python path in httpd.conf :
python-path=/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
And i have exception:
Exception Type: TemplateDoesNotExist
Exception Value: index.html
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/wadadaaa/webapps/promo_site/templates/index.html (File does not exist)
any ideas how to fix it?
When deploying to WebFaction I add the project's parent directory to the python_path:
python-path=/home/wadadaaa/webapps/promo_site:/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
If you're using Django 1.5.x, a common way I "map" directory paths like TEMPLATE_DIRS, STATIC_ROOT, etc, is to use a function to compute those. This is especially useful if you work on more than one machine, or in a group, where the path to these files is going to vary per-developer:
# settings.py
import os
def map_path(directory_name):
return os.path.join(os.path.dirname(__file__),
'../' + directory_name).replace('\\', '/')
...
TEMPLATE_DIRS = (
map_path('templates'),
)
This is a convenient way to map your templates directory, given a project structure of:
my_app/
my_app/
__init__.py
settings.py
...
a_module/
__init__.py
models.py
templates/
layouts/
base.html
index.html
...
I have the following structure (Django 1.4):
containing_dir/
myproject/
myapp1/
myapp2/
myapp3/
myproject, myapp1, myapp2, and myapp3 all have init.py, so they're all modules.
In manage.py (under containing_dir) I have os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
in myproject.settings i define:
[..]
ROOT_URLCONF = 'myproject.urls'
INSTALLED_APPS = (
[..]
'myproject.myapp1',
'myproject.myapp2',
'myproject.myapp3',
)
[..]
In myapp1.urls.py I define:
urlpatterns = patterns('myapp1',
url(r'^agent/$', 'views.agent', name='agent')
)
and I try to import it in myproject.urls I try to import myapp1 urls like this:
(r'^myapp1/', include('myproject.myapp1.urls'))
but whenever I try lo load localhost:8000/myapp1/agent I get
Exception Value: No module named myapp1
I think thrown from withing myapp1.urls
Any help? thanks
You must have a
__init__.py
file inside your "myproject" directory. When you say:
(r'^myapp1/', include('myproject.myapp1.urls'))
you are saying "myproject" (as well as myapp1) is a python packege.
In myproject.settings make following changes :
INSTALLED_APPS = (
[..]
'myapp1',
'myapp2',
'myapp3',
)
Try:
urlpatterns = [
...
url(r'^app_name/', include('app_name.urls', namespace='project_name'))
...
]
Does ROOT_URLCONF need to point to myproject.urls?
If you place your apps inside of myproject you need to use the proper view prefix.
urlpatterns = patterns('myproject.myapp1',
...
To solve this issue just select "myproject" directory in PyCharm and set this as a source root.
Your project don't know from which root it has to search for given app.
It fixed the issue for me.
Thank you.
Recently, In new versions of Django introduces path(route, view, kwargs=None, name=None) instead of old url() regular expression pattern.
You must have __init__.py file in app folders to recognize it as a package by django project i.e myproject
Django project i.e. myproject urls.py file must be updated to include examples like:
path('', include('django_app.urls'))
path('url_extension/', include('django_another_app.urls'))
Above example includes two apps urls in it. One is without adding any extension to path in url and another is with extension to path in current url.
Also, Do not forget to add django apps in INSTALLED_APPS in settings.py file to recognise it as app by django project something like this.
ROOT_URLCONF = 'myproject.urls'
INSTALLED_APPS = [
...
django_app,
django_another_app
...
]
For more information look at documentation.