热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Django中的ImageField1.5.4—处理图像的路径-ImageFieldinDjango1.5.4—dealingwiththeimages'paths

IamusingDjango1.5.4andIhaveaprojectwiththefollowingstructure:我使用的是Django1.5.4,我有一个项目的

I am using Django 1.5.4 and I have a project with the following structure:

我使用的是Django 1.5.4,我有一个项目的结构如下:

django_site
    -django_site
    -books # the app
        -media
            -images
                -books
                -authors
        -static
            -images
    -templates

This is the necessary code:

这是必要的守则:

# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'

# models.py
class Book(models.Model):
    image = models.ImageField(upload_to="images/books/")

# urls.py
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^books/$', 'books.views.get_all_books'),
)

My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.

我的问题是,在通过admin站点添加图像之后,如果我点击上传按钮下面的图像链接,会出现一个404页面,抱怨这条路径不存在。

The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.

请求URL为http://127.0.1:8000 /media/books/media/image /book/out_of_control_1。但实际上我希望结果是/root/django_site/books/media/images/out_of_control_1.JPG。

How do I fix that? Looking forward to your responses.

我该怎么做呢?期待您的回复。

1 个解决方案

#1


2  

Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT

由于您似乎使用了开发服务器,所以我认为您的问题与这个问题有关:Django MEDIA_URL和MEDIA_ROOT

As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.

正如答案中所解释的,当调试被设置为True时,您必须为媒体处理设置一个特定的URL模式。

You can also have a look at the docs regarding this question.

你也可以看看关于这个问题的文档。


推荐阅读
author-avatar
Liushan2502897753
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有