简体   繁体   English

媒体网址返回错误500 django

[英]Media Url returns error 500 django

have the following problem, I have in my settings.py configured in the following way: 有以下问题,我在settings.py中以以下方式配置:

RUTA_PROYECTO = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(RUTA_PROYECTO,'fotos')
MEDIA_URL = '/media/'

In my urls file I have: 在我的网址文件中,我有:

from django.conf.urls.static import static

Urlpatterns = patterns ('',
....
) + static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

I have in my models 我的模特儿有

def upload_name(instance, filename):
return '{}/{}'.format(instance.persona.nro_doc, filename)

class FotosPersona(models.Model):
    persona        = models.ForeignKey(Personas,related_name='fotos_persona')
    tipo_foto      = models.CharField(max_length=25)
    foto           = models.ImageField(upload_to=upload_name)
    fecha          = models.DateField(auto_now=True)

    class Meta:
        db_table = 'fotos_persona'

When I save the data, the image is stored in a folder within the following structure: 当我保存数据时,图像存储在以下结构内的文件夹中:

Project
 |
 |
 |----Project
         |---- settings.py
         |---- media
         |---- urls.py
         .....

But when I want to display the image in a template, it is not displayed. 但是,当我想在模板中显示图像时,它不会显示。

When in the browser I entered the 在浏览器中输入

http://localhost:8000/media/other_folder/image_file.jpg url http:// localhost:8000 / media / other_folder / image_file.jpg网址

it returns me an HTTP 500 error. 它返回我一个HTTP 500错误。

I know my answer is a little late, but this might be useful considering your set up. 我知道我的回答有点晚了,但是考虑到您的设置,这可能会很有用。 Essentially, you have given the MEDIA_URL as "/media/". 本质上,您已将MEDIA_URL指定为“ / media /”。 This is all right. 没关系

However, you would be facing a problem with reference to the URL patterns. 但是,您将在参考URL模式时遇到问题。 You will probably have a generic URL along the lines of: 您可能会具有以下通用网址:

path('<index>', views.index, name='index'),

In such a scenario this URL would be processing everything that comes from the media URL. 在这种情况下,此URL将处理来自媒体URL的所有内容。 Although this is not ideal, only for the purpose of development I would recommend changing your URL patterns to the following: 尽管这不是理想的选择,但出于开发目的,我建议将您的URL模式更改为以下内容:

urlpatterns = static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) + [
    ...
    ]

Now in the hierarchy of processing URL patterns "/media/" would come before any URL that has a general pattern. 现在,在处理URL模式的层次结构中,“ / media /”将出现在具有常规模式的所有URL之前。 Now media URLs would be processed before everything else. 现在,媒体URL将先处理。

Hopefully, this can address your query. 希望这可以解决您的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM