第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Django 在媒體文件夾中創(chuàng)建另一個(gè)媒體文件夾

Django 在媒體文件夾中創(chuàng)建另一個(gè)媒體文件夾

一只斗牛犬 2023-07-18 13:44:34
我是 Django 的初學(xué)者,這正是它的作用。我按照文檔中的說(shuō)明進(jìn)行操作,但無(wú)論如何,也許出了什么問(wèn)題?從管理頁(yè)面我添加\設(shè)置產(chǎn)品并選擇“圖像”,然后當(dāng)我保存它時(shí)創(chuàng)建縮略圖并嘗試保存在“/media/uploads/”中,但它創(chuàng)建另一個(gè)“媒體”文件夾當(dāng)站點(diǎn)路徑為“/media/uploads/img.png”時(shí),圖像存儲(chǔ)在“/media/media/uploads/img.png”中。這是代碼:/shop/settings.py:MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # or even 'media/'/shop/urls.py:urlpatterns = [    ...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)添加后,然后在我的產(chǎn)品模型中我這樣做:/apps/store/models.py:class Product(models.Model):    ...    image = models.ImageField(upload_to='media/uploads/', blank=True, null=True)    thumbnail = models.ImageField(upload_to='media/uploads/', blank=True, null=True)    ...    def save(self, *args, **kwargs):        self.thumbnail = self.make_thumbnail(self.image)        super().save(*args, **kwargs)    @staticmethod    def make_thumbnail(image, size=(512, 512)):        if not image:            return        img = Image.open(image)        if img.mode in ('RGBA',):  # converting image to RGB if it's RGBA            img.load()            rgb_convert = Image.new('RGB', img.size, 0)            rgb_convert.paste(img, mask=img.split()[3])            img = rgb_convert        img.thumbnail(size)        thumb_io = BytesIO()        img.save(thumb_io, 'PNG', quality=80)        thumb = File(thumb_io, name=image.name)        return thumb我嘗試將“upload_to”更改為“uploads/”,然后它以正確的方向存儲(chǔ)文件,但現(xiàn)場(chǎng)路徑也更改為“/uploads/img.png”,而它必須是“/media/uploads/img” .png'有什么問(wèn)題嗎?謝謝!
查看完整描述

2 回答

?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

參數(shù)upload_to=…[Django-doc]是相對(duì)于MEDIA_ROOT.?因此,如果您想將其存儲(chǔ)在目錄uploads中的某個(gè)目錄中media,請(qǐng)使用以下命令上傳:

class?Product(models.Model):
????#?…
????thumbnail?=?models.ImageField(upload_to='uploads/',?blank=True,?null=True)

然而,為了呈現(xiàn)URL,您需要使用.url屬性 [Django-doc],因此:

{%?if?p.thumbnail?%}
????<img?src="{{?p.thumbnail.url?}}">
{%?endif?%}

這里有{% if p.thumbnail %}必要檢查NULL/None值。


查看完整回答
反對(duì) 回復(fù) 2023-07-18
?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊

這是因?yàn)槟銓懥恕皍pload_to='media/uploads'”..從你的設(shè)置中django將創(chuàng)建媒體文件夾,并且由于你寫了“media/uploads”,uploads位于媒體文件夾內(nèi),該文件夾位于設(shè)置中聲明的主媒體內(nèi)。所以就寫


class Product(models.Model):

thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True)


查看完整回答
反對(duì) 回復(fù) 2023-07-18
  • 2 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)