Overriding the default admin site - can't find myproject - django

I am trying to override the default admin site. I followed the Django Docs here:
https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#overriding-the-default-admin-site
I did everything as stated there so the files look like beneath:
admin/admin.py:
from django.contrib import admin
from django.utils.translation import gettext as _, gettext_lazy
class CustomAdminSite(admin.AdminSite):
# Text to put in each page's <h1>.
site_header = gettext_lazy('TEST administration')
admin/apps.py
from django.contrib.admin.apps import AdminConfig
class CustomAdminConfig(AdminConfig):
default_site = 'admin.CustomAdminSite'
core/urls.py
urlpatterns = [
path(r'testadmin/', admin.site.urls),
]
core/settings.py
INSTALLED_APPS = [
'admin.apps.CustomAdminConfig',
]
Now I am getting a ImportError:
ImportError: Module "admin" does not define a "CustomAdminSite" attribute/class
I think this is because django is looking for a admin module that lives inside of the virtual environment with the CustomAdminSite class. This is not the case, the admin folder with the necessary python files lives in the base path of myproject. When I state this in my settings like: myproject.admin.apps.CustomAdminConfig I am getting a ModuleNotFOundError:
ModuleNotFoundError: No module named 'myproject'
I guess the same thing as stated above. Django is looking for a module named myproject inside of the virtual environment.
Is there a way to change this in the settings or a way to bypass this some other way?

As #IainShelvington said, the name of the module is likely admin.admin, since admin is the name of the app, and then in that app you have a "submodule" named admin, so this should be:
from django.contrib.admin.apps import AdminConfig
class CustomAdminConfig(AdminConfig):
default_site = 'admin.admin.CustomAdminSite'

Related

Pycharm unresolved references with Django

This is my 5th day learning Django. I've got a Django project working in a virtual environment using Pycharm. The problem I have is, Pycharm can't reference certain imports when they are actually working in Django without any issues. I've selected the right interpretor for the project but Pycharm cannot connect the imports with the .py files in the Django project. The selected interpretor does have Django for the virtual environment but it still doesn't work. Below are the screenshots:
import works in Django but Pycharm cannot resolve reference with the correct interpretor
You need to mark the project's root source. You can do this by right-clicking:
Or by going to PyCharm settings > Project: Project Structure > and make sure to mark the project root directory (the folder that holds your project's apps) as a "Sources" directory.
Sidenote: I noticed you're not using directory-level urls.py. I would suggest doing that instead of trying to import URLs from other apps into the project-level urls.py.
What this means is, each app - articles, accounts, etc. - would have a file named "urls.py".
In your project-level urls.py, you do this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
path('accounts/', include('accounts.urls')),
]
In your app-level urls.py, you do this:
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name="hey"),
]

Django / app import problem from submodule

I'm writing my own Django app, and trying to import submodule from my core library like that:
INSTALLED_APPS = [
'django.contrib.admin',
...
'core.login',
]
And interpreter gives me:
django.core.exceptions.ImproperlyConfigured:
Cannot import 'login'.
Check that 'core.login.apps.CustomloginConfig.name' is correct.
So login.apps looks like that
from django.apps import AppConfig
class CustomloginConfig(AppConfig):
name = 'login'
Are there any rules how I can edit this files to start Django properly?
apps.py file needs to be as so
from django.apps import AppConfig
class CustomloginConfig(AppConfig):
name = 'core.login'
This is where you tell django that I have registered this app 'core.login' and where to find it.
If login folder is in a core folder, then the above should work.
I think theres a lot of django apps out there that have organized things this way.
One being Kiwi but Im sure theres many others.

Dotted name in AppConfig

With django 1.7 they added support for configuration classes instead of the magic string from previous versions.
My project has several applications into a folder named apps which is added to the PYTHON_PATH.
After adding a simple AppConfig derived class I'm running into many import errors and I want to rule out silly mistakes.
Suppose this structure:
project_root/
my_project/
apps/
my_app/
another_app/
Would it be correct to have this config?:
# my_app/apps.py
class MyAppConfig(AppConfig):
name = 'apps.my_app'
# my_app/__init__.py
default_app_config='apps.my_app.apps.MyAppConfig'
# settings.py
INSTALLED_APPS = (
...
'apps.myapp.apps.MyAppConfig'
...
)
Curently the project fails when trying to import models (or tasks or any other module) issuing:
from apps.my_app.tasks import AwesomeTask
ImportError: no module named my_app.tasks
I solved a similar issue by renaming the apps folder. It was somehow conflicting with Django internals system.
I hope this helps someone.

ImportError: No module named App1.views (django 1.5). Any idea?

I have 2 Apps in myproject. When I try to import App1 views from App2 views, I get ImportError: No module named App1.views. for e.g (from myproject.App1.views import *). And I added both apps under INSTALLED_APPS in settings.py
In Django 1.5, you need to omit the project name from the imports. The release notes of Django 1.4 explains this change in the applicatiion structure and import paths. So the correct import statement is:
from App1.views import *

Django admin: Can't add app to admin

I have installed Django on my host (I use their install version 1.1.1), all is working fine.
I have created some apps and they are registered in my settings.py (I can verify this works when i visit the site, the app shows up).
In the folder of this app i have created admin.py with the following content:
from progmaticnet.page.models import Page, PageContent
from django.contrib import admin
class PageContentInline( admin.StackedInline ):
model = PageContent
extra = 1
max_num = 1
class PageAdmin( admin.ModelAdmin ):
inlines = [ PageContentInline ]
class Media:
#js = ('/site_media/js/tinymce/jscripts/tiny_mce/tiny_mce.js', '/site_media/js/tinymce/textarea.js')
admin.site.register( Page, PageAdmin )
But my app doesn't show up in the admin... It is said in the documentation that you'll need to restart the server, although i can't do that (runs on apache), i have a dispatch.fcgi with this content:
#!/usr/bin/python
import sys, os
project_sys="/home/progmati/public_html"
#add a custom python path
sys.path.insert(0, project_sys)
# set the DJANGO_SETTINGS_MODULE environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'progmaticnet.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded")
I have killed the process and started it anew but with no avail...
Does anybody know what to do about it?
Thanks
Why is the js declaration commented out in your Media class? That looks like it'll be an invalid class definition as a result (class definitions can't be entirely empty). Try uncommenting it, or adding pass below the commented out line, like this:
class Media:
#js = ('/site_media/js/tinymce/jscripts/tiny_mce/tiny_mce.js', '/site_media/js/tinymce/textarea.js')
pass