Django 图像 url 重定向是通过 HTML 模板的链接而不是媒体文件?
Posted
技术标签:
【中文标题】Django 图像 url 重定向是通过 HTML 模板的链接而不是媒体文件?【英文标题】:Django image url redirect is going through link for HTML template rather than media file? 【发布时间】:2021-05-18 18:22:48 【问题描述】:我有一个 Django 应用程序,一个包含图像的模板。我知道的问题是服务器试图通过来自 html 模板而不是媒体文件的重定向链接显示图像。
例子:
<tbody>
% for equipment in inventory_obj %
<tr>
<td class="table-blocks"> equipment.id </td>
<td class="table-blocks"> equipment.name </td>
<td class="table-blocks"> equipment.total_quantity </td>
<td class="table-blocks"> equipment.location </td>
<td class="table-blocks"><a href= " equipment.img_reference ">IMAGE</a></td>
<td class="table-blocks">
<a href="/EditInventory/ equipment.id " id="Edit-Page-Link">Edit</a>
</td>
</tr>
% endfor%
但是当我添加 equipment.img_reference.url
时 - 应用程序返回属性错误,指出未找到文件。
equipment.img_reference
也有问题,当我点击链接时,应用程序通过 HTML 应用程序打开图像 - url 为:
localhost/ViewInventory/images_uploaded/WIN_20210215_19_54_16_Pro.jpg
- 什么时候应该是这样的:localhost/media/images_uploaded/WIN_20210215_19_54_16_Pro.jpg
这是应用程序和项目的 urls.py
文件:
应用程序:
from django.conf import settings
from django.conf.urls.static import static
from . import views # . is from 'this app' import views
urlpatterns = [
path("", views.homepage, name="home"), #name must be same as the function name in the views.py, the path('') in the urls.py of mysite looks for the same path in '' and then renders the page which follows that
path("LossReport/", views.report_loss, name="loss_report_page"),
path("AddInventory/", views.add_new_to_inventory, name="add_new_to_inventory"),
path("AddPractical/", views.add_new_practical, name="add_new_practical"),
path("EditPractical/", views.edit_practical, name="edit_practical"),
path("ViewInventory/", views.view_inventory, name="view_inventory"),
path("EditInventory/<int:id>", views.edit_inventory, name="edit_inventory"),
path("Update/<int:id>", views.update, name="update"),
]
项目:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('inventory.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这里是 settings.py
文件,我在其中添加了媒体根目录和媒体 URL
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
老实说,在我开始创建新模板之前,这一切都很好。我已经撤消了对应用程序所做的所有更改,希望这会起作用 - 但它没有:/
编辑:
这是其他文件:
views.pydef view_inventory(request):
inventory_obj = Inventory_Equipment.objects.all()
return render(request,"main/ViewInventory.html", 'inventory_obj': inventory_obj)
models.py
class Inventory_Equipment(models.Model):
id = models.IntegerField(primary_key=True, null=False, blank=True)
name = models.CharField(max_length=255, blank=True, null=True, default="Name this Equipment") # might have to divide this into name from frop down and new name
total_quantity = models.IntegerField(null=True, blank=True) # qty to add an entirely new equipment - bottom of form
location = models.CharField(max_length=20, default='Physics Office', blank=True)
img_reference = models.ImageField(null=True, blank=True, upload_to='images_uploaded/')
class Meta:
db_table="inventory"
def __str__(self):
return self.name
forms.py
from django import forms
from .models import Inventory_Equipment
class Add_Inventory_Form(forms.ModelForm):
new_quantity = forms.IntegerField(required = False, widget=forms.HiddenInput(), initial=0)
existing_name = forms.CharField(max_length=255, required = False)
class Meta:
model = Inventory_Equipment
fields = "__all__"
这是给定的错误:
请求方法:GET 请求网址:http://127.0.0.1:8000/ViewInventory/ Django 版本:3.1.3 异常类型:ValueError 异常值: 'img_reference' 属性没有与之关联的文件。
目前它不会渲染这个模板,因为模板文件有equipment.img_reference.url
而不是equipment.img_reference
。
【问题讨论】:
请包括equipment
模型和你得到的错误的完整堆栈跟踪
我已经编辑了帖子。谢谢
【参考方案1】:
在模板中试试这个:-
% for equipment in inventory_obj %
<tr>
<td class="table-blocks"> equipment.id </td>
<td class="table-blocks"> equipment.name </td>
<td class="table-blocks"> equipment.total_quantity </td>
<td class="table-blocks"> equipment.location </td>
% if equipment.img_reference %
<td class="table-blocks"><a href= " equipment.img_reference ">IMAGE</a></td>
% endif %
<td class="table-blocks">
<a href="/EditInventory/ equipment.id " id="Edit-Page-Link">Edit</a>
</td>
</tr>
% endfor%
【讨论】:
这不起作用:/虽然正在渲染表格并且没有属性错误,但链接仍然重定向到错误的URL。以上是关于Django 图像 url 重定向是通过 HTML 模板的链接而不是媒体文件?的主要内容,如果未能解决你的问题,请参考以下文章
如果在 django 的 urls.py 中找不到,则将任何 url 重定向到 404.html