开发者

django MEDIA_URL changes in child template?

开发者 https://www.devze.com 2023-03-28 09:06 出处:网络
I have these URLs in my project .urls: urlpatterns = patterns(\'\', (r\'^categories/\', include(\'category.urls\')),

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 t开发者_如何学Pythonemplates 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?


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

0

精彩评论

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