【发布时间】: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.py
def 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模型和你得到的错误的完整堆栈跟踪 -
我已经编辑了帖子。谢谢
标签: html django url redirect django-templates