nitely / Spirit

Spirit is a modern Python based forum built on top of Django framework
http://spirit.readthedocs.io
MIT License
1.16k stars 332 forks source link

ValueError when I run in default production settings #297

Closed YUX closed 3 years ago

YUX commented 3 years ago

ValueError when I run in production settings while works great on dev settings.

ValueError: Missing staticfiles manifest entry for 'spirit/stylesheets/styles.all.min.css'

ERROR 2020-12-19 14:11:23,668 log 19205 139777169270528 Internal Server Error: /
Traceback (most recent call last):
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/spirit/topic/views.py", line 124, in index_active
    return render(
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/templatetags/static.py", line 106, in render
    url = self.url(context)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/templatetags/static.py", line 103, in url
    return self.handle_simple(path)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/templatetags/static.py", line 118, in handle_simple
    return staticfiles_storage.url(path)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 147, in url
    return self._url(self.stored_name, name, force)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 126, in _url
    hashed_name = hashed_name_func(*args)
  File "/root/mambaforge/envs/xxxxxxx/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 417, in stored_name
    raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'spirit/stylesheets/styles.all.min.css'
ERROR 2020-12-19 14:11:23,722 basehttp 19205 139777169270528 "GET / HTTP/1.1" 500 145
nitely commented 3 years ago

Follow the install steps, or maybe run collectstatics, or maybe change the static storage setting. It's a Django issue, so if nothing fixes it try stackoverflow. I can't really help if I can't reproduce the issue.

nitely commented 3 years ago

FWIW, https://community.spirit-project.com/ runs with the default prod settings...

nitely commented 3 years ago

I was able to reproduce it locally, just run the spiritinstall command (it'll run collectstatics among other things).

YUX commented 3 years ago

thank you for your responses, but collectstatics didn't fix the problem for me. I get 0 static files copied to '/Users/yux/Documents/Projects/mysite/static', 2122 unmodified. then the same ValueError: Missing staticfiles manifest entry for 'spirit/stylesheets/styles.all.min.css' when I run in production settings. I've tried different Django version on another server, I got the same error.

❯ pip list
Package                           Version
--------------------------------- -------------------
asgiref                           3.3.1
certifi                           2020.12.5
Django                            3.1.4
django-djconfig                   0.10.0
django-haystack                   3.0
django-infinite-scroll-pagination 1.1.0
django-spirit                     0.12.1
mistune                           0.8.4
olefile                           0.46
Pillow                            8.0.1
pip                               20.3.3
pytz                              2020.4
setuptools                        49.6.0.post20201009
sqlparse                          0.4.1
wheel                             0.36.2
Whoosh                            2.7.4

I ran the exact commands in the Docs

pip install django-spirit
spirit startproject mysite
cd mysite
python manage.py spiritinstall
python manage.py createsuperuser
python manage.py runserver

everything looks great on http://127.0.0.1:8000

then I created prod_local.py

from .prod import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

ALLOWED_HOSTS = ['127.0.0.1', ]

then python manage.py runserver --settings=mysite.settings.prod_local and things break.

I get the ``` ValueError: Missing staticfiles manifest entry for 'spirit/stylesheets/styles.all.min.css' ERROR 2020-12-20 00:18:12,377 basehttp 30325 6200111104 "GET / HTTP/1.1" 500 145

nitely commented 3 years ago

Well, you are running the commands using the default config (dev), and then running the server using the prod settings. That's the issue. You must run all commands using the prod settings, ex python manage.py spiritinstall --settings=mysite.settings.prod_local, if that doesn't work, then modify the manage.py to use the prod_local settings (that's what I do).

YUX commented 3 years ago

thank you for these instructions, finally I didn't see the ValueError after I had modified manage.py. and I can see the right staticfiles.json file.

but when I check in Firefox I see the http://127.0.0.1:8000/static/spirit/stylesheets/styles.all.min.02f514517c22.css and http://127.0.0.1:8000/static/spirit/scripts/all.min.7c6099484c1c.js are 404.

yet I can see those files with hash in the file name in the folder.

nitely commented 3 years ago

Yeah, Django only serves static files when setting DEBUG = True, which you don't want to do in a prod server. You can either install and configure Whitenoise, or use Nginx to serve the static files, and while you are at it, you may want to use Nginx as a reverse proxy for Spirit, and also use a better webserver like gunicorn instead of the Django built-in server.

YUX commented 3 years ago

I see. This is an awesome project. Thank you very much.