Reputation: 179
I created a Python-Django
web application.I set an album_logo field in models.py file and I tried to upload an image url through python shell using the code
>>>from music.models import Album, Song
>>>Album.objects.all()
[]
>>>a = Album(artist="Taylor Swift", album_title="Red", genre=" Country", album_logo="http://carywebdesigns.com/wp-content/uploads/2015/02/www2.jpg")
>>>a.save()
>>>a.artist
'Taylor Swift'
>>>a.album_title
'Red'
>>>a.id
1
>>>a.pk
1
Models.py
from django.db import models
# Create your models here.
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre = models.CharField(max_length=100)
album_logo = models.CharField(max_length=1000)
def __str__(self):
return self.album_title + ' - ' + self.artist
And I run this codes using python manage.py runserver
,I got the correct answer.But the image was not loaded.
And I have 2 template files.
index.html
{% if all_albums %}
<h3>Here are all my Albums:</h3>
<ul>
{% for album in all_albums %}
<li><a href="/music/{{ album.id }}/">{{ album.album_title }}</a></li>
{% endfor %}
</ul>
{% else %}
<h3>You don't have any albums</h3>
{% endif %}
detail.html
<img src="{{ album.album_logo }}">
<h1>{{ album.album_title }}</h1>
<h3>{{ artist }}</h3>
<ul>
{% for song in album.song_set.all %}
<li>{{ song.song_title }} - {{ song.file_type }}</li>
{% endfor %}
How can I solve this problem.Please help me.Thanks in advance..
Upvotes: 0
Views: 199
Reputation: 2212
For example you need to create the list of objects of Album
model:
def listAlbum(request):
album_list = Album.objects.all()
return render(request, '/your/template.html', {'album_list':album_list})
in this case to show the image of objects in template it should look like this:
{% for a in album_list %}
<img src="{{a.album_logo}}>
{% endfor %}
If you want to display detail object, then your view should look like this:
def detailAlbum(request, pk):
album = Album.objects.get(pk=pk)
return render(request, '/your/template.html', {'album':album})
In this case your template should looks like this:
<img src="{{a.album}}>
Hope it helps you
Upvotes: 1