Django不能识别MEDIA_URL路径? [英] Django not recognizing the MEDIA_URL path?

查看:128
本文介绍了Django不能识别MEDIA_URL路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让TinyMCE在一个简单的视图功能上运行,但是tiny_mce.js文件的路径被搞砸了。该文件位于/Users/home/Django/tinyMCE/media/js/tiny_mce/tiny_mce.js。我相信/media/js/tiny_mce/tiny_mce.js是正确的,因为开发服务器无法访问Django项目文件夹根目录以上的文件。在渲染页面中,它在调试器中说没有找到javascript文件。这是因为它试图通过/js/tiny_mce/tiny_mce.js查看路径名的/ media / part。

So I'm trying to get TinyMCE up and running on a simple view function, but the path to the tiny_mce.js file gets screwed up. The file is located at /Users/home/Django/tinyMCE/media/js/tiny_mce/tiny_mce.js. I believe that /media/js/tiny_mce/tiny_mce.js is correct, as the development server has no access to files above the root of the Django project folder. In the rendered page, it says in the debugger that the javascript file was not found. This is because it was trying to look through /js/tiny_mce/tiny_mce.js without addressing the /media/ part of the pathname.

无论如何,这里是脚本代码片段在一个名为simple.html的模板中的javascript。 < script type =text / javascriptsrc ={{MEDIA_URL}} / js / tiny_mce / tiny_mce.js>< / script>
< script type =text / javascript>
tinyMCE.init({
mode:textareas,
theme:simple
});
< / script>

Anyway, here's the script snippet for the javascript in a template named 'simple.html'. <script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ mode : "textareas", theme : "simple" }); </script>

这是我的设置的重要部分。
$ b

And this is what the vital parts of my settings is like.

MEDIA_ROOT = os.path.join(_base, 'media')
MEDIA_URL = 'http://127.0.0.1:8000/media/'
ADMIN_MEDIA_PREFIX = '/media/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'tinymce',
)


推荐答案

您可以手动将其传递给模板,其他人建议或确保使用 RequestContext 而不是普通的上下文。 RequestContext将自动填充包含MEDIA_URL和其他与媒体相关的变量的上下文。

You can either pass it to the template manually as others have suggested or ensure that you are using a RequestContext instead of plain Context. RequestContext will automatically populate the context with certain variables including MEDIA_URL and other media-related ones.

文档示例:

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

这篇关于Django不能识别MEDIA_URL路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆