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?

8
  • Consider this page docs.djangoproject.com/en/dev/howto/static-files/… and this one docs.djangoproject.com/en/dev/howto/static-files/… Also modify your urlpatterns: ` from django.contrib.staticfiles.urls import staticfiles_urlpatterns # ... the rest of your URLconf goes here ... urlpatterns += staticfiles_urlpatterns() ` Commented Feb 5, 2013 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 ? Commented Feb 5, 2013 at 10:31
  • Did you add 'django.core.context_processors.static' to your TEMPLATE_CONTEXT_PROCESSORS in settings.py? Take a look here docs.djangoproject.com/en/dev/howto/static-files/… Commented Feb 5, 2013 at 10:33
  • yeah i had used it and edited settings.py file code above Commented Feb 5, 2013 at 10:35
  • Did you put files to static folder by yourself or do it with manage.py collectstatic? Commented Feb 5, 2013 at 10:37

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

You've used both static and staticfiles_urlpatterns. Can you elaborate on why and the differences between them?
You should not import settings directly like that, as stated here. Instead use: from django.conf import settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.