Setup Static and media root and static and media url in django

 http://www.django.co.zw/en/tutorials/setting-django-s-static-and-media-urls/

follow the step by step tutorial for settings up static and media urls.

My points are below.

1. Goto the settings.py file.

2. "import os" we need to import os Class because we need to do some work with it.

3. go to the TEMPLATES section and change the DIRS list to "os.path.join(BASE_DIR, 'templates' "

4. go to the bottom of the file while STATIC_URL is written.

5. below the STATIC_URL write STATICFILES_DIRS = [os.path.join(BASE_DIR,'static_project')] it is a list.

6. now also create a static_project folder in your base directory.

7. in settings.py file below the STATICFILES_DIRS writedown STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root') 

8. below it write down MEDIA_URL = '/media/'

9. now write MEDIA_ROOT = os.path.join(BASE_DIR,'static_cdn', 'media_root')

the complete code will be look like

in settings.py file

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR,'static_project')]

STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root') 

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR,'static_cdn', 'media_root')


10. now go to urls.py file of the project.

11. import the below things

from django.conf import settings

from django.conf.urls.static import static

12. import the below things 

from django.conf.urls.static import static

13. under the urlpatterns = []

14. write down below it

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

urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) 

15. now complete and open the terminal

16. write down the below command one by one

    1. python manage.py makemigrations

    2. python manage.py migrate

    3. python manage.py collectstatic

17. complete

Comments