I' m deploying my first django app on heroku. After deploy the heroku doesn't launch my app. After checking heroku logs --tail
I have got an error:
: ModuleNotFoundError: No module named 'tabele.wsgi'
My procfile looks like:
web: gunicorn tabele.wsgi:application --log-file -
I tries also:
web: gunicorn tabele:app
web: gunicorn tabele.wsgi --log-file -
I'm beginner and I don't understand if (in my case 'tabele') should be folder containing manage.py or the different one?
In my project folders including manage.py and another one including settings.py and wsgi.py has the same name "tabele"
Could you explain me what is what in procfile file for better understanding?
Any idea what I'm doing wrong?
djangoherokuapp
|-- tabele/
| |--- __init_-.py
| |--- settings.py
| |--- urls.py
| |--- wsgi.py
|----- manage.py
|------Procfile ⬅⬅⬅
|------requirements.txt
|----- app/
| |--- admin.py
| |--- apps.py
| |--- __init__.py
| |--- models.py
| |--- tests.py
| |--- views.py
Add a Procfile in the project root directory to define process types
and explicitly declare what command should be executed to start your
app.
Open the Procfile and add the line below:
web: gunicorn tabele.wsgi --log-file -
--log-file - means "log to stdout". The --log-file flag lets you
set a path to a log file, and - means "stdout" (in this context).
Or try with:
web: gunicorn tabele.wsgi
Related
I have several custom settings I'd like to define in my Django app. Up to this point, I've put them in a constants.py file in each individual app.
myapp/constants.py
HOURS_PER_EVENT = 4
MAX_MEMBERS_PER_EVENTS = 150
MAX_EVENTS_PER_YEAR = 10
...
It just occurred to me I may be able to put these in settings.py and after a quick test everything seems to work fine. Is this allowed or is settings.py reserved for core django settings defined here? If it's not allowed, is there a better place to put these.
Yes it is allowed to extend your application settings, and it is really simple to do it. All that you need to do is a simple manipulation from your current project settings.py to a module settings.
Your file structure is probably like the following:
mysite/
|-- mysite/
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
What you need to do is to change it to something like this:
mysite/
|-- mysite/
| |-- __init__.py
| |-- settings/
| | |-- __init__.py
| | |-- base.py <-- (this is your old settings.py)
| | +-- constants.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
This is a pattern described in this tutorial.
I have a Django project which requires Celery tasks to run for two of its apps. I was wondering if there was a way to have a single celery.py file which runs both of them.
File tree:
src
|--api
| |-- init.py
| |-- otherFiles
| |-- celery
| |-- celery.py
|
|--xyz
| |-- init.py
| |-- otherFiles
| |-- celery
| |-- celery.py
|
|--otherApps
If I were to run to Celery apps manually, I would run:
source venv/bin/activate
cd src
celery worker -A api
And in another terminal:
source venv/bin/activate
cd src
celery worker -A xyz
The two celery workers require access to the same databases.
This is how my celery file looks like:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
app = Celery("api")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()
The other replaces app = Celery('api') with app = Celery('xyz').
Is this the correct way to handle this? Can I create a single script and run both tasks from it?
If not, what's the best way to daemonize this?
Production server is running Ubuntu 18.04 and python 3.6.9, so I'm planning on using systemd although suggestions are welcome too.
I user Django 1.10 with uWSGI and nginx on ubuntu 16.04 and deploy my app with ansible. My project have not default structure, but quite common ( thank Two scoopce for this :).
I use split dev and production settings and config folder instead 'name' project folder. It's looks like this:
|-- config
| |-- __init__.py
| |-- settings
| | |-- __init__.py
| | |-- base.py
| | `-- dev.py
| |-- urls.py
| |-- wsgi_dev.py
| `-- wsgi_production.py
|-- manage.py
`-- requirements.txt
My production.py genarate from ansible with security encrypt and locate in config/settings.
With this config i get "no python application found check your startup logs". Uwsgi don't see my application.
( {{ }} it's jinja2 syntax for ansible )
/etc/uwsgi/sites/{{ project_name }}
[uwsgi]
chdir = {{ django_root }}
home = /home/{{ project_user }}/venvs/{{ project_name }}
module = config.wsgi_production:application
master = true
processes = 5
socket = /run/uwsgi/{{ project_name }}.sock
chown-socket = {{ project_user }}:www-data
chmod-socket = 660
vacuum = true
After several weeks i can find problem in my wsgi.py. It common solution use os.environ['ENV'] for DJANGO_SETTINGS_MODULE, but with deffrent users and permissions its dosen't work.
If you use in your wsgi.py file something like this:
os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings." + os.environ["ENV"]
And have problem with no python application found - split your wsgi file. I can catch that os.environ["ENV"] return empty string. I add it for my all user, use source and etc. But uwsgi in emperior mode don't see it.
You sould use wsgi_dev.py and wsgi_production.py where you can write somethink like this os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production". It's not so elegant but solve this problems fine.
For use splitting wsgi you can write something like this in wsgi.py
import os
from django.core.wsgi import get_wsgi_application
if os.environ.get('DEV') is True:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"config.settings.production")
application = get_wsgi_application()
I'm with a problem with heroku.
Above, follow the static files configuration of my project:
PROJECT_ROOT = os.path.abspath(os.path.join(__file__, os.pardir))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'core/static/')
STATIC_URL = '/static/'
Here is the structure of my project
myapp
|--- core
|--- static
|--- client
|--- product
|--- myapp
|--- settings.py
|--- urls.py
I have run first on local:
$ python manage.py makemigrations
$ python manage.py migrate
And on server:
$ heroku run python manage.py makemigrations
$ heroku run python manage.py migrate
After, I do:
$ git add .
$ git commit -m "I do some commit here."
$ git push heroku master
And click here to see the site.
After, as I didn't see my template, I did:
python manage.py collectstatic --noinput
So, I click here again. But did no work yet.
In my template base base.html, I begin it with {% load static %}.
Does have someone could help me?
Thank you very much.
since Django 1.4 (I think) django create a folder for my project when I start a project. Django add a folder for any application I created (with python manage.py startapp) at the same level of my project folder.
Project_name
|---project_name_dir/
|---application_dir/
`---manage.py
I really like the following folder structure:
Project_name
|---project_name_dir/
| |---application_dir/
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | `-- views.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| |---templates/
| | `---application_dir/
| `---static/
| |---css/
| |---font/
| |---img/
| `---js/
|---deployment/
|---documentation/
|---config/
`---manage.py
Because I have a folder with all my django files (project_name_dir/) and other directories for non django files.
So why Django put application at the same level of my project folder?
In Django, the position of the application directory is not considered. Django only uses the name of the application.
Thus, the position of the application is basically a matter of convenience of the programmer.
This is also the reason why two apps should not have the same name: even if they are imported in INSTALLED_APPS as
('app.app1', 'app1')
Django only concerns with the last part after the dot, i.e. app1.
So, in the end, you can use the directory structure you want, as long as the apps' names don't collide and you point to the app on INSTALLED_APPS. Because of this, if there isn't any special reason, you should put them on the project's root, like Django does.