Django - apps.py not recognized - django

Django version 1.11.7
Trying to execute signals.py for a post_save execution my code works if i put it in my models.py file but i want to separate my signals from my models in app_1 but seems that even my apps.py isn't executed.
The project structure
my_project
├──docs
├──src
│ ├── __init__.py
│ ├── apps
│ │   ├── app_1
│ │ │ ├── __init__.py
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── signals.py
│ │ │ ├── ...
│ │ │ └── models.py
│ │   ├── ...
│ │   └── app_x
│ ├── fabfile.py
│ ├── manage.py
│ ├── media
│ │   └── files
│ ├── requirements.txt
│ ├── settings.py
│ ├── static
│ │   ├── file_1.html
│ │   ├── ...
│ │   └── file_x.html
│ ├── templates
│ │   ├── app_1
│ │   ├── ....
│ │   └── app_x
│ ├── urls.py
│ └── wsgi.py
apps.py
from django.apps import AppConfig
class AppOneConfig(AppConfig):
name = 'app_1'
def ready(self):
print("test")
from . import signals
__ init __.py
default_app_config = "apps.app_1.apps.AppOneConfig"
And here is the error i have
django.core.exceptions.ImproperlyConfigured: Cannot import 'app_1'. Check that 'apps.app_1.apps.AppOneConfig.name' is correct.
settings.py, INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.app_1'
]

apps.py:
from django.apps import AppConfig
class AppOneConfig(AppConfig):
⬇⬇⬇
name = 'apps.app_1'
def ready(self):
print("test")
from . import signals
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.app_1.apps.AppOneConfig' ⬅⬅⬅⬅⬅
]

Related

ModuleNotFoundError: No module named 'django.config' when running test in Django 4.1

I have a django (v4.1) project with no tests defined. Before I started writing tests, I ran python manage.py test, expecting to get something like:
Found 0 test(s).
System check identified no issues (0 silenced).
....
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
But instead I got this:
Found 1 test(s).
System check identified no issues (0 silenced).
E
======================================================================
ERROR: django.config (unittest.loader._FailedTest.django.config)
----------------------------------------------------------------------
ImportError: Failed to import test module: django.config
Traceback (most recent call last):
File "/usr/local/lib/python3.11/unittest/loader.py", line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/unittest/loader.py", line 350, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'django.config'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
I've checked this question but it seems more to do with a migration problem from Django 2 to 3 and how to run tests.
Clarifications:
When I say there are no tests defined, I mean that there are the auto-generated test.py files created by manage.py startapp in three applications, but I've commented out the standard import statement # from django.test import TestCase.
I tried searching for any instances of django.config using grep -r "django.config" . from the repo directory (which is above django's BASE_DIRECTORY and there are no instances of that in any of the files.
I have named the "project" directory (the one containing settings) "config" so wondered if there was come import confusion stemming from that. Did a search for all occurrences of "config":
$ grep -r "config" .
./templates/base.html: using the 'builtins' key of OPTIONS in settings (in ./config/base/templates.py).
./config/admin.py: default_site = 'config.admin.CustomAdminSite'
./config/asgi.py:ASGI config for server project.
./config/settings/base/databases.py:from config.utilities import dprint
./config/settings/base/databases.py: "NAME": str(BASE_DIR / "config/db.sqlite3"),
./config/settings/base/core.py:ROOT_URLCONF = "config.urls"
./config/settings/base/core.py:WSGI_APPLICATION = "config.wsgi.application"
./config/settings/base/authentication.py:# authall configuration settings
./config/settings/base/applications.py: # NB see ./authentication.py for social account config of allauth
./config/settings/base/applications.py: "config.admin.CustomAdminConfig", # replaces django.contrib.admin
./config/settings/__init__.py:from config.utilities import dprint
./config/settings/__init__.py:SECRETS_DIR = BASE_DIR / 'config/secrets/'
./config/wsgi.py:WSGI config for server project.
./apps/accounts/admin.py:from config.utilities import dprint
./apps/core/views.py:from config.utilities import dprint
./manage.py: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
Project structure beneath django's BASE_DIRECTORY:
├── __init__.py
├── apps
│ ├── accounts
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── forms.py
│ │ ├── managers.py
│ │ ├── models.py
│ │ ├── signals.py
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── models.py
│ │ ├── urls.py
│ │ └── views.py
│ └── recipes
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── config
│ ├── __init__.py
│ ├── admin.py
│ ├── asgi.py
│ ├── db.sqlite3
│ ├── secrets
│ │ ├── development.secret
│ │ └── secret.template
│ ├── settings
│ │ ├── __init__.py
│ │ ├── base
│ │ └── development
│ ├── tests.py
│ ├── urls.py
│ ├── utilities.py
│ └── wsgi.py
├── manage.py
├── static
│ ├── css
│ │ └── styles.css
│ └── js
└── templates
├── allauth
│ └── account
├── base.html
├── base_with_nav.html
├── core
│ ├── index.html
│ └── item1.html
├── navbar.html
└── navbar_rhs_items.html

celery 'function' object has no attribute 'delay' how to get return value after delay?

Hi I have a problem in getting async function return value.
this is my views.py code
Directory is djangoweb/views.py
def preprocess_log2(request,uuid):
data = get_object_or_404(Adata, uuid=uuid)
if request.method == "POST":
result = log2_transform_task.delay(data.raw_path)
test = result.get()
data.mat_data = test
data.save()
return redirect("raptorlite:home")
return render(request, 'raptorlite/detail.html',{"data":data})
this is my task code
Directory is djangoweb/bin/log2_transform.py
#shared_task
def log2_transform_task(raw:str, out_dir = None) -> str:
return out_name
sorry for not uploading my source code but I checked result it work perfectly
and it is my settings.py
CELERY_BROKER_URL = 'redis://redis:6379/0'
result_extended = True
accept_content = ['application/json']
result_serializer = 'json'
task_serializer = 'json'
timezone = 'Asia/Seoul'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
enter image description here
when I run code if not use result.get() it works well but I want to get return value so I followed celery docs however after using get to return value suddenly that error happended..
please help me..
I followed celery docs and I read about similar error case in here but I checked that function name do not duplicate
my apps file tree is
├── djangoweb
│ ├── admin.py
│ ├── apps.py
│ ├── bin
│ │ ├── add_annotation.py
│ │ ├── adjust_qqnorm.py
│ │ ├── entrez_id2symbol.py
│ │ ├── generate_id.py
│ │ ├── gpl_probe2other.py
│ │ ├── __init__.py
│ │ ├── log2_transform.py
│ │ ├── outlier_fitting.py
│ │ ├── __pycache__
│ │ ├── renovate_entrez_gene.py
│ │ ├── rescale_1to2.py
│ │ ├── run_rescale_1to2.py
│ │ └── stats_onco_lite.py
│ ├── forms.py
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py

RuntimeError: Model doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am writing an app in Django and I'm trying to do some unit testing
but I can't seem to find why the test is failing
that is the test page:
import re
from django.test import TestCase
from django.urls import reverse
from . import models
class BasicTests(TestCase):
def test_firstname(self):
print('test11')
acc = models.Accounts()
acc.first_name = 'Moran'
self.assertTrue(len(acc.id) <= 9, 'Check name is less than 50 digits long')
self.assertFalse(len(acc.id) > 50, 'Check name is less than 50 digits long')
the error i get is :
RuntimeError: Model class DoggieSitter.accounts.models.Accounts
doesn't declare an explicit app_label and isn't in an application in
INSTALLED_APPS
thats my installed app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'
]
TLDR;: Try changing line 4 above to an explicit import such as from DoggieSitter.accounts import models
I had this problem whenever running tests where the tests.py had a relative import such from .models import ModelName. After searching for an hour or so, I stumbled on this answer to exactly the tutorial I was following.
In my case, I was trying from .models import Recipe. My project structure is as below, so I changed to from apps.recipes.models import Recipe and the test now runs fine. It's a shame because I'd prefer to keep using relative imports.
src
├── __init__.py
├── apps
│ ├── accounts
│ ├── core
│ └── recipes
│ │ ├── models.py
│ │ ├── ... etc
├── config
│ ├── __init__.py
│ ├── admin.py
│ ├── asgi.py
│ ├── db.sqlite3
│ ├── secrets
│ ├── settings
│ │ ├── __init__.py
│ │ ├── base
│ │ └── development
│ ├── tests.py
│ ├── urls.py
│ ├── utilities.py
│ └── wsgi.py
├── manage.py
├── static
└── templates
PS - another even more explicit way that also seems to work is:
from django.apps import apps
Recipe = apps.get_model(app_label='recipes', model_name='Recipe')
... but I think I prefer the simpler explicit import statement.

Getting a circular import error in django urls

I'm getting the following circular import error in the project level urls.py file:
ImproperlyConfigured: The included URLconf 'pres_value.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
To be clear, pres_value is the project level and present is the app level. So apparently the error is occurring in the project level urls file. The app has been registered in the settings.py file.
Any help on what's wrong here is appreciated.
Project level pres_value/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('present/', include('present.urls')),
]
App level present/urls.py:
from django.urls import path
from .views import HomePageView, PresentValueView, AboutPageView, ResultView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
path('about/', AboutPageView.as_view(), name='about'),
path('pv/', PresentValueView.as_view(), name='present_value'),
path('result/', ResultView.as_view(), name='result'),
]
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'present',
]
Directory structure:
├── Pipfile
├── Pipfile.lock
├── db.sqlite3
├── manage.py
├── pres_value
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── present
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ ├── models.py
│ ├── templates
│ │ └── present
│ │ ├── about.html
│ │ ├── home.html
│ │ ├── pv.html
│ │ └── result.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
└── templates
├── base.html
├── footer.html
├── header.html
└── nav.html
views.py
from django.shortcuts import reverse
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView
from .forms import PresentForm
from .models import PresentValue
# Create your views here.
class HomePageView(TemplateView):
template_name = 'present/home.html'
class PresentValueView(CreateView):
model = PresentValue
template_name = 'present/pv.html'
# fields = ['first', 'second']
success_url = reverse('result')
class AboutPageView(TemplateView):
template_name = 'present/about.html' ## this is where I show an error
class ResultView(TemplateView):
template_name = 'present/result.html'
The circular import is caused by reverse().
Solution: Use reverse_lazy.
from django.core.urlresolvers import reverse_lazy
class PresentValueView(CreateView):
model = PresentValue
template_name = 'present/pv.html'
# fields = ['first', 'second']
success_url = reverse_lazy('result')

`FileNotFoundError`: when issue `python manage.py collectstatic`

When I tried to issue commands to collect staticfiles
In [41]: ! python manage.py collectstatic
It throw FileNotFoundError:
FileNotFoundError: [Errno 2] No such file or directory:
'/Users/me/Desktop/Django/forum/static'
Nevertheless, there exist file '/Users/me/Desktop/Django/forum/static'
In [44]: ! tree -L 2
.
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── forums
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py
14 directories, 15 files
The setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#my apps
"forums"
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), #notice the comma
)
You should provide the STATIC_ROOT in your settings, by default Django looks for a directory named static in your root project, so that 's why your get this /Users/me/Desktop/Django/forum/static'``FileNotFoundError the folder is not present.
You have that directory static next to your settings, it should not be there. By the way also your templates should not be there.
Move them back to your root project
├── db.sqlite3
├── forum
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
| |---static # remove this
│ ├── templates # remove this here
│ ├── urls.py
│ └── wsgi.py
|---static -- # GOOD
|---templates # GOOD
You need to have static directory outside from Django App. This directory and your Django app should be separate.
Then, if you specify :
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), #notice the comma
)
It means your static directory is in your base directory. Or you have only forum, forums, ll_forum ...
If you want absolutly keep your static directory in your Django App, make this directory in the BASE_DIR (move from initial position to your base project) and write something like this in settings.py file :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
Finally, you should have something like this :
.
├── db.sqlite3
├── forum
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── static
│ ├── templates
│ ├── urls.py
│ └── wsgi.py
├── forums
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── ll_forum
│ ├── bin
│ ├── include
│ ├── lib
│ ├── pip-selfcheck.json
│ ├── pyvenv.cfg
│ └── share
└── manage.py
│
└── static
│
└── templates