1

Error: ValueError at /followed/n/

Actually i don't get why this error appear, i've tried using int() and float() but, anything seems to work

views.py

def followed(request, follow_to):
    return render(request, "test.html",{'following':Following.objects.get(follow_to=follow_to),
        'selfieList':Selfie.objects.filter(selfie_user=follow_to),})

template

{% for f in following %}
        <a href="{% url 'followed' f.follow_to %}">{{f.follow_to}}</a> <br>
    {% endfor %}

urls.py

url(r'^followed/(?P<follow_to>[-\w]+)/$', followed, name='followed'),

models.py

class Following(models.Model):
    follow_from = models.ForeignKey("auth.User",related_name='from_person')
    follow_to = models.ForeignKey("auth.User", related_name='to_person')
    date_follow = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return unicode(self.follow_from)

    def __str__(self):

2 Answers2

1

You have problem with this piece of code in your view.

Following.objects.get(follow_to=follow_to)

Here the follow_to parameter passed to view is string and from the url mentioned it seems it is 'n'. But you are searching for foreign key which will search for id of an object. id is integer.

So in the query its trying to convert 'n' into int to search appropriate object. But the conversion fails.

You need to either check for that and/or use id related regex in url for follow_to parameter.

Rohan
  • 52,392
  • 12
  • 90
  • 87
1

If the django.contrib.auth.models.User model is used, its pk is an integer. However, the url pattern matches any word character (\w). Thus if only numbers should be allowed, modifying urls.py to only allow integer id matches might be a solution.

url(r'^followed/(?P<follow_to>\d+)/$', followed, name='followed'),
zsepi
  • 1,572
  • 10
  • 19