Django 无法将图像文件嵌入用户上载的模板中

Django 无法将图像文件嵌入用户上载的模板中,django,django-templates,django-media,Django,Django Templates,Django Media,我正在尝试在模板中加载上载的图像。图片上传正确,url也正确,但我仍然收到404错误。错误是:-获取404(未找到) 图像显示在文件夹中:- 模板 {% if complaint.media %} <img src="{{ complaint.media.url }}" height="100px" width="100px" > {% endif %} 型号.py MEDIA_DIR = os.path.join(BASE_DIR,'media') MEDIA_ROOT =

我正在尝试在模板中加载上载的图像。图片上传正确,url也正确,但我仍然收到404错误。错误是:-获取404(未找到)

图像显示在文件夹中:-

模板

{% if complaint.media %}
 <img src="{{ complaint.media.url }}" height="100px" width="100px" >
{% endif %}
型号.py

MEDIA_DIR  = os.path.join(BASE_DIR,'media')
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
class Complaints(models.Model):
  media = models.ImageField(upload_to='complaints_image',blank=True)
class ComplaintForm(forms.ModelForm):

class Meta():
    model = Complaint
    fields = ('department','heading','text','media',)
forms.py

MEDIA_DIR  = os.path.join(BASE_DIR,'media')
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
class Complaints(models.Model):
  media = models.ImageField(upload_to='complaints_image',blank=True)
class ComplaintForm(forms.ModelForm):

class Meta():
    model = Complaint
    fields = ('department','heading','text','media',)
出现此错误是因为媒体文件不是自动提供的(默认情况下)。
如果您的
媒体\u URL
定义为
'/MEDIA/'
,则您可以通过将以下代码段添加到
入门级URL.py
,为媒体文件提供服务:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


阅读更多细节


注意:这种提供介质(和静态)文件的方式不适合生产使用!
有关如何处理生产服务器的更多详细信息,请阅读。

谢谢,它已修复