Django Rest Framework without styles or scripts

10

Once I have the API working, when I enter the graphical environment ( domain.com/API ) of tests I get no styles, meaning that all < a href="/ questions / tagged / css" class="post-tag" title = 'show questions with the tag "css"'> css and the can not find them.

In the settings.py I added the STATIC_URL and STATIC_ROOT after I made the python manage.py collectstatic and it has put all of admin within the directory, but all that they are rest_framework no.

I've seen another post in stack overflow that said to add STATICFILES_DIRS but it has not worked either.

I have evidently made a service gunicorn restart after each change.

Could someone enlighten me?

STATIC_URL = '/static/'
STATIC_ROOT = '/home/django/django_project/static/'

MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/django/django_project/media/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '/home/django/django_project/'),
)

No one knows how to load all those files?

ERROR: Here is one of the errors for you to see the route you are looking for.

the route the browser is searching for is http://sub.domain.com/static/rest_framework/css/default.css

The fact is that within my folder static are all files.

EDITO: When executing the command that says @ césar I get the following message.

[email protected]:/home/django/django_project# python manage.py findstatic rest_framework/css/default.css

/home/django/django_project/django_project/urls.py:33: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got my_app.views.home). Pass the callable instead.
  url(r'^$', 'my_app.views.home', name='home'),

/home/django/django_project/django_project/urls.py:37: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
  url(r'^API', include(router.urls)),

System check identified some issues:

WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.

Found 'rest_framework/css/default.css' here:
  /usr/local/lib/python2.7/dist-packages/rest_framework/static/rest_framework/css/default.css

No one knows how to do it? Some charitable soul? I've tried everything and I can not find the way.

EDIT: I've deleted the entire static folder and I've done a collectstatic again and the same thing keeps happening to me. (Can not find the img of the boolean /static/admin/img/icon-yes.svg and they are in that directory)

    
asked by RuralGalaxy 11.05.2016 в 15:45
source

2 answers

1

I imagine that you will have solved the problem, starting from the time that the question has. but to leave some answer for future consultations. You have an error in the settings of the project.

When you declare BASE_DIR you get the root path of the project, in your case the value of the variable must be:

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

What produces the following:

 BASE_DIR = '/home/django/django_project/'

Then in the settings we must create routes using BASE_DIR and not like you have done with the string:

STATIC_URL = '/static/'
STATIC_ROOT = '/home/django/django_project/static/'

Must be done:

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

Then in:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '/home/django/django_project/'),
)

You are telling Django to concatenate BASE_DIR + BASE_DIR what it produces:

'/ home / django / django_project / home / django / django_project /'

Path that surely does not exist in the system.

What django does with the collectstatic is to search in these directories that we add in STATICFILES_DIRS and copy them to STATIC_ROOT including by default all the 'static' folders of the applications listed in INSTALLED_APPS. example: django.contrib.admin is installed on site-packages: PYTHONPATH + / site-packages / django / contrib / admin / and within that folder exists 'static / admin /'

Collect static will copy everything in the 'static' directory to STATIC_ROOT.

Having this clear, then we must verify that all resources are present within the STATIC_ROOT.

and when serving the project with apache, nginx or any other HTTP server we must indicate that all the requests made to myomominio.com/static serve them directly from the STATIC_ROOT folder and grant the permissions so that it can access the directory:

Apache example:

Alias /static /home/django/django_project/static/

<Directory /home/django/django_project/static/>
   Require all granted
</Directory>

Example Nginx:

location /static {
    alias /home/django/django_project/static/; 
}

and to finish you must make sure that the STATIC_ROOT folder has read permissions for all users.

chmod -R +r  /home/django/django_project/static/

With this we will have guaranteed that all static resources are available.

And keep in mind, always in our apps, place the statics inside static / app_name / in order to prevent our files from writing those of others: example if you put /home/django/django_project/app_name/static/admin/main.css and django.contrib.admin contains main.css, your main.css file will replace the admin file and affect the operation of admin.

Since django collects the files in the same order that the apps are listed in settings INSTALLED_APPS and admin will be before app_name.

    
answered by 17.02.2017 в 16:20
0

You must run the command so that django extracts the static from all the packages.

python manage.py collectstatic
    
answered by 18.10.2016 в 23:38