0

I am developing my first django website (based on tango with django tutorial), and faced problem with serving static files when Debug = False. (When I had Debug = True everything worked well)

my folders structure is as following:

|___401.shtml
|___500.shtml
|___requirements.txt
|___400.shtml
|___populate_rango.py
|___manage.py
|___templates
| |___rango
| | |___index.html
| | |___category.html
| | |___cats.html
| | |___category_list.html
| | |___add_category.html
| | |___base.html
| | |___register.html
| | |___restricted.html
| | |___about.html
| | |___login.html
| | |___add_page.html
| |___registration
| | |___logout.html
| | |___registration_complete.html
| | |___registration_form.html
| | |___login.html
|___media
| |___photo.png
| |___.htaccess
| |___profile_images
| | |___Screenshot_from_2016-07-18_145936.png
| | |___Screenshot_from_2016-07-21_112910.png
|___cgi-bin
| |___.htaccess
|___rango
| |___admin.py
| |___migrations
| | |___0003_category_slug.py
| | |_____init__.pyc
| | |___0003_category_slug.pyc
| | |___0001_initial.pyc
| | |___0001_initial.py
| | |___0004_userprofile.pyc
| | |___0002_auto_20160804_1857.py
| | |___0002_auto_20160804_1857.pyc
| | |___0004_userprofile.py
| | |_____init__.py
| |___models.py
| |___tests.py
| |_____init__.pyc
| |___models.pyc
| |___templatetags
| | |_____init__.pyc
| | |___rango_extras.pyc
| | |___rango_extras.py
| | |_____init__.py
| |___forms.py
| |___views.pyc
| |___urls.py
| |___admin.pyc
| |___views.py
| |___urls.pyc
| |___forms.pyc
| |_____init__.py
|___tango_with_django_project
| |___settings.pyc
| |___wsgi.pyc
| |_____init__.pyc
| |___urls.py
| |___settings.py
| |___urls.pyc
| |___wsgi.py
| |_____init__.py
|___static
| |___images
| | |___photo.png
| | |___rango.png
| |___.htaccess
| |___js
| | |___rango-jquery.js
| | |___jquery.js
| | |___rango-ajax.js
|___db.sqlite3
|___test.py
|___404.shtml
|___403.shtml

i added STATIC_ROOT in settings.py path as follows:

import os
import uwsgi
from uwsgidecorators import timer
from django.utils import autoreload

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

(...)

ROOT_URLCONF = 'tango_with_django_project.urls'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    STATIC_ROOT,
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')

and now i want to run from ssh terminal

python manage.py collectstatic

however i am facing an error

Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'tango_with_django_project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named uwsgi

i read post and schwärzl answer here:

ImportError: Could not import settings

and modified wsgi.py file to:

import os
import sys
sys.path.append('home/mrklap/domains/tredo.net/public_html/tango_with_django_project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango_with_django_project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

but still when i am in (or out) virtualenv in ssh and run:

>>> import sys
>>> for path in sys.path: print path

...

i am not getting added path

/home/mrklap/venv/lib/python2.7
/home/mrklap/venv/lib/python2.7/plat-x86_64-linux-gnu
/home/mrklap/venv/lib/python2.7/lib-tk
/home/mrklap/venv/lib/python2.7/lib-old
/home/mrklap/venv/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/home/mrklap/venv/local/lib/python2.7/site-packages
/home/mrklap/venv/lib/python2.7/site-packages
>>> 

why i can't see that path in listing below?

Community
  • 1
  • 1
Reven
  • 776
  • 2
  • 8
  • 23
  • Looking at the error 'No module named uwsgi', it looks as if you haven't installed `uwsgi`. – Alasdair Aug 17 '16 at 10:58
  • Note that the `collectstatic` command collects files in `STATICFILES_DIRS`, and copies them to `STATIC_ROOT` where you serve them from. Therefore, it doesn't make sense to include `STATIC_ROOT` in the `STATICFILES_DIRS` setting. – Alasdair Aug 17 '16 at 10:59
  • Alasdair, thanks for help but i see (when i am in virtual env) that uwsgi is installed) `(venv) [mrklap@s34:: ~/domains/tredo.net/‌​public_html ]:$ pip freeze Django==1.7 django-bootstrap-too‌​lkit==2.15.0 django-registration-‌​redux==1.4 Pillow==3.3.0 uWSGI==2.0.13.1` do you have any other ideas? – Reven Aug 17 '16 at 11:29
  • Actually, I don't think you should have `import uwsgi` in your settings file at all. It's only going to work when the uwsgi server is running your project, it won't work for management commands like `collectstatic`. What are you using it for in your settings?Try removing it. – Alasdair Aug 17 '16 at 12:09
  • @Alasdair you were right. My mistake that I didn't put whole settings file here. Problem was caused by following lines in settings.py: `import uwsgi` and function which was searching for changes in code `@timer(3) def change_code_gracefull_reload(sig): if autoreload.code_changed(): uwsgi.reload()` which i found in [link](http://serverfault.com/questions/411362/how-do-i-make-uwsgi-restart-when-a-python-script-is-modified). However, at the moment i need to restart the server each time when I change the code, is there any way around that? – Reven Aug 17 '16 at 16:07
  • I don't know about how to get auto-reloading to work with uswgi. You might be better to ask a new question - it's very different to your original question. – Alasdair Aug 17 '16 at 17:06

1 Answers1

2

try sudo pip install uwsgi as you have imported this in settings file

Aamish Baloch
  • 1,200
  • 12
  • 9
  • Alasdair, Aamish thanks for help but i see (when i am in virtual env) that uwsgi is installed) `(venv) [mrklap@s34:: ~/domains/tredo.net/public_html ]:$ pip freeze Django==1.7 django-bootstrap-toolkit==2.15.0 django-registration-redux==1.4 Pillow==3.3.0 uWSGI==2.0.13.1 ` do you have any other ideas? – Reven Aug 17 '16 at 11:13
  • I am activating by `source ~/venv/bin/activate` is it correct? – Reven Aug 17 '16 at 11:19
  • Yes it's fine. Try uninstalling and reinstalling Uwsgi. You might occur some problem installing, that will help us solve the issue. – Aamish Baloch Aug 17 '16 at 11:30
  • (in venv) made `pip uninstall uwsgi` (finished succesful) and `pip install uwsgi` (Successfully installed uwsgi-2.0.13.1) and when collectstatic got the same error as above. – Reven Aug 17 '16 at 11:36
  • Then I have to get into this in more detail. I'll ping back to you soon. – Aamish Baloch Aug 17 '16 at 12:22
  • please lookup for my last comment in post below, however thank you for your help. By the way, later after reinstalling uwsgi i faced error when trying to connect to server - you can avoid that by giving exact path to uwsgi file in my case it was `/home//venv/bin/uwsgi` – Reven Aug 17 '16 at 16:12