Reputation: 11
I'm getting this error when trying to get the image attribute
and set some values according to the content of service_name
attribute,
both service_name
and image_url
are attributes of the same model,
but I want to set the image according to the content service_name
using condition
statements. Any help or other way of doing it is highly appreciated
Here is the models module:
class JobServices(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
service_name = models.CharField(max_length=100, default="nom du service")
hour = models.IntegerField(default="Heures")
amount = models.FloatField(default="Cout")
service_date = models.DateTimeField(auto_now_add=True, null=True)
description = models.CharField(max_length=2000, null=True, default="annonce")
image_url = models.CharField(max_length=2000, null=True, blank=True)
image = models.ImageField()
And here is the view module
def taches(request):
taches = JobServices.objects.all()
if request.user.is_authenticated:
service_name = request.POST.get('service_name')
image_url = request.POST.get('image_url')
if service_name == 'don du sang':
image_url = 'https://images.pexels.com/photos/4226902/pexels-photo-4226902.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'
for object in taches:
object.save()
elif service_name == 'cours de soutien':
image_url = 'https://images.pexels.com/photos/1181529/pexels-photo-1181529.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'
for object in taches:
object.save()
elif service_name == 'jardinage':
image_url = 'https://images.pexels.com/photos/4505166/pexels-photo-4505166.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'
for object in taches:
object.save()
elif service_name == 'reparation':
image_url = 'https://www.pexels.com/photo/gray-scale-photo-of-gears-159298/'
for object in taches:
object.save()
else:
image_url = 'https://images.pexels.com/photos/8080823/pexels-photo-8080823.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'
for object in taches:
object.save()
return render(request, 'taches.html', {'taches': taches})
else:
return redirect('login')
Taches.html:
div class="container">
<h2>{{ user }}, voici les tâches disponibles pour vous</h2>
<div class="row">
{% for tache in taches %}
<div class="col">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{ tache.image_url }}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><b></b>{{ tache.service_name }}</h5>
<b class="card-text"><b></b>{{ tache.amount }} VT pour {{ tache.hour }} heures </b></p>
<a href="#" class="btn btn-primary">Postuler</a>
</div>
</div>
</div>
{% endfor %}
</div>
Upvotes: 0
Views: 755
Reputation: 1398
Your queryset isn't a single object, you'll want to use the update method since you're dealing with multiple objects or a for loop.
or
for object in taches:
object.image_url = 'URL'
object.save()
In your template change :
<img class="card-img-top" src="{{ tache.image_url }}" alt="Card image cap">
to
<img class="card-img-top" src="{{ tache.image_url.url }}" alt="Card image cap">
Upvotes: 1