Is django-cms routed incorrectly to a static URL?

I am still relatively new to Django and have just started to understand Django-CMS, however I cannot go through the basic introduction from the official docs .

My problem may be related to sekizai, but it looks like a basic problem with url.py.

Basically, I followed the textbook almost exactly. The only difference is that I have a cms application on blog /, the full path ~ / workspace / djangocms / blog /. I correctly set STATIC_URL and STATIC_ROOT in the settings.py parameters, as well as my MEDIA_URL and MEDIA_ROOT .

I am raising the media path and directory, because if I set the base template for the css link in {{ MEDIA_URL }}css/somecss.min.css , it works fine. However, doing the same with STATIC_URL: {{ STATIC_URL }}css/somecss.min.css does not work and produces 404s.

Also, from what I can tell, by default / static / routes seem to work fine for other directories. The code generated by {% cms_toolbar %} generates a fine and css from places like /static/cms/css/plugins/cms.toolbar.css that are properly served.

Urls.py content

 from django.conf.urls.defaults import * from django.contrib import admin from django.conf import settings admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'blog.views.home', name='home'), # url(r'^blog/', include('blog.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), (r'^admin/', include(admin.site.urls)), url(r'^', include('cms.urls')), ) if settings.DEBUG: urlpatterns = patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), url(r'', include('django.contrib.staticfiles.urls')), ) + urlpatterns 
+6
source share
4 answers

Got a response from #django on freenode. Answering it myself here, if someone needs it:

Mainly for django-cms STATIC_ROOT is mainly used for assets of django-cms itself during production. If django is started using the python manage.py runserver , STATIC_ROOT does not matter.

From the Django docs : use collectstatic to collect all the static assets used (which for django-cms will probably be under a directory like / usr / local / lib / python ## / dist-packages). This shows which STATIC_ROOT files will be used during production.

If you want to serve other directories using STATIC_URL, you will have to add them to the STATICFILES_DIRS settings.py section. (Be sure to read the comments - use absolute paths, not relative paths). This is actually relatively obvious when creating a clean Django application, however, since I wanted to strictly follow the Django-cms tutorial, it was not as obvious as it should have been.

+6
source

I went around this problem after following the same tutorial.

My site will load a standard welcome page and I can create CMS pages. However, when I tried to load static files into my templates, I kept getting 404 errors.

The answer given by FP helped me a lot.

To be clear, this worked for me:

  • Rename the static project directory to something like local_static

  • Add the following options to settings.py

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))

STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, "local_static/"), )

It worked for me. Now I can stop pulling my hair.

+8
source

The solution is so simple that I also missed it.

from settings.py;

 STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'mysitename', 'static'), ) 

If you look at "mysitename", another folder called "static" will appear. If you put the files there, everything will work as you might expect. The "static" folder at the same level as manage.py is a red herring.

+1
source

I wrote the same answer here , but I ran into the same problems. Although I was able to serve static files for everything else, for some reason my django-cms files kept returning 404. I checked settings.py and permissions, but I had no luck.

In Ubuntu 14.04, running Nginx and Gunicion, I can solve the problem by editing:

 sudo nano /etc/nginx/sites-enabled/django 

And I added the following code block

 location /static/cms { alias /usr/local/lib/python2.7/dist-packages/cms/static/cms/; } 

Then I restarted Nginx and Gunicorn

 sudo service nginx restart && sudo service gunicorn restart 

and I was able to see all the missing static files

0
source

Source: https://habr.com/ru/post/904010/


All Articles