2

I am new to django, presently I am learning and trying to build a basic site with django-cms. Below are my project folder structure and code files

project_folder
    manage.py 
    main_folder 
         static
             css
                new-styles.css
             js
                new-styles.js
         templates                      
             base.html
             home.html
         media
             pic_1.gif 
             .....
         settings.py
         urls.py
         ....... 

settings.py

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR,'templates'))
TEMPLATE_CONTEXT_PROCESSORS = (
       'django.contrib.auth.context_processors.auth',
       'django.core.context_processors.i18n',
       'django.core.context_processors.request',
       'django.core.context_processors.media',
       'django.core.context_processors.static',
       'django.core.context_processors.debug',
       'django.contrib.messages.context_processors.messages',
       'cms.context_processors.media',
       'sekizai.context_processors.sekizai',
)
......
.......

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings

urlpatterns = patterns('',
            url(r'^admin/', include(admin.site.urls)),
            url(r'^$', ...........),               
)

if settings.DEBUG:
    urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns

base.html

{% load cms_tags sekizai_tags menu_tags %}
<html>
  <head>
    <title>{%block "title" %}{% endblock %}</title>
    <link rel="stylesheet" href="{{ STATIC_URL }}css/new-styles.css">
  {% render_block "css" %}
  </head> 
  <body>
  {% cms_toolbar %}
    {% block "base_content" %}
    {% endblock %}
  {% render_block "js" %}  
  </body>
</html>

home.html

{% extends "base.html" %}
{% load cms_tags %}
{% block "title" %}Welcome to Services{% endblock %}
{% block "base_content" %}
  <p>Hi this is paragraph</p>
  <img src="{{ MEDIA_URL }}images/promo3.jpg" />
 {% placeholder "number_one" %} 
 {% endblock %}

new-styles.css

body {  
    padding:0px; margin:0px; margin-top:40px;
        background-color:#b0c4de;
     }
p {
      background-color:#e0ffff;
  }     

So above are my complete project structure and code files, but my actual problem is new-styles.css file is not working, even though I had written some css code in the file it is not linking to the template, so can anyone please let me know whats wrong and why base.html template file is unable to access the new-styles.css file, whether the path given in link tag is wrong or the path setting in the settings.py is wrong?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • Consider this page https://docs.djangoproject.com/en/dev/howto/static-files/#using-django-contrib-staticfiles and this one https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-in-templates Also modify your urlpatterns: ` from django.contrib.staticfiles.urls import staticfiles_urlpatterns # ... the rest of your URLconf goes here ... urlpatterns += staticfiles_urlpatterns() ` – Denis Nikanorov Feb 05 '13 at 10:30
  • yeah i had gone through every link and written code according to the tutorial, i had mentioned entire code above because i am unable to find why template can't render/use the css file ? – Shiva Krishna Bavandla Feb 05 '13 at 10:31
  • Did you add `'django.core.context_processors.static'` to your `TEMPLATE_CONTEXT_PROCESSORS` in settings.py? Take a look here https://docs.djangoproject.com/en/dev/howto/static-files/#with-a-context-processor – Denis Nikanorov Feb 05 '13 at 10:33
  • yeah i had used it and edited settings.py file code above – Shiva Krishna Bavandla Feb 05 '13 at 10:35
  • Did you put files to static folder by yourself or do it with `manage.py collectstatic`? – Denis Nikanorov Feb 05 '13 at 10:37
  • oh actually i created myself a folder called static,i created again two folders css which consists of new-services.css and js which consists of new-services.js. After that i had run python manage.py collectstatic by which all the files are grouped int o my static folder – Shiva Krishna Bavandla Feb 05 '13 at 10:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23957/discussion-between-shiva-krishna-and-denis-nikanorov) – Shiva Krishna Bavandla Feb 05 '13 at 10:44
  • Hi if u don't mind can we continue this in chat here http://chat.stackoverflow.com/rooms/23957/discussion-between-shiva-krishna-and-denis-nikanorov – Shiva Krishna Bavandla Feb 05 '13 at 10:45

1 Answers1

5

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from project_name import settings

admin.autodiscover()

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

urlpatterns += staticfiles_urlpatterns()
catherine
  • 22,492
  • 12
  • 61
  • 85
  • 2
    You've used both `static` and `staticfiles_urlpatterns`. Can you elaborate on why and the differences between them? – OrangeDog Dec 08 '17 at 10:47
  • You should not import settings directly like that, as stated [here](https://stackoverflow.com/a/8780907/13824776). Instead use: ```from django.conf import settings``` – xodeeq Apr 08 '23 at 23:49