python Django 无法识别 MEDIA_URL 路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2039889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django not recognizing the MEDIA_URL path?
提问by rk919
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.
所以我试图让 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/ 部分。
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>
无论如何,这里是名为“simple.html”的模板中的 javascript 脚本片段。 <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>
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',
)
回答by luc
It looks like your are using the debug server (your url is http://127.0.0.1:8000/...) . Did you install the static serve in your urls.py?
看起来您正在使用调试服务器(您的网址是http://127.0.0.1:8000/...)。您是否在 urls.py 中安装了静态服务?
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)
The 'show_indexes':True options make possible to browse your media. Go to your medias root http://127.0.0.1:8000/media/and see it there is something
'show_indexes':True 选项可以浏览您的媒体。去你的媒体根http://127.0.0.1:8000/media/看看有什么
回答by Ben Regenspan
You can either pass it to the template manually as others have suggested or ensure that you are using a RequestContextinstead of plain Context. RequestContext will automatically populate the context with certain variables including MEDIA_URL and other media-related ones.
您可以按照其他人的建议将其手动传递给模板,也可以确保您使用的是RequestContext而不是普通的 Context。RequestContext 将自动使用某些变量填充上下文,包括 MEDIA_URL 和其他与媒体相关的变量。
Example from docs:
来自文档的示例:
def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
回答by David Wolever
It looks like ars has answered your realquestion… But you'll run into another problem: MEDIA_URL
must be differentfrom ADMIN_MEDIA_PREFIX
. If they aren't, the ADMIN_MEDIA_PREFIX
will take precedence. I usually fix this by changing ADMIN_MEDIA_PREFIX
to /admin-media/
.
它看起来像ARS已经回答了你真正的问题...但你会碰到另一个问题:MEDIA_URL
必须是不同的ADMIN_MEDIA_PREFIX
。如果不是,则ADMIN_MEDIA_PREFIX
优先。我通常通过更改ADMIN_MEDIA_PREFIX
为/admin-media/
.
回答by ars
Do you have the media context processor in your settings?
您的设置中有媒体上下文处理器吗?
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.media',
)
You might also try putting the following in your settings to see if the debug messages reveal anything:
您还可以尝试将以下内容放入您的设置中,以查看调试消息是否显示任何内容:
TEMPLATE_DEBUG = True
回答by JChen___
Please read the official Django DOC carefully and you will find the most fit answer.
请仔细阅读官方 Django DOC,您会找到最合适的答案。
The best and easist way to solve this is like below.
解决这个问题的最好和最简单的方法如下所示。
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)
The related url is: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
相关网址是:https: //docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
回答by Jesus Noland
Thanks a lot for your help everyone. I was able to solve it as
非常感谢大家的帮助。我能够解决它
settings.py -->
设置.py -->
MEDIA_ROOT = '/home/patrick/Documents/code/projects/test/media/'
MEDIA_URL = 'http://localhost:8000/media/'
urls.py -->
urls.py -->
(r'^media/(?P<path>,*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
index.html -->
index.html -->
img src="/media/images/logo.jpg"
Again, thanks a lot, I was going crazy trying to find out how to do all of this. This solution worked for me on a Django version 1.2.5 Development server running Ubuntu 10.04.
再次,非常感谢,我快疯了,试图找出如何做到这一切。该解决方案在运行 Ubuntu 10.04 的 Django 1.2.5 版开发服务器上对我有用。
回答by elad silver
I had a similar problem, I have a one-page-app and Apparently I had a newbie mistake that made django miss my media path i had this:
我有一个类似的问题,我有一个单页应用程序,显然我有一个新手错误,使 django 错过了我的媒体路径,我有这个:
url(r'^', home, name="home"),
instead of this:
而不是这个:
url(r'^$', home, name="home"),
the missing $
made my url pattern miss my media url
丢失$
使我的网址模式错过了我的媒体网址
回答by Nikhil Wagh
I was also having the same problem, this is how I got it done.
我也遇到了同样的问题,我就是这样解决的。
If you are using a FileField in your model class instead of MEDIA_URL use .url member to access the url of your file. For eg: something like this
如果您在模型类中使用 FileField 而不是 MEDIA_URL,请使用 .url 成员来访问文件的 url。例如:像这样的东西
href = "{{ some_object.file_name.url }}"
here file_name is the FileField in model class.
这里 file_name 是模型类中的 FileField。