CSS of a DJANGO application are not rendered on heroku - django

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.

Related

heroku procfile problem : ModuleNotFoundError: No module named 'myApp.wsgi'

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

Django 2.1.7: Makemigrations command result: "No change detected in app"

(I am aware that a number of Django users have had the same issue.
I have looked at a number of solutions online but none has worked for me so far.)
I have set up my apps.py, settings.py and models.py files as explained in Django official tutorial (please see the 3 files below).
When I enter in the terminal:
$ python3 manage.py makemigrations munichliving_app
It returns:
No changes detected in app 'munichliving_app'
(file settings.py) in INSTALLED_APP --> I added and tested both one at a time:
'munichliving_app' and
'munichliving_app.apps.MunichLivingConfig'
apps.py file: https://pastebin.com/raw/qaYy1x44
setting.py file: https://pastebin.com/raw/cSsbfPsx
models.py: https://pastebin.com/raw/U0QeM16k
Django official tutorial states that I should see something along the lines of:
Migrations for 'polls':
polls/migrations/0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
Thank you.
Your app is munichliving (the module that contains models.py), but you have munichliving_app in your INSTALLED_APPS setting. The munichlivin_app is the project folder (the one that contains settings.py). It doesn't normally contain models so you shouldn't usually have to add it to INSTALLED_APPS or make migrations for it.
Replace 'munichliving_app' with 'munichliving' in your INSTALLED_APPS.
Next, I would remove your apps.py because it doesn't appear to be used. If you do keep it, then change it to name='munichliving', then use'munichliving.apps.MunichLivingConfig'inINSTALLED_APPS`.
Finally, create migrations with
./manage.py makemigrations munichliving
Try this:
python manage.py migrate --fake appname
Or delete the migration folder in your app, go to the database and delete the file in django_migrations table, then migrate again:
python manage.py makemigrations
python manage.py migrate

how process static file when deploying django using docker and aws eb

I used Django, Docker and AWS Elastic Beanstalk to employ my website. I followed the instruction of https://github.com/glynjackson/django-docker-template.
I met problem when I try to load static file, the browser try to visit mysite.com/static/css/xx.css to get the css and javascipt file which is different with what I run it locally.
In settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
I also used:
# !/bin/sh
cd /var/projects/mysite && python manage.py migrate --noinput && python manage.py collectstatic --noinput
supervisord -n -c /etc/supervisor/supervisord.conf
Currently, I write additional views and urls for javascript and css file. So browser can get these file from url. But how to do it correctly?

Install and Deploy Django app on Heroku

I often forget steps and wish there was a quick instructional guide on deploying a django project on Heroku.
How to Install and Deploy a Django app on Heroku?
I have posted a step-by-steps answer for steps that have worked for me.
You will get:
Django app both on heroku and your computer.
Postgres databases on both machines
git/bitbucket
authentication: login, logout, register, forgot pass, email authentication only (optional & default)
static files working on both machines
Bootstrap 3.0.3 included
South Migrations (instructions)
Requirements
heroku account
github/bitbucket account
mac with OSX (tested on 10.9)
UPDATE:
To do an installation the quick way, check out the other answer.
Folder structure
PROJECT_WRAPPER - it will hold everything, including PS
DJANGO_PROJECT - it will hold the code
DJANGO_APP - the main app will have that name
Anywhere you see those, replace with your real names!!!
Virtual Env
If you don’t have virtualenv, you need to get it. It will allow you to have separate installations of software for each project:
pip install virtualenv
then we create our project:
cd ~
mkdir PROJECT_WRAPPER && cd PROJECT_WRAPPER
virtualenv venv
now you have a dedicated folder that will contain independent installations and version of python, django, etc.
We activate and and start working on project the following way:
source venv/bin/activate
Postrges app
Just before we continue, we will install postgres.app. Grab it from:
http://postgresapp.com/
Install.
We will now hook up our environment with it:
PATH=/Applications/Postgres.app/Contents/MacOS/bin/:$PATH
Requirements.txt
Now we will need to install the following things:
Python, Django - no explanations required
South - Migrations of database (dev version of Django does not require it)
django-toolbelt - required by heroku and includes everything required for heroku
psycopg - postgres database
simplejson, mixpanel - these are optional, you could skip if you didn't like
So to create the requirements.txt file, we will get it ready from my git repository:
clone https://raw2.github.com/mgpepe/django-heroku-15/master/requirements.txt -o requirements.txt
Now with one command we will install everything from our requirements.txt:
pip install -r requirements.txt
Great, now we can verify that we have django with:
python -c "import django; print(django.get_version())"
Start a Django Project
Let’s start the project with this line and don’t forget the dot in the end:
django-admin.py startproject DJANGO_PROJECT .
Now if you type ls you should see a folder with your project name that contains your Django project.
To see if it all works run:
python manage.py runserver
DATABASE
Run the Postgres app.
Create a database with (I used my osx username):
createdb YOUR_DATABASE_NAME --owner=YOUR_OSX_USERNAME
change the DATABASES to look like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'YOUR_DATABASE_NAME',
'USER': 'YOUR_OSX_USERNAME',
'PASSWORD': 'YOUR_DATABASE_PASSWORD', #might be empty string ''
'HOST': '127.0.0.1',
# 'PORT': '5432',
}
}
And also let’s hook up the South migrations. Your INSTALLED_APPS should look like that:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
)
Change the SECRET_KEY variable to something else than what it is.
Now if everything was fine you should be able to create the first tables with:
python manage.py syncdb
FIRST APP
Now make your first app in your project
python manage.py startapp DJANGO_APP
in the file: ~/PROJECT_WRAPPER/DJANGO_PROJECT/settings.py
add the DJANGO_APP app to the list in the variable INSTALLED_APPS. Should look like that:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'DJANGO_APP',
)
TEMPLATES
in settings file add the line:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
In order for the templates to be well organized and working, we will copy base.html in one folder and the rest of templates in the app itself:
cd ~/PROJECT_WRAPPER/
mkdir templates
curl https://raw2.github.com/mgpepe/django-heroku-15/master/templates/base.html -o base.html
Now the rest of templates:
cd ~/PROJECT_WRAPPER/DJANGO_APP/
mkdir templates && cd templates
mkdir DJANGO_APP
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/changepass.html -o changepass.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/forgot_pass.html -o forgot_pass.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/home.html -o home.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/login.html -o login.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/logout.html -o logout.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/registration.html -o registration.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/splash.html -o splash.html
AUTH SYSTEM WITH EMAIL
Since it has been lately fashionable to use email instead of username, we will do that too.
*NOTE: if you decide not to use it, you can skip this step BUT you have to edit the views and templates to use username instead of email. *
In settings add the following line:
AUTHENTICATION_BACKENDS = (DJANGO_PROJECT.backends.EmailAuthBackend’,)
then copy the file backends.py in our project directory:
cd ~/PROJECT_WRAPPER/DJANGO_PROJECT/
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/backends.py -o backends.py
HEROKU LOCALLY
You can simulate heroku working on your computer with Foreman. Let’s create the simplest configuration file:
cd ~/PROJECT_WRAPPER
echo "web: gunicorn DJANGO_PROJECT.wsgi" > Procfile
foreman start
Now that you see it working without errors stop it with CTRL+C
in settings all the way at the bottom add:
# HEROKU
###########################
# Parse database configuration from $DATABASE_URL
if os.environ.has_key('DATABASE_URL'):
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
In your DJANGO_PROJECT/wsgi.py file and add the following to bottom:
from dj_static import Cling
application = Cling(get_wsgi_application())
STATIC FILES
Ideally you would server static files from Amazon or something like that. But for simple sites you could use Django. Setting it up requires you to append this in settings file:
# HEROKU STATIC ASSETS CONFIGURATION
################################
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
and put all static files in a specific folder. First go to your project folder with something like:
cd ~/PROJECT_WRAPPER/DJANGO_PROJECT/
and now you can just copy/paste the rest:
mkdir static && cd static
mkdir css && cd css
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/css/bootstrap.min.css -o bootstrap.min.css
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/css/styles.css -o styles.css
cd ..
mkdir js && cd js
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/js/bootstrap.min.js -o bootstrap.min.js
cd ..
mkdir img && cd img
In this last folder, you will put all images you need.
URL SETTINGS AND VIEWS
In urls.py copy these lines right before ‘example’:
url(r'^$', "pmfmain.views.splash", name="splash"),
url(r'^login$', "pmfmain.views.login_view", name="login"),
url(r'^signup$', "pmfmain.views.register", name="signup"),
url(r'^forgot$', "pmfmain.views.forgot_pass", name="forgotmypass"),
url(r'^logout$', "pmfmain.views.logout_user", name="logout"),
url(r'^dashboard$', "pmfmain.views.home", name="home”),
then copy views.py from my github repo to your DJANGO_PROJECT folder:
cd ~/PROJECT_WRAPPER/DJANGO_APP/
rm views.py
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/views.py -o views.py
Do a find & replace replacing DjMainApp with your real DJANGO_APP name throughout the whole views.py
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/forms.py -o forms.py
GIT
Some files need not be in git, so let’s set the config for this:
echo -e "venv\n*.pyc\n*.log\n*.pot\nstaticfiles" > .gitignore
and now lets commit:
git init
git add .
git commit -m ‘initial commit of django app’
Create a repository in git, then copy the git url (the one that ends in .git). Then:
git remote add origin THE_URL
git pull origin master
BITBUCKET ALTERNATIVE
If you don’t want to pay for github and you want your repository private, you can use bitbucket.
Login to your account
Create a new repository
Click add existing project
git remote add origin https://USERNAME#bitbucket.org/USERNAME/REPOSITORY_NAME.git
MULTIPLE HEROKU ACCOUNTS & KEYS
Even if you never have to have multiple heroku accounts, it is an easy way to setup and use it even for one account. So on we go:
cd ~
heroku plugins:install git://github.com/ddollar/heroku-accounts.git
the add a heroku account with:
heroku accounts:add personal
Enter your Heroku credentials.
Email:YOUR_HEROKU_EMAIL
Password: YOUR_HEROKU_PASSWORD
It says it in the console, and you have to do it:
Add the following to your ~/.ssh/config
Host heroku.personal
HostName heroku.com
IdentityFile /PATH/TO/PRIVATE/KEY
IdentitiesOnly yes
Go to your project folder with something like:
cd ~/PROJECT_WRAPPER
and then set the new account as:
heroku accounts:set personal
To create a new ssh KEY:
ssh-keygen -t rsa
When asked for name, write the full path and name as shown. then type your password or leave blank
Then add the keys both to your OSX and heroku:
heroku keys:add ~/.ssh/YOUR_KEY_NAME.pub
ssh-add ~/.ssh/YOUR_KEY_NAME
DEPLOYING HEROKU FILES
Now that you have keys in order, you should be able to do
heroku apps
and see that there are no apps. To add your first app:
heroku apps:create YOUR_APP_NAME
And now to upload to the server:
git push heroku master
now go to YOUR_APP_NAME.herokuapp.com to see your site!
DOMAIN SETUP
remains to be explained if anybody wants, let me know
NOTES
In-depth documentation at:
https://docs.djangoproject.com/en/1.6/intro/tutorial01/
https://devcenter.heroku.com/articles/getting-started-with-django
In my other answer, the process is well described, but takes time. So I have made a ready installation that is good to go in less than 15minutes.
https://github.com/mgpepe/django-heroku-15
If you'd prefer the full explanation with the long path see the answer below.
THESE ARE THE ERRORS WHICH I FIND WHILE WORKING ON DJANGO FOR 2 YEARS [ENJOY]
Dyno should be added/seen in heroku->resources and if it is not added in resources of Heroku then it means there is a problem in the
"Procfile"
web: gunicorn [django_project].wsgi --log-file -
"django_project" above is your project name , change it to your project name
Remember to do changes in the settings.py file
DEBUG=True
ALLOWED_HOSTS = ["your-app.herokuapp.com","127.0.0.1"]
add this in settings.py file
#->this should be in the middleware
'whitenoise.middleware.WhiteNoiseMiddleware',
#->this at the bottom
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
[ First do "pip install django-heroku" ]
place this at the top of settings.py file:
import django_heroku
place this at the bottom of "settings.py" file:
django_heroku.settings(locals())
Heroku only works with postgres, remember this
[ go to https://www.elephantsql.com/ ]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "5432",
}
}
Make Sure database in the background, if not running, start "postgres" in the services.msc.
[ This is in taskmanager->Services->postgresql->right click->start]
python manage.py migrate
go to "your app" in heroku and go to "settings" and select "Add buildpack" in the settings and select "python"
####################### Important ##############################
==> Create a new Git repository Initialize a git repository in a new or
existing directory
cd my-project/
git init
heroku git:remote -a iris-django-ml
==> Deploy your application
Commit your code to the repository and deploy it to Heroku using Git.
git add .
git commit -am "make it better"
git push heroku master
"run this command in your directory"
heroku login
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
restart again with deleting your git file of your working directory, delete heroku project (settings -> bottom)

Django migrate app using south - duplicate initial

I'm trying to migrate django apps, and i have problem with one. When i run
python manage.py migrate --list |grep -v "*"
i get output
dbtemplates
( ) 0001_initial
( ) 0002_auto__del_unique_template_name
( ) 0003_initial
I jut downloaded dbtemplates package, and in downloaded folder migrations are 3 files:
__init__.py
0001_initial.py
0002_auto__del_unique_template_name.py
So in my django project 0003 shouldn't be here i think. Should i remove 0003, mark as fake? If i need to remove 0003 migration, where can i find this package?
python manage.py shell
[1]: import dbtemplates
[2]: dbtemplates
This show the path package