我想根据自己的观点显示图片。例如,如果我的数据库中有一个用户值为“ 100”,那么我的网站会向我显示一个特定的图像,如果该值不是“ 100”,则会显示另一个图像。我用“打印”编写了一个简单的代码,但是我不知道如何用图像替换“打印”,然后将其显示在页面上。
这是我的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')
答案 0 :(得分:1)
您可以像下面这样
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 %}
与您的图片相对应的网址。