4
votes

I'm trying to deploy my django application to my VPS. So I followed this tutorial(link) and the only issue i'm having is that I can't get my static files to show up.

I can see my static files(so nginx is working properly): example: http://188.xxx.xxx.93/static/css/bootstrap.css.

But when I'm going to my website, I can see that it links a css file like this: <link href="/static/css/bootstrap.css" rel="stylesheet"> So it tries to find the file(using the port): http://188.xxx.xxx.93:8001/static/bootstrap.css which apparently doesn't 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 the 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 my django settings.py:

# 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 won't show up.

3

3 Answers

1
votes

I was just reading another tutorial link and I found out that STATIC_URL can be something like this: STATIC_URL = 'http://static.example.com/'

So I guess the solution is this one?

If anyone experienced can let me know if this is the correct way or if it's not the right approach?

Ps. My css files now in my website are like this:

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

0
votes

I recommend you read the following doc, which will give a better understanding of Django static files. http://bitsoul.com/2012/10/04/understanding-django-static-files-for-beginners/

0
votes

I would recommend doing it somewhat differently, so your entire project can be portable. In settings.py, here's 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"),
)

I hope this helps!