开发者

Django not displaying image in css

I have a django application.I have 开发者_StackOverflow社区added image (url) in css,it isn't displaying the image.

But when I used it as a html file,it is displaying the image.Should I set any url in settings.py for this? Source [shown here][1]. [1]: http://dpaste.com/164620/


That is definitely a local path. You need either a path relative to the doc root (by default, Django doesn't serve media files, it's discouraged to let it do that outside testing environments) or an absolute URL if the file is on a different (sub-)domain (e.g. a local Apache vhost that serves the media files).

If you're using relative paths, beware that the path will be relative to the page the path is mentioned on (i.e. if you put it in a CSS file, it will be relative to the CSS file; if you put it in a template, it will be relative to whatever page is shown with that template).

If you're using absolute paths, beware that the path will be relative to the doc root of that (sub-)domain.

EDIT: NO, really. It's the path. A path in CSS or HTML will be parsed by your browser. So even if you run the thing on localhost, an absolute path (starting with /) will be parsed as relative to the document root (i.e. handed over to Django's URL resolution).

If you have the site running on http://localhost:8000, /home/logic/quote/template/hummingbirds.gif will be treated as http://localhost:8000/home/logic/quote/template/hummingbirds.gif, i.e. your browser will make a HTTP GET request to the server running at localhost:8000 for the path /home/logic/quote/template/hummingbirds.gif. If the server is Django, it will try to find a rule matching /home/logic/quote/template/hummingbirds.gif in your urls.py. You can't refer to a file in your filesystem by just passing the local path.

If you want to serve static files (e.g. images) with Django (i.e. on the same domain and port Django runs on), you need to configure it to serve these files first like so: http://docs.djangoproject.com/en/dev/howto/static-files/

If you want to refer to a file on your filesystem (BAD practice and needs to be replaced if the thing EVER goes online), you need to use the file:// protocol explicitly. Absolute URLs (i.e. without protocol prefix and domain name) will always be treated as relative to the current protocol and domain.


is /home/logic/quote/template/hummingbirds.gif your image web path? it look like a local path. does it show when you type http://your.host/home/logic/quote/template/hummingbirds.gif

if you set a background-color, does the color appear?

suggestion: on firefox with firebug:

  • enable css warning
  • lokks at the parsed css file (css tab) to see if your rule is well writen
  • inspect the element your background should be on and verify it has the css rule applied and not overwriten
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜