Django - filtering objects with a field that has a value with spaces in it not working?

StackOverflow https://stackoverflow.com/questions/23092380

  •  04-07-2023
  •  | 
  •  

Question

I'm trying to get urls working in django to display all items that have a field equal a certain value. The field on my model is a charfield, like this:

class Episode(models.Model):
    series = models.CharField(max_length=200, blank=True)

I am not using slugfields or anything like that (I don't know what the point of them is?), and I cannot seem to get filtering working when the value of the series field has spaces in it. My view looks like this:

def series(request, slug):
    print slug
    tag = slug.replace("-", " ")
    print tag 
    print Episode.objects.filter(series=tag)

    return render(request, "taggedpage.html", {   
        "episodes": Episode.objects.filter(series=tag),
        "tag": tag

        })

my url pattern:

url(r'^series/(?P<slug>[-\w]+)/$','galleries.views.series', name='series'),

For example, if the series field is equal to "the sopranos", trying to visit the url

http://mytvsite.com/series/the-sopranos/

returns no objects. However, if the field is only one word, it works fine, e.g if the series was "house", visiting

http://mytvsite.com/series/house/

would return all the objects with a series: field value of house. Is this some regex/slug thing?

Was it helpful?

Solution

I don't know what was causing the problem exactly, but I installed django-autoslug and it all works now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top