python - Can someone explain this weird behaviour in {%url%} in django jinja -


when i'm passing argument url (placeholder 2) works fine,

<a href="{% url 'home:detail' 2 %}">{{ category.filter.first.person}}</a> 

and when this:

<a href="{% url 'home:detail' category.filter.first.person %}">{{ category.filter.first.person}}</a> 

it passes person object's output of str, name argument, expected.

but when this:

<a href="{% url 'home:detail' category.filter.first.person.id %}">{{ category.filter.first.person}}</a> 

django throws error:

reverse 'detail' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['detail/(?p<pk>\\d+)/$'] 

as if person object doesn't have id, of course not true, because:

<a href="{% url 'home:detail' 2 %}">{{ category.filter.first.person.id}}</a> 

the text link shows right id. weird thing can't pass id of person argument placeholder works fine.

edit added models code

class person(models.model):     name = models.charfield('name',max_length=40,unique=true)     pub_date = models.datetimefield('date_added',blank=true,null=true)     profile = models.manytomanyfield('usermanage.profile', through = 'home.ranking',through_fields=('person', 'profile'),swappable=true)     def __str__(self):         return self.name  class match(models.model):     person = models.foreignkey(person)     player1 = models.foreignkey('usermanage.profile', related_name='player1')     player2 = models.foreignkey('usermanage.profile', related_name='player2')     score = models.charfield(blank=true,null=true,max_length=10)     finished = models.booleanfield(default=false)     time_created = models.datetimefield(blank=true,null=true) 

and view code

class profileview(loginrequiredmixin, generic.listview):     model = profile     template_name = 'usermanage/profile.html'     context_object_name = 'matches'  def get_context_data(self,**kwargs):     context=super().get_context_data(**kwargs)     profile=self.request.user.profile     person_categories = person.objects.filter(profile=profile).annotate(                                             match_count=count('match')).order_by(                                                                 '-match_count')     categories=[]     person in person_categories:         matches = match.objects.filter(                 person=person,player1=profile             ) | match.objects.filte r(             person=person,player2=profile             ).order_by('-time_created')          categories.append(matches)      context['categories'] = categories     return context 

categories list of querysets containing matches player played/is playing.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -