Deploy Django path static files

I am trying to deploy a django application for my VPS. So I followed this guide ( link ), and the only problem I am facing is that I cannot make my static files appear.

I can see my static files (so nginx works correctly): Example: http: //188.xxx.xxx.93/static/css/bootstrap.css .

But when I go to my site, I see that it links the css file as follows: <link href="/static/css/bootstrap.css" rel="stylesheet"> Therefore, it tries to find the file (using the port): http: //188.xxx.xxx.93 : 8001 / static / bootstrap. css, which apparently does not work. What should I change to my django settings in order to make static files work?

Below you can find the structure of my files on VPS.

Virtual Env: /opt/myapps/

Django project: /opt/myapps/uniprogress/

Static Files: /opt/myapps/uniprogress/static/

Nginx config: / etc / nginx / sites-available / uniprogress

server {

server_name 188.xxx.xxx.93;

access_log off;

location /static/ {
    alias /opt/myapps/uniprogress/static/;
}

location / {
    proxy_pass http://127.0.0.1:8001;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}

and finally in the django.py settings:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_ROOT = '/opt/myapps/uniprogress/static/'
STATIC_URL = '/static/'

I also used: python manage.py collectstatic, but still my static files will not be displayed.

+4
source share
3 answers

, , STATIC_URL : STATIC_URL = 'http://static.example.com/'

, , ?

- , ?

Ps. css - :

<link href="http://188.xxx.xxx.93/static/css/bootstrap.min.css" rel="stylesheet">.

+1

I would recommend doing it a little differently, so your whole project can be portable. In settings.py, here is an example:

# Get the current directory
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Define the URL path
STATIC_URL = '/static/'

# Join the BASE_DIR with static, which means you can move the project folder anywhere
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

Hope this helps!

0
source

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


All Articles