user514310
user514310

Reputation:

Want to hide error message (View)

I'm matching the name in Album Model. And Its working just fine. But Some time It does not match. And It raise error message Album matching query does not exist. I want if no match is found. Then don't display any error message. Is it possible? Thanks

   def movie_details(request,slug):
    movies = get_object_or_404(Movie,slug=slug)
    # calculating wikipedia directory structer for images
    #image_name = movies.image
    name = movies.title
    album_name = Album.objects.get(name__exact=name)
    album_id = album_name.id
    song_list = Song.objects.filter(album=album_id)
    #image_path = generateWikiImage(image_name)
    #
    return render_to_response('base_movies.html',locals(),
                              context_instance=RequestContext(request)
                              )

Error message

Album matching query does not exist

UPDATE:

def movie_details(request,slug):
movies = get_object_or_404(Movie,slug=slug)
# calculating wikipedia directory structer for images
#image_name = movies.image
name = movies.title
try:
    album_name = Album.objects.get(name__exact=name)
    album_id = album_name.id
    song_list = Song.objects.filter(album=album_id)
except:
    pass

Thanks F.C.

Upvotes: 1

Views: 208

Answers (1)

bouteillebleu
bouteillebleu

Reputation: 2493

As suggested in the comment above, you need to catch the exception that's raised. In your case, it'd be:

try:
    album_name = Album.objects.get(name__exact=name)
except Album.DoesNotExist:
    pass

If you want to set album_name to some default value if you can't find it, replace pass with code to set it to your default value.

(This StackOverflow post also deals with a similar issue.)

Upvotes: 1

Related Questions