0

I have these URLs in my project .urls:

urlpatterns = patterns('',
    (r'^categories/', include('category.urls')),
)

In the categroy app, my category.urls:

urlpatterns = patterns('category.views',
    (r'^$', 'category_tree'),
    (r'^add/?$', 'category_add'),)

I have this in my settings.py:

MEDIA_URL = "http://localhost:80/media/"
ROOT_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(ROOT_PATH, 'templates'),
)

In the project templates directory there is a base template "base.html" with this line:

<link href="{{MEDIA_URL}}css/base.css" rel="stylesheet" />

In my "category" app, I also have templates "category_tree.html" and "category_add.html". These both extend from base.html:

{% extends "base.html" %}

The blocks in base.html are rendered correctly with content from these two child templates/views. But the css and images of category_add.html aren't found.

There is a link on categroy_tree.html like this:

<div><a href="add">Add category</a></div>

This points to the correct view if clicked. But then the css MEDIA_URL request changes from

http://localhost/media/css/base.css
// (Correct)

to

http://localhost:8000/categories/css/base.css
// (Incorrect)

Why is this happening and what do I have to do to fix this?

Robse
  • 853
  • 3
  • 14
  • 28
  • 1
    Are you sure the template is printing `{{ MEDIA_URL }}` correctly? What does the link look like in the generated HTML? This question might help: http://stackoverflow.com/questions/3756841/django-media-url-blank – Shawn Chin Aug 15 '11 at 14:44
  • Thanks Shawn see below, problem is fixed. – Robse Aug 15 '11 at 14:57

1 Answers1

2

The add category view isn't using a RequestContext to render the page, so MEDIA_URL is not sent to the template context.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895