Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:09:21+00:00 2026-05-21T17:09:21+00:00

I am trying to filter an image through a category. It works but not

  • 0

I am trying to filter an image through a category. It works but not the way I want it to. Here are how my models are setup:

class Image(models.Model):
    CATEGORY_CHOICES = (
    ('Cover', 'Cover'),    
    ('Scan', 'Scan'),
    ('Other', 'Other'),
    )
    title = models.CharField(max_length=256)
    image = models.ImageField(upload_to="images/")
    category = models.CharField(max_length=10, choices=CATEGORY_CHOICES)
    contributor = models.ManyToManyField(Contributor, blank=True, null=True)
    date_added = models.DateField(auto_now_add=True)    
    def __unicode__(self):
        return self.title
    class Meta:
        ordering = ['title']


class Issue(models.Model):

    ...

    images = models.ManyToManyField(Image, related_name="images_inc", blank=True, null=True)

    ....

    def __unicode__(self):
        return u'%s #%s' % (self.title, self.number)
    def get_absolute_url(self):
        return "/issues/%s" % self.slug     
    class Meta:
        ordering = ['-date_added']

Views.py:

def issue(request, issue_slug):
    issue = get_object_or_404(Issue, slug=issue_slug)
    cover = Image.objects.filter(category='Cover')
    scan = Image.objects.filter(category='Scan')
    return render_to_response('comics/issue_detail.html', {'issue': issue, 'cover': cover, 'scan': scan}, context_instance=RequestContext(request))

I am trying to filter it by Cover or Scan, as you can see, but when I put this into my template:

{{ cover }} or {{ scan }}

It returns:

<Image: Astonishing X-Men 1 Cover A>] [<Image: Astonishing X-Men 1, teamshot>]

I need it to return the image URL, obviously. Adding {{ cover.url }} doesn’t work.

Oh, and I just realized it does not display the specific image that is in the issue. It displays ALL images that categorized as either Scan or Cover.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:09:22+00:00Added an answer on May 21, 2026 at 5:09 pm

    In your current code, you’re returning all the images, even the ones that aren’t associated with your chosen comic issue. I’m assuming you want the ones for the selected issue. You probably want to do this instead:

    def issue(request, issue_slug):
        issue = get_object_or_404(Issue, slug=issue_slug)
        try:
            cover = issue.images_inc.filter(category='Cover')[0]
        except IndexError:
            cover = None
        try:
            scan = issue.images_inc.filter(category='Scan')[0]
        except IndexError:
            scan = None
        return render_to_response('comics/issue_detail.html', {'issue': issue, 'cover': cover, 'scan': scan}, context_instance=RequestContext(request))
    

    Then, in the template:

    {% if cover.image %}
        <img src="{{ cover.image.url }}" alt="{{ cover.image.title }}" />
    {% endif %}
    
    {% if scan.image %}
        <img src="{{ scan.image.url }}" alt="{{ scan.image.title }}" />
    {% endif %}
    

    Update

    If you do want more than one cover or scan, then you don’t want to get only the first item. And really, you should probably just move this logic to models.py to avoid possible code duplication.

    Something like this would be better:

    # models.py
    
    
    class Issue(models.Model):
        ...
        def scans(self):
            return self.images.filter(category='Scan')
    
        def covers(self):
            return self.images.filter(category='Cover')
    
    # views.py
    
    def issue(request, issue_slug):
        issue = get_object_or_404(Issue, slug=issue_slug)
        return render_to_response('comics/issue_detail.html', {'issue': issue }, context_instance=RequestContext(request))
    

    Then, in the template:

    <ul>
    {% for cover in issue.covers %}
        <li><img src="{{ cover.image.url }}" alt="{{ cover.image.title }}" /></li>
    {% empty %}
        <li>No cover</li>
    {% endfor %}
    </ul>
    
    <ul>
    {% for scan in issue.scans %}
        <li><img src="{{ scan.image.url }}" alt="{{ scan.image.title }}" /></li>
    {% empty %}
        <li>No scans</li>
    {% endfor %}
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 models: class Team(models.Model): Team model for creating a basic team. image
i'm trying to show image with grayscale-filter. Here is my code: $images = glob('gallery/*small*');
I'm trying to perform a Median filter on an image in Java but it's
I'm trying to filter through some images. Each image has 3 sets of values
im trying to create an image filter in OpenGL CE. currently I am trying
I'm trying to filter a list of elements via ':not()', and jQuery seems to
I am trying to display an image using OpenGL ES through android NDK.. What
I am trying to filter an image with out using imfilter . I should
I have been trying to call filter css method from jquery for IE, but
I am trying to setup my application to use Spring and also an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.