Django runserver won't run because of bootstrap error message - python

When I try to run my server in Django it is not able to find my app track that I just created:
ModuleNotFoundError: No module named 'track'
I got the same message for the app I was working on previously. I also get error messages relating to bootstrap, the last one being this:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'
I started noticing this once I removed the bootstrap configurations from another app I was working on. I just downloaded bootstrap and know very little about it, especially how it could stop a Django server from running and not recognizing my app.
Any ideas???

If you already created an app which is 'track' by this command: python manage.py startapp track, don't forget to add it to the installed apps on the settings.py.
example:
INSTALLED_APPS = [
'app1',
'track',
'app2'
]
Also don't forget the add "," after each app, last one doesn't matter.

Related

Deplying Django to Heroku - DisallowedHost Error despite Heroku URL in DJANGO_ALLOWED_HOSTS

I've been at this for hours and need help. Initially I thought it was because I had mistakenly done something small that caused it, so I deleted the app, and created everything - virtual env, heroku app, django projects/apps - fresh.
I get the same error.
I made a cookiecutter django app and deployed it to heroku. Everything goes smoothly until it's time to actually use the site.
When I run heroku open, I get the DisallowedHosts error:
DisallowedHost at /
Invalid HTTP_HOST header: 'MY-NEW-APP.herokuapp.com'. You may need to add 'MY-NEW-APP.herokuapp.com' to ALLOWED_HOSTS.
heroku config shows that DJANGO_ALLOWED_HOSTS=MY-NEW-APP.herokuapp.com. I don't overwrite it in my settings file.
I have import django_heroku and django_heroku.settings(locals()) in my settings file.
DJANGO_SETTINGS_MODULE is correctly set to that file.
What's more, I get a warning about DEBUG=True, when DEBUG=False in my settings file and in the heroku environment.
What am I missing? Are hyphens a bad thing? Should I be using herokuapp.com instead of the full URL?
Solved my own question. I just had to:
heroku config:set DJANGO_ALLOWED_HOSTS=.MY-NEW-APP.COM,MY-NEW-APP.herokuapp.com,.herokuapp.com

Django on Heroku: relation does not exist

I built a Django 1.9 project locally with sqlite3 as my default database. I have an application named Download which defines the DownloadedSongs table in models.py:
models.py
from __future__ import unicode_literals
from django.db import models
class DownloadedSongs(models.Model):
song_name = models.CharField(max_length = 255)
song_artist = models.CharField(max_length = 255)
def __str__(self):
return self.song_name + ' - ' + self.song_artist
Now, in order to deploy my local project to Heroku, I added the following lines at the bottom of my settings.py file:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
My application has a form with a couple of text fields, and on submitting that form, the data gets inserted into the DownloadedSongs table. Now, when I deployed my project on Heroku and tried submitting this form, I got the following error:
Exception Type: ProgrammingError at /download/
Exception Value: relation "Download_downloadedsongs" does not exist
LINE 1: INSERT INTO "Download_downloadedsongs" ("song_name", "song_a...
This is how my requirements.txt file looks like:
beautifulsoup4==4.4.1
cssselect==0.9.1
dj-database-url==0.4.1
dj-static==0.0.6
Django==1.9
django-toolbelt==0.0.1
gunicorn==19.6.0
lxml==3.6.0
psycopg2==2.6.1
requests==2.10.0
static3==0.7.0
Also, I did try to run the following commands as well:
heroku run python manage.py makemigrations
heroku run python manage.py migrate
However, the issue still persists. What seems to be wrong here?
Make sure your local migration folder and content is under git version control.
If not, add, commit & push them as follows (assuming you have a migrations folder under <myapp>, and your git remote is called 'heroku'):
git add <myapp>/migrations/*
git commit -m "Fix Heroku deployment"
git push heroku
Wait until the push is successful and you get the local prompt back.
Then log in to heroku and there execute migrate.
To do this in one execution environment, do not launch these as individual heroku commands, but launch a bash shell and execute both commands in there: (do not type the '~$', this represents the Heroku prompt)
heroku run bash
~$ ./manage.py migrate
~$ exit
You must not run makemigrations via heroku run. You must run it locally, and commit the result to git. Then you can deploy that code and run those generated migrations via heroku run python manage.py migrate.
The reason is that heroku run spins up a new dyno each time, with a new filesystem, so any migrations generated in the first command are lost by the time the second command runs. But in any case, migrations are part of your code, and must be in version control.
As Heroku's dynos don't have a filesystem that persists across deploys, a file-based database like SQLite3 isn't going to be suitable. It's a great DB for development/quick prototypes, though. https://stackoverflow.com/a/31395988/784648
So between deploys your entire SQLite database is going to be wiped, you should move onto a dedicated database when you deploy to heroku I think. I know heroku has a free tier for postgres databases which I'd recommend if you just want to test deployment to heroku.
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
this worked for me.
I know this is old, but I had this issue and found this thread useful.
To sum up, the error can also appear when executing the migrations (which is supposed to create the needed relations in the DB), because recent versions of Django check your urls.py before doing the migrations. In my case - and in many others' it seems, loading urls.py meant loading the views, and some views were class-based and had an attribute defined through get_object_or_404:
class CustomView(ParentCustomView):
phase = get_object_or_404(Phase, code='C')
This is what was evaluated before the migrations actually ran, and caused the error. I fixed it by turning my view's attribute as a property:
class CustomView(ParentCustomView):
#property
def phase(self):
return get_object_or_404(Phase, code='C')
You'll know quite easily if this is the problem you are encountering, as the Traceback will point you towards the problematic view.
Also this problem might not appear in development because you have migrated before creating the view.

Heroku looking for the wrong wsgi file. Django

I'm on Django, but I'm not sure if it matters. Anyway, I get an Application Error, and when I check the logs I see the error:
ImportError: No module named redlibros.wsgi
And it is fine cause the wsgi file is not name "redlibros.wsgi", I don't even know where it gets that name. The module is named
WSGI_APPLICATION = 'letrasclub.wsgi.application' # on my settings
web: gunicorn letrasclub.wsgi --log-file - # on my procfile
and on my folders it looks like this:
LetrasClub
letrasclub
wsgi.py
templates
static
...
Any idea where to find the error?
EDIT
Ok, some extra info: I have a different repo, with a different Heroku remote. I copied that repo, changed the app, created a new Heroku remote and then pushed to the new one.
So, if I write
git remote -v
heroku https://git.heroku.com/letrasclub2.git (fetch)
heroku https://git.heroku.com/letrasclub2.git (push)
origin https://github.com/Alejoss/LetrasClub2.0.git (fetch)
origin https://github.com/Alejoss/LetrasClub2.0.git (push)
Looks fine, if I go to the old app location and write the same command I get the old remote, the one that is related to the "redlibros.wsgi" app, perfect.
Now, why when I try to push the new app to the new heroku remote, I get the error that means Heroku is looking for the Old wsgi file, I changed the wsgi name, I changed the Procfile, I changed the wsgi file declaration on the settings, what am I missing?
You look at wrong project.
You look at https://github.com/Alejoss/redlibros (I guess it's Your project), not at https://github.com/Alejoss/LetrasClub2.0

Django Setup Error: View is not callable

I got a basic Django setup working on my webhost, then I copied it down to my local machine. Both my local and my remote are using the same version of Django, and the same database (Postgres) with the same exact settings.py.
The thing is, when I run manage.py runserver on my local, and then browse to localhost, I get:
ViewDoesNotExist at /
Could not import myapp.main. View is not callable.
The view does exist though, and works perfectly fine on my remote. When I look at the PYTHONPATH in the debug output it includes the base directory of my django setup, and the "main" app is in the INSTALLED_APPS in my settings.py.
Basically everything is setup the same as on my remote (except that the remote is using Apache and I'm using runserver), at least as far as I can tell, but they are behaving differently. Does anyone have any clue what could be wrong?
EDIT:
It turns out I'm an idiot, and one of my files (urls.py) on the server never made it in to the commit. As a result, my local file wasn't what I thought it was, and I failed to realize the problem. Once I updated urls.py everything worked.
put 'myapp' in your settings.py
INSTALLED_APPS = (
'myapp',
)
urls.py
url(r'^$', 'myapp.views.main', name='main'),
check that in views.py, a function named 'main' should be there

Completely removing a django app that has dot notation

I want to remove the django.contrib.comments app from a project I'm working on. I tried:
$ python manage.py sqlclear django.contrib.comments
on the shell but got:
Error: App with label django.contrib.comments could not be found. Are
you sure your INSTALLED_APPS setting is correct?
I have double checked my INSTALLED_APPS setting and indeed django.contrib.comments is present.
Any suggestions on how to get around this?
The app_name is actually just the last element of the file path. So comments should work.
You could run
python manage.py reset app_name
Then remove app_name from your installed apps.

Categories