117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
![]() |
#!/usr/bin/env python
|
|||
|
"""
|
|||
|
测试达人头像显示功能
|
|||
|
演示如何使用本地图片和外部URL
|
|||
|
"""
|
|||
|
|
|||
|
import os
|
|||
|
import django
|
|||
|
|
|||
|
# 设置Django环境
|
|||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'daren.settings')
|
|||
|
django.setup()
|
|||
|
|
|||
|
from apps.daren_detail.models import CreatorProfile
|
|||
|
from django.core.files import File
|
|||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|||
|
|
|||
|
def test_avatar_display():
|
|||
|
"""测试头像显示功能"""
|
|||
|
print("=== 达人头像显示功能测试 ===\n")
|
|||
|
|
|||
|
# 1. 查询现有达人
|
|||
|
creators = CreatorProfile.objects.all()[:3]
|
|||
|
|
|||
|
print("1. 现有达人头像状态:")
|
|||
|
for creator in creators:
|
|||
|
avatar_url = creator.get_avatar_url()
|
|||
|
has_local = bool(creator.avatar)
|
|||
|
has_external = bool(creator.avatar_url)
|
|||
|
|
|||
|
print(f" - {creator.name}:")
|
|||
|
print(f" 本地图片: {'✓' if has_local else '✗'}")
|
|||
|
print(f" 外部URL: {'✓' if has_external else '✗'}")
|
|||
|
print(f" 显示URL: {avatar_url or '无'}")
|
|||
|
print()
|
|||
|
|
|||
|
# 2. 演示URL访问方式
|
|||
|
print("2. 头像URL访问示例:")
|
|||
|
for creator in creators:
|
|||
|
avatar_url = creator.get_avatar_url()
|
|||
|
if avatar_url:
|
|||
|
if creator.avatar:
|
|||
|
print(f" 本地图片: http://localhost:8000{avatar_url}")
|
|||
|
else:
|
|||
|
print(f" 外部URL: {avatar_url}")
|
|||
|
else:
|
|||
|
print(f" {creator.name}: 无头像")
|
|||
|
print()
|
|||
|
|
|||
|
# 3. 创建测试数据示例
|
|||
|
print("3. 创建测试达人示例:")
|
|||
|
|
|||
|
# 示例1:仅外部URL
|
|||
|
creator1, created = CreatorProfile.objects.get_or_create(
|
|||
|
name="测试达人A",
|
|||
|
defaults={
|
|||
|
'avatar_url': 'https://example.com/avatar1.jpg',
|
|||
|
'category': 'Beauty & Personal Care',
|
|||
|
'followers': 1000
|
|||
|
}
|
|||
|
)
|
|||
|
print(f" - {creator1.name}: {creator1.get_avatar_url()}")
|
|||
|
|
|||
|
# 示例2:仅本地图片(如果存在的话)
|
|||
|
existing_avatar = CreatorProfile.objects.filter(avatar__isnull=False).first()
|
|||
|
if existing_avatar:
|
|||
|
print(f" - {existing_avatar.name}: {existing_avatar.get_avatar_url()}")
|
|||
|
|
|||
|
print("\n=== 前端使用示例代码 ===")
|
|||
|
print("""
|
|||
|
// JavaScript: 获取并显示头像
|
|||
|
fetch('/api/daren_detail/creators/')
|
|||
|
.then(response => response.json())
|
|||
|
.then(data => {
|
|||
|
data.results.forEach(creator => {
|
|||
|
if (creator.avatar_display_url) {
|
|||
|
console.log(`${creator.name}: ${creator.avatar_display_url}`);
|
|||
|
|
|||
|
// 创建图片元素
|
|||
|
const img = document.createElement('img');
|
|||
|
img.src = creator.avatar_display_url;
|
|||
|
img.alt = `${creator.name}的头像`;
|
|||
|
img.className = 'creator-avatar';
|
|||
|
|
|||
|
// 添加到页面
|
|||
|
document.getElementById('creators-list').appendChild(img);
|
|||
|
}
|
|||
|
});
|
|||
|
});
|
|||
|
""")
|
|||
|
|
|||
|
print("\n=== HTML模板使用示例 ===")
|
|||
|
print("""
|
|||
|
<!-- Django模板中使用 -->
|
|||
|
{% for creator in creators %}
|
|||
|
<div class="creator-card">
|
|||
|
<h3>{{ creator.name }}</h3>
|
|||
|
{% if creator.get_avatar_url %}
|
|||
|
<img src="{{ creator.get_avatar_url }}"
|
|||
|
alt="{{ creator.name }}的头像"
|
|||
|
class="avatar">
|
|||
|
{% else %}
|
|||
|
<div class="no-avatar">暂无头像</div>
|
|||
|
{% endif %}
|
|||
|
</div>
|
|||
|
{% endfor %}
|
|||
|
""")
|
|||
|
|
|||
|
if __name__ == "__main__":
|
|||
|
try:
|
|||
|
test_avatar_display()
|
|||
|
except Exception as e:
|
|||
|
print(f"测试出错: {e}")
|
|||
|
print("请确保:")
|
|||
|
print("1. 已运行 python manage.py migrate")
|
|||
|
print("2. 数据库中有达人数据")
|
|||
|
print("3. media/avatars/ 目录存在")
|