I want to setup app app2 outside project directory. If you check following screenshot, I have created djnago project project, inside this project, created app app1 and created app 'app2' outside project. Now how to include app2 and urls.py in project settings and urls.py.
INSTALLED_APPS = [
.
.
'../common_functions/app2',
]
I tried above but getting errors: **TypeError: the 'package' argument is required to perform a relative import for ../common_functions/app2
Related
It seems like to be best practice (or at least one common way) to create a Django 3 based fullstack project which uses project specific Django apps (instead of standalone Django apps) with a structure like this (refer to e.g. here):
fullstack_project/
frontend/
... # frontend stuff goes into here
backend/ # Django project name
apps/
app1/
apps.py
logic.py
models.py
...
app2/
apps.py
logic.py
models.py
...
wsgi.py
...
manage.py
The apps (here: app1) are integrated in the most basic form (no ruls, views, etc.) via fullstack_project/backend/apps/app1/apps.py
class App1Config(AppConfig):
name = 'backend.apps.app1'
and fullstack_project/backend/settings.py
INSTALLED_APPS = [
...
'backend.apps.app1.apps.App1Config',
'backend.apps.app2.apps.App2Config',
]
Using logic of one app (e.g. app2/logic.py) in another app (e.g. app1/logic.py) works just fine (e.g. via from backend.apps.app2.logic import ... in app1/logic.py).
However if I try to access the models of app1 in the logic of app2 I get django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.. I could implement a database wrapper for an app to allow cross-app database access. However ass the apps are cross-project anyway I'd like to avoid that and allow access more easily. How can I fix this issue?
It is possible that there will be some code in __init__.py which is trying to access DB even before the app is registered and ready. so if you have any code in __init__.py comment it or make sure it will access database once the app is ready.
In my django project named website I have two apps app1 & app2 . I want to keep app2 accessible only through my local machine. I dont want app2 to be publicly accessible online through my website. I have already pushed my entire website to heroku. So how can I delete app2 from heroku website and avoid pushing app2 to heroku in future.
I have tried following things:
Added app2/ to .gitignore. So future changes are avoided. But current version of app2 still exist on heroku website.
I deleted app2 on local machine and tried to push the changes, but I got error in static collection saying no app named app2 exists.
I would also like to avoid having Django's default admin functionality available through my website. I would like to keep it local too.
Thanks
I would keep both apps under version control, then control the differences through settings. For example, if you only have DEBUG set locally:
settings.py
if settings.DEBUG:
INSTALLED_APPS += ['app2']
urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns = [
include('app2/', include('app2.urls')),
] + urlpatterns
This is just an example, without knowing more about your project, it is hard to show exactly how to do it. But with this example, app2 would only be loaded and have routes available when the DEBUG setting was True.
I have multiple settings_x.py
I want to disable app1 which is inside INSTALLED_APPS of one settings_1.py
I tried to comment the app, but the app is still avalaible from urls.py.
how can I do this? I dont want to delete, I just want to turn off the app.
in urls.py of project ,import settings of project.
from django.conf import settings
in urls.py of project, add this code.
if 'app1' in settings.INSTALLED_APPS:
urlpatterns += url(r'^app1/', include('app1.urls'), name='app1'),
if you add this code, system will check if the app is added to INSTALLED_APPS list in settings.py or not. If it does not exist, system will not add its urls.py configurations and also the urls of app will not work.
Apps are available even though they are not specified in settings.py. I can't imagine your case when you want to make the app unavailable from urls.py.
A solution to your problem can be using a version control system like git and using multiple branches. Delete the app in the branch where you don't need the app.
I am testing django system and I started a project and a app for it. I have a problem with i18n system.
I have used _() function in app's view.py.
I put the 'django.middleware.locale.LocaleMiddleware' line in the MIDDLEWARE_CLASSES in project's settings.py file.
I run django-admin.py makemessages -l tr in app folder.
I run django-admin.py compilemessages in app folder.
When I run the service it didn't work. After I search in here and some other places I see the way how django looking these message files. First it looks app's folder, after project's folder. There are another places as well.
I moved my message files in project's folder and it worked. I moved the locale folder in app's folder again and it didn't work again.
I need to understand why. I want every translations, message files in releated app.
Regards,
I found the problem, I forgot to add the app name to INSTALLED_APPS settings. When I added, it works.
I tested it for myself and found it working.
Are you sure you use it in code in right way?
app/views.py:
from django.utils.translation import ugettext as _
from django.contrib import messages
...some code here...
if saved_changes:
messages.success(request, _('Item \'%s\' was saved.') % item.name)
I want to be able to import things from applications in my project without referring to my project name.
My folder structure is like so; I have a project called djangoproject1, and inside I have a folder called apps, and then I have my individual apps in that folder.
djangoproject1, apps, and all my applications have an empty "__init__.py" file in them.
In my settings.py file I have the following code:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
I've tried adding my apps to INSTALLED_APPS several ways:
'djangoproject1.apps.app1',
'djangoproject1.apps.app2',
or
'apps.app1',
'apps.app2',
or
'app1',
'app2',
but nothing works. Whenever I try to do:
from app1 import *
I got an unresolved import error. I'm using the latest versions of eclipse and django
Ok, so I got it to work by adding the apps folder to the PYTHONPATH through eclipse under Project Properties. Is this eclipse only though? I'm not sure how this will work when I try and deploy the site. What do you guys think?
The statement sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps")) looks OK to me. Following this you only need to add app1 to INSTALLED_APPS and everything should work. But apparently they are not working.
Try the following: 1. Print your sys.path and verify that your project's app directory is in the list. 2. Double check that your have an __init__.py inside your apps folder.
Update
OK. Now can you fire up Django shell and try importing again? If it fails, please post the stack trace here.
You must include apps directory to PYTHONPATH. Be sure that in apps directory don't have __init__.py, because it become a package instead of "simple directory". Than, include in settings.py
app1, app2, app3
Use
from app1 import *