Django:如何根据 views.py 中的函数显示不同的图像

Django: How display a different image according to my function in views.py

我想根据我的观点显示图像。例如,如果我的数据库中有一个用户的值“100”,我的网站会显示一个特定的图像,如果该值不是“100”,它会显示另一个图像。我用 "print" 写了一个简单的代码,但我不知道如何用图像替换 "print" 然后在我的页面上显示它。

这是我的views.py

def Test(request):
if request.user.is_authenticated:
    try:
        name = coach.objects.get(user=request.user)
    except coach.DoesNotExist:
        name = coach(user=request.user)
    objs = Lesson.objects.all().filter(mycoach = name)
    args = {'objs': objs}
    if name.monday=="100":
        print("Monday")
    else:
        print("Not Monday")
    messages.add_message(request, messages.INFO, 'Welcome to your profile')
    return render(request, 'test.html', args)
else:
    return render(request, 'home.html')

你可以像下面那样做

def Test(request):
  if request.user.is_authenticated:
    try:
        name = coach.objects.get(user=request.user)
    except coach.DoesNotExist:
        name = coach(user=request.user)
    objs = Lesson.objects.all().filter(mycoach = name)
    args = {'objs': objs}
    if name.monday=="100":
       args['image_url'] = 'https://www.google.com/photos/about/static/images/google.svg' 
    else:
        args['image_url'] = 'http://cdn1.itpro.co.uk/sites/itpro/files/styles/article_main_wide_image/public/images/dir_248/it_photo_124192.jpg?itok=ey4UVXkQ'
    messages.add_message(request, messages.INFO, 'Welcome to your profile')
    return render(request, 'test.html', args)
else:
    return render(request, 'home.html')

home.html

{% if image_url %}
    <img src="{{image_url}}" alt=""/>
{% endif %} 

将图片网址替换为您的。