2017-04-13 80 views
1

我試圖讓我打標爲True在我的網站在data['checked_users']用戶:Django的無效字面INT()基數爲10: 'Stalone'

models.py

class Player(models.Model): 
    jucator = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, related_name='jucator') 
    porecla = models.CharField(max_length=50, null=True, blank=True) 
    selectat = models.BooleanField(default=False) 

    def __str__(self): 
     return u'%s' % self.jucator 

views.py

def check_user(request): 
    data = dict() 
    data['players'] = Player.objects.all() 
    if request.method == 'POST': 
     list_of_checks = request.POST.getlist('checks[]') 
     # data['checked_users'] = list_of_checks 
     data['checked_users'] = Player.objects.filter(jucator__in=list_of_checks) 
    return render(request, 'checkbox.html', data) 

checkbox.html

<body> 
    {% block content %} 
     <form action="" method="POST">{% csrf_token %} 
      <table> 
        {% if request.user.is_superuser %} 
         {% for user in players %} 
          <tr> 
           <td><input type="checkbox" name="checks[]" value="{{ user.jucator }}" title="selected_players">{{ user.jucator }}<br></td> 
          </tr> 
         {% endfor %} 
          <tr> 
           <td><input type="submit" name="submit" value="Submit"></td> 
          </tr> 
        {% else %} 
         {% for user in players %} 
          <tr><td>{{ user.jucator }}</td></tr> 
         {% endfor %} 
        {% endif %} 
      </table> 
     {% for player in checked_users %} 
      {{ player }} 
     {% endfor %} 
     </form> 
    {% endblock %} 
</body> 

如果我試試這個,它顯示了球員:

data['checked_users'] = list_of_checks 

如果我嘗試用過濾器,我得到這個錯誤:

invalid literal for int() with base 10: 'Stalone' 
+0

你可以通過編輯你的Q來發布list_of_checks和Player.objects.filter(jucator__in = list_of_checks)的值嗎? –

+1

更改爲'value =「{{user.jucator.id}}」'並重試。 –

+0

我添加了值list_of_checks pls的值 –

回答

2

你需要把裏面的value="{{ user.jucator }}"對象jucator的實際id。不是jucator對象本身。因此,改爲:value="{{ user.jucator.id }}"。現在您可以使用Player.objects.filter(jucator__in=list_of_checks)進行查詢,因爲list_of_checks包含整數列表(id s的jucator s)。

相關問題