0

my settings.py is like this:

import os

ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))


STATIC_ROOT = ''

STATIC_URL = '/static/waijiaocrm/'


STATICFILES_DIRS = (

    os.path.join(ROOT, '/static/waijiaocrm/')
)

my static files is stored at project/static/waijiaocrm/

static/waijiaocrm

-admin/

-css/

-image/

-js/

my template is like this:

{% load static from staticfiles %}
<link href="{% static 'css/base.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'css/login.css' %}" rel="stylesheet" type="text/css" />

my urls.py:

url(r'^login/$', 'django.contrib.auth.views.login',
        {'authentication_form': MyAuthenticationForm,
        'template_name': 'waijiaocrm/login.html'}, name="login"),

when I run the login, I can only get the login html, without css or image files, I did as the documentation says, but can't get the right page, how to fix this?

[08/May/2013 17:54:10] "GET /accounts/login/ HTTP/1.1" 200 2814
[08/May/2013 17:54:10] "GET /static/waijiaocrm/css/base.css HTTP/1.1" 404 1662
[08/May/2013 17:54:10] "GET /static/waijiaocrm/css/login.css HTTP/1.1" 404 1665
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/lib/jquery-1.8.2.min.js HTTP/1.1" 404 1704
[08/May/2013 17:54:10] "GET /accounts/login/image/crm_logo.png HTTP/1.1" 404 3778
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/lib/jquery.placeholder.min.js HTTP/1.1" 404 1722
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/formValidator.js HTTP/1.1" 404 1683
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/login.js HTTP/1.1" 404 1659
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/lib/jquery.placeholder.min.js HTTP/1.1" 404 1722
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/formValidator.js HTTP/1.1" 404 1683
[08/May/2013 17:54:10] "GET /static/waijiaocrm/js/login.js HTTP/1.1" 404 1659

I finally fixed it,it's because of a wrong template_dir configuration, thank all of you guys for your patience and your time, @Hedde, I really appreciate your layout for your project, and I vote for your answer in the question you linked below.

Roger Liu
  • 1,768
  • 3
  • 16
  • 25
  • Are you running in DEBUG mode? If not, static files [aren't served](https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-option---insecure) by Django by design. – gertvdijk May 08 '13 at 09:35
  • yes I'm running it in DEBUG mode – Roger Liu May 08 '13 at 09:36

4 Answers4

0

Assuming waijiaocrm is an app, you should use that prefix

{% static 'waijiaocrm/css/base.css' %}

Have a look here at common directory structures for Django

Community
  • 1
  • 1
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • it's not an app, but a directory for holding the static files – Roger Liu May 08 '13 at 09:02
  • I put all of my static files in a directory static/waijiaocrm , and none of my apps is named waijiaocrm – Roger Liu May 08 '13 at 09:03
  • You should probably not do that unless it is your production static folder – Hedde van der Heide May 08 '13 at 09:03
  • I don't want to put the static files in my app directory, how to fix it? – Roger Liu May 08 '13 at 09:06
  • I have updated my answer [here](http://stackoverflow.com/questions/11216829/django-directory-structure) to give you an idea how to layout your project. – Hedde van der Heide May 08 '13 at 09:10
  • well, now I want to fix my static file path problem, cause the static files are written by somebody else, and I wrote the backend py files, now I'm trying to get them together to have my program work. I wonder what's going wrong with my configuration. – Roger Liu May 08 '13 at 09:19
0

if your MEDIA_URL is defined as ‘/media/’, you can do this by adding the following snippet to your urls.py

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

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
anish
  • 6,884
  • 13
  • 74
  • 140
  • I'm sorry but it seems you are not answering my question – Roger Liu May 08 '13 at 09:07
  • can u check the chrome/firefox Developer Tool by pressing F12 shortcut, to check whether the css are getting resolved or not by the browser this will help u – anish May 08 '13 at 09:12
  • I've already checked the source html code generated by django, and the css and image urls it generates are wrong – Roger Liu May 08 '13 at 09:21
  • @RogerLiu What do the URIs look like then? – gertvdijk May 08 '13 at 09:36
  • it looks like this: , the prefix is what I defined as static_url in settings – Roger Liu May 08 '13 at 09:43
  • @RogerLiu This is perfectly fine and according to what I expect from your configuration. Please elaborate on what *exactly* is wrong. What response does the Django development server give you on the browser request? What URI did you expect instead? etc. And please *edit* your question instead of providing new information in comments. It's a Q&A site, not a discussion forum, you see? :) – gertvdijk May 08 '13 at 09:46
  • Well, it obviously doesn't give the right path of the static files, what I see now is raw html with no style, I expect to click that link it generates and get the corresponding css file – Roger Liu May 08 '13 at 09:48
  • @gertvdijk now when I click the css link, it gets "A server error occurred. Please contact the administrator." – Roger Liu May 08 '13 at 09:51
  • @RogerLiu If you see a 500 response without a traceback, DEBUG isn't on. Moreover, look at the server output (console) for an error, not in your browser only displaying the output. And again: please **edit** your question with all this new information. – gertvdijk May 08 '13 at 09:53
  • @gertvdijk I found I forgot to add a ',' in the staticfile_dir in settings, after I add it and run the code again, I got a lot of 404 responses... I added it in my question – Roger Liu May 08 '13 at 09:57
  • finnaly I fix it , I messed it up with my template_dir setting... my dir name should be templates instead of template, I forgot a 's'.. this is so embarrassing.. Thank you for your time and patience, I appreciated it buddy – Roger Liu May 08 '13 at 10:19
0

Make sure that your main app, the one with wsgi.py in it is listed in installed apps.

chrisortman
  • 1,514
  • 15
  • 22
0

I had to use

STATICFILES_DIRS = ( '/home/USERNAME/webapps/django/PROJECT/static/', )

That helped me.

jwebuser
  • 103
  • 1
  • 3