When creating an app with python manage.py startapp myapp, it automatically creates an apps.py file.
from django.apps import AppConfig
class MyappConfig(AppConfig):
name = 'myapp'
When I removed it, everything seems to work as before (at least my tests all passed). Is it a bad practice to remove these kind of files from the apps? Should we keep them to avoid side effects?
The recommended approach in Django is to use the app config in your INSTALLED_APPS:
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
...
]
If you do this, then the apps.py file is required.
However, if you do not customize the app config at all, then it is possible to remove the apps.py if you use the app name in INSTALLED_APPS instead:
INSTALLED_APPS = [
'myapp',
...
]
Related
i would like to set a specific order of the models list of an app in the django dashboard. my models look like this:
i've tried several tutorial like this one and looked other people solution inside SO like this questions How to use custom AdminSite class? Set ordering of Apps and models in Django admin dashboard Reorder app and models in Django admin
but none of these works, they either use a third party library or they don't specify where to put the pieces of code. This one seem to be the most promising but i tried to put the piece of code,
class WebsiteAdminSite(admin.AdminSite):
def get_app_list(self, request):
"""
Return a sorted list of all the installed apps that have been
registered in this site.
"""
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
}
app_dict = self._build_app_dict(request)
# a.sort(key=lambda x: b.index(x[0]))
# Sort the apps alphabetically.
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: ordering[x['name']])
return app_list
inside the admin.py of my app but it doesn't work.
i can't provide any error since none is given, it simply doesn't work. i think it should since in django 4.1 there should be the possibility to override AdminSite but i can't make it work.
Where should i put the class in the code above? is there anything else i should add in the admin.py? like the following lines of code i've found in this answerhttps://stackoverflow.com/questions/58256151/set-ordering-of-apps-and-models-in-django-admin-dashboard
mysite = WebsiteAdminSite()
admin.site = mysite
sites.site = mysite
what do these lines of code do? when i add them after the class i broke the code and an error appear raised by django.contrib.admin.sites.catch_all_view
UPDATE
following an answer i'm adding my project folder structure and the changes i've made.
my project folder look like this now:
-base
-base
admin.py
apps.py
asgi.py
settings.py
urls.py
wsgi.py
-website (the app folder)
-migrations
-templates
admin.py
apps.py
models.py
tests.py
urls.py
views.py
-dbsqlite3
manage.py
so i've added the admin.py inside my project folder with the class i am interested in, added the class
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
inside my apps.py in the app folder (website) and modified the INSTALLED_APPS in settings.py that now look like the following:
INSTALLED_APPS = [
'base.apps.WebsiteAdminConfig',
#'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
]
my apps.py inside the app folder (website) look like this:
from django.apps import AppConfig
class WebsiteConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'website'
and the apps.py inside my project folder (base) :
from django.contrib.admin.apps import AdminConfig
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
now i have another problem, this line
app['models'].sort(key=lambda x: ordering[x['name']])
give me a key error 'Groups'
fixed this it should be functioning.
UPDATE2
to manage the error i needed to add "Groups" and "Users" inside the ordering dictionary inside the class WebsiteAdminSite:
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
"Groups":11,
"Users":12,
}
It would be helpful for you to post the structure of your project. Something similar to this.
- mysite
- mysite
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
Where should i put the class in the code above?
According to Django documentation, it should be stored in your projects in mysite/admin.py. Like so:
- mysite
- mysite
- admin.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
According to Django documentation, you will also need to create a AdminConfig class in mysite/apps.py.
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'mysite.admin.MyAdminSite'
And replace 'django.contrib.admin' with said config class in the INSTALLED_APPS variable in mysite/settings.py.
INSTALLED_APPS = [
...
'mysite.apps.MyAdminConfig', # replaces 'django.contrib.admin'
...
]
Reading the documentation is important, don't rush over it.
is there anything else i should add in the admin.py?
Depends on what else you want to customize. You shouldn't need to add anything else. Check the documentation if you want to change more things, or take a look at the source code on github.
If I try to add blog.apps.BlogConfig string to INSTALLED_APPS (to define the blog app), local server won't start giving me an error. Photos attached below:
I am expecting for the separate section to appear on site.
Was doing it by tutorial of the Youtuber Corey Schafer:
https://www.youtube.com/watch?v=qDwdMDQ8oX4&list=PL-osiE80TeTtoQCKZ03T
It's so simple.
Just put your application name in INSTALLED_APPS list.
Like, if you have application name "blog", then just put ...
INSTALLED_APPS = [
'blog',
...
]
That's it !
Still, if you need to put name of your application in INSTALLED_APPS list as "blog.apps.BlogConfig", then just go to apps.py in your application, and put ...
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'
Thanks !
I'm working with Django 3.2 and trying to configure correctly the AppsConfig subclass of apps.py in order to avoid duplicate apps names when initzialing the project.
The context is the following. I have in my INSTALLED_APPS two apps whose names are equal although their paths not:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
To avoid the error shown below (and according to the documentation), I need to create an apps.py file subclassing AppConfig in at least one of the apps called myapp. I've decided to create that file in the second one, second_app.myapp.
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: myapp
The app.py in second_app.myapp module looks like as follows:
from django.apps import AppConfig
class MySecondAppConfig(AppConfig):
name = "second_app.myapp"
label = "my_second_app"
And in the __init__.py I've added:
default_app_config = 'second_app.myapp.apps.MySecondAppConfig'
My admin.py looks like:
from django.contrib import admin
from .models import MyModel
class MySecondAppAdminModel(admin.ModelAdmin):
list_display = ('attr_1', 'attr_2')
admin.site.register(MyModel, MySecondAppAdminModel)
When I start the project all works OK and I can use that model information, views called from second_app.myapp also work OK.
The problem comes when I access to the admin site (http://localhost:8000/admin), where only appears the first_app.myapp admin form instead of both.
Can you help me? Thanks in advance.
instead of this:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
Just try this way only:
INSTALLED_APPS = [
...
'first_app',
'second_app'
]
You got that error because here first_app.myapp and second_app.myapp myapp is duplicate. you should not use like this in both the apps.
OR
INSTALLED_APPS = [
...
'first_app.apps.MyFirstAppConfig',
'second_app.apps.MySecondAppConfig'
]
I want to declare default_app_config variable for my app, but I have an error:
django.core.exceptions.ImproperlyConfigured: Cannot import 'accounts'.
Check that 'apps.accounts.apps.AccountsConfig.name' is correct.
All my apps is located in directory apps:
project_name/
apps/
app1/
app2/
....
accounts/
I am using my apps like this:
INSTALLED_APPS = [
# Local Apps
'apps.category',
'apps.products',
'apps.cart',
'apps.orders',
'apps.accounts',
'apps.attributes',
'apps.pages',
'apps.compare',
'apps.filters',
'apps.brands'
]
And now when I want to set default_app_config for my app, I get the error above.
__init__.py in app folder:
default_app_config = 'apps.accounts.apps.AccountsConfig'
apps.py in app folder:
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = 'accounts'
verbose_name = 'Пользователи'
Why I get the error?
AppConfig name is full python path to the application. So try...
name = 'apps.accounts'
Let's assume I have the following project:
myproject/
myproject/
__init__.py
settings.py
urls.py
wsgi.py
templates/
base.html
app1/
__init__.py
admin.py
models.py
urls.py
views.py
templates/
base.html
index.html
I want to have a base template, which all other apps will use, in myproject/template/base.html.
Then, I want to have the app templates in app/templates.
One option would be writting in myproject/settings.py:
TEMPLATE_DIRS = (
"/dir/to/myproject/myproject/templates",
"/dir/to/myproject/app1/templates",
)
But, is this the best way to do it?
Django supports this automatically, via the app_directories loader that is installed by default. See the documentation.
Maybe you can do the following:
#settings.py
import os
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
os.path.join(PROJECT_PATH, 'app1/templates'),
)
This will save you from editing each and every absolute path in setting file in case you happen to move your project.