![在这里插入图片描述](https://img-blog.csdnimg.cn/20210525160904850.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210525162455302.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jqc3ljMTIzNDU2,size_16,color_FFFFFF,t_70)
from collections import OrderedDict
from apps.goods.models import GoodsChannel
def get_categories():
channels = GoodsChannel.objects.order_by('group_id', 'sequence')
categories = OrderedDict()
for channel in channels:
group_id = channel.group_id
if group_id not in categories:
categories[group_id] = {
'channels': [],
'sub_cats': []
}
category1 = channel.category
categories[group_id]['channels'].append({
'id': category1.id,
'name': category1.name,
'url': channel.url
})
category2s = category1.subs.all()
for cat2 in category2s:
category3s = cat2.subs.all()
sub_cats3 = []
for cat3 in category3s:
sub_cats3.append({
'id': cat3.id,
'name': cat3.name
})
categories[group_id]['sub_cats'].append({
'id': cat2.id,
'name': cat2.name,
'sub_cats': sub_cats3
})
return categories
from utils.goods import get_categories
class IndexView(View):
def get(self, request):
categories = get_categories()
contents = {}
content_categories = ContentCategory.objects.all()
for cat in content_categories:
contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')
context = {
'categories': categories,
'contents': contents
}
return render(request, 'index.html', context)
-
图片不显示问题
url没有拼接正确
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210525163104621.png)
-
新建storage.py,自定义存储类
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021052516312818.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jqc3ljMTIzNDU2,size_16,color_FFFFFF,t_70)
from django.core.files.storage import Storage
from meiduo_market import settings
class FastDFSStorage(Storage):
def _open(self, name, mode='rb'):
pass
def _save(self, name, content):
pass
def url(self, name):
return settings.FDFS_BASE_URL + name
DEFAULT_FILE_STORAGE = 'utils.fastdfs.storage.FastDFSStorage'
FDFS_BASE_URL = 'http://192.168.85.129:8888/'