[英]FilePathField for Image Path not working with Django Admin Panel
New to Django and web development in general.一般是 Django 和 web 开发的新手。 But I have followed a tutorial for creating my first site.但我遵循了创建我的第一个网站的教程。 Everything worked fine, until I finished it and wanted to edit the info using the Django admin panel.一切正常,直到我完成它并想使用 Django 管理面板编辑信息。
The main issue is that I have a model shown below that uses FilePathField to describe the image locations.主要问题是我有一个 model 如下所示,它使用 FilePathField 来描述图像位置。 The tutorial had me place the images in /home/pi/mysite/projects/static/img
.该教程让我将图像放在/home/pi/mysite/projects/static/img
中。
from django.db import models
class Project(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
technology = models.CharField(max_length=20)
image = models.FilePathField('/img')
def __str__(self):
return self.title #or self.title for blog posts
This worked fine at first for displaying the images on the website, but once I tried to navigate to the /change/
url from the admin panel, I would get FileNotFoundError at /admin/projects/project/1/change/ [Errno 2] No such file or directory: '/img'
起初,这在网站上显示图像时效果很好,但是一旦我尝试从管理面板导航到/change/
url,我会FileNotFoundError at /admin/projects/project/1/change/ [Errno 2] No such file or directory: '/img'
So I looked around and found that FilePathField is an absolute path, so tried defining it absolutely with /home/pi/mysite/projects/static/img/
.所以我环顾四周,发现 FilePathField 是一个绝对路径,所以尝试用/home/pi/mysite/projects/static/img/
绝对定义它。 This stopped the error from occurring which let me at least access the /change/
url from the admin panel.这阻止了错误的发生,这让我至少可以从管理面板访问/change/
url。 The panel now showed the image field with a dropdown that I could use to choose between the different images I had up there, which were located at /home/pi/mysite/projects/static/img/
.面板现在显示了带有下拉列表的图像字段,我可以使用它在上面的不同图像之间进行选择,这些图像位于/home/pi/mysite/projects/static/img/
。 But from the admin panel, once I hit save, the images would no longer show up on the site.但是在管理面板中,一旦我点击保存,图像将不再显示在网站上。 Upon using inspect element on the website, it seems it was trying to pull the images from /static/projects/static/img
.在网站上使用检查元素后,它似乎试图从/static/projects/static/img
中提取图像。 I then tried to move the images to /static/projects/static/img
but then they no longer showed up in the dropdown box from the admin panel, and also weren't displaying on the website.然后我尝试将图像移动到/static/projects/static/img
但它们不再显示在管理面板的下拉框中,也没有显示在网站上。
So at this point I am just a bit confused where I should be pointing FilePathField too that will allow me to display the images on the website and also change them through the admin panel.所以在这一点上,我有点困惑,我应该在哪里指向 FilePathField,这将允许我在网站上显示图像并通过管理面板更改它们。
A much better approach is to use django's FileField
like so:更好的方法是像这样使用 django 的FileField
:
image = models.FileField(null=True, blank=True)
In your settings.py
you need to include the media path.在您的settings.py
,您需要包含媒体路径。 Check you have this in your settings.检查你的设置中有这个。
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
Last step is to extend the urlpatterns in your urls.py:最后一步是扩展 urls.py 中的 urlpatterns:
urlpatterns = [
# your paths
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will generate a /media/
directory where your uploaded media files will point to.这将生成一个/media/
目录,您上传的媒体文件将指向该目录。 Then in your frontend you can simply use the model property image.url
to get the absolute url of the image.然后在您的前端,您可以简单地使用 model 属性image.url
来获得图像的绝对 url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.