用 Django 自带后台,做上传图片功能。预览的时候打不开。
图片正确的路径是:
http://127.0.0.1:8000/media/img/2022/02/13/%E6%9C%AA%E6%A0%87%E9%A2%98-1_z6YBLvG.jpg
后台超级链接的路径是:
http://127.0.0.1:8000/admin/web/detectioninfo/4/change/media/img/2022/02/13/%E6%9C%AA%E6%A0%87%E9%A2%98-1_z6YBLvG.jpg
还没学明白,不知道哪里的问题,求指点
url.py 的 urlpatterns
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
class DetectionInfo(models.Model):
img_1 = models.ImageField(verbose_name="现场图", upload_to='img/%Y/%m/%d',blank=True,null=True)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = 'media/'
1
uasier 2022-04-05 18:25:39 +08:00
import time
from django.db import models # Create your models here. def image_upload_to(instance, filename): type = filename.split(".")[-1] now_time = time.strftime('%Y/%m/%d/%H%M%S', time.localtime()) return 'image/{now_time}.{type}'.format(now_time=now_time, type=type) class Image(models.Model): """ 图片文件表 """ objects = models.Manager() username = models.CharField(max_length=128, blank=False, default="") # 用户名 user_ip = models.CharField(max_length=16, blank=False, default="") # ip img_name = models.CharField(max_length=128, blank=False, default="") # 图片文件名 img = models.ImageField(upload_to=image_upload_to) # 图片存储地址 created_time = models.DateTimeField(auto_now_add=True) # 记录创建时间 class Meta: """ 对当前表进行相关设置: """ ordering = ('id',) db_table = 'image' verbose_name = '图片表' verbose_name_plural = verbose_name |
2
grimpil 2022-04-05 18:56:29 +08:00
先确认一下 BASE_DIR 是啥吧
|
3
veoco 2022-04-05 19:10:51 +08:00
改成 MEDIA_URL = '/media/'
|
4
maloneleo88 OP @veoco 好了!就一个"/" 我 R , 搁浅了两个月,又来学, 就一个 / 我 R !!!!! 泪牛满面- -!!!
|
5
din4 2022-04-15 00:27:31 +08:00
这几天新建 app 也是这样 搞了半天发现自己 settings.py 里面路径忘加 /
|