User
User

Reputation: 24739

Managing URLs in a Django site inside a folder

So I have my Django project in a folder on my server, like so:

www.mydomain.com/myfolder/

The index page loads, but things like the css document don't because the paths are improper. Am I missing a shortcut? I looked through the settings.py documentation for a place to specify my site folder, but didn't find anything to help.

So example, if I specify this in my settings:

STATIC_URL = 'static/'

Then it will look for my css doc on my /myfolder/login page at:

http://mydomain.com/myfolder/login/static/stylesheet.css

If I specify:

STATIC_URL = '/static/'

Then it will look for my css doc on my /myfolder/login page at:

http://mydomain.com/static/stylesheet.css

Meanwhile, my css document is at:

http://mydomain.com/myfolder/static/stylesheet.css

Do I need to change everything and specify /myfolder/ before everything?

Upvotes: 0

Views: 98

Answers (3)

django11
django11

Reputation: 811

I think you need to add path of you STATIC_ROOT in settings, and update urls.py file.

My example:

#settings.py 

....

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static",)
#path to your static folder

#urls.py

...
from django.conf import settings
from django.conf.urls.static import static

....

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Upvotes: 1

Sam Dolan
Sam Dolan

Reputation: 32532

Don't you just need to do this?

STATIC_URL = '/myfolder/static/'

Upvotes: 1

Alfred Huang
Alfred Huang

Reputation: 18255

This question has a good practice solution on django tutorial:

See:

https://docs.djangoproject.com/en/1.6/intro/tutorial06/#customize-your-app-s-look-and-feel

That you should create a /static/appname/ folder inside each of your app, and then put the static files inside it.

So in your app, the static file url should turned into something like:

http://mydomain.com/static/app_name/stylesheet.css

Which won't collides with other app.

You'd better read the link I offered above, the tutorial shows a good habit.

Upvotes: 1

Related Questions