Django的-用户名被写为User_admin.User.None,如何解决?

时间:2019-09-18 17:42:47

标签: python django django-models django-views jinja2

我已经建立了一个网站,用户可以在该网站上发帖,其他用户可以提供赞,或者如果他们希望他们可以删除自己的赞。我想在那个帖子给过的网站上写下来。因此,我尝试了以下方式,但是它正在html文件中打印出Sub MySub() Dim dbs As DAO.Database Dim rst As DAO.Recordset Dim flg As Boolean Set dbs = CurrentDb Set rst = dbs.OpenRecordset("select * from tblImport inner join tblUser on tblImport.UserID = tblUser.ID") With rst If Not .EOF Then .MoveFirst Do Until .EOF Or flg ' No short-circuit evaluation in VBA flg = IsNull(!Cusip) .MoveNext Loop End If .Close End With Set rst = Nothing Set dbs = Nothing End Sub 。 在产品型号中。py

user_admin.User.None

这是我的views.py


class Item_Product(models.Model):

   title = models.CharField(max_length=100)
   pub_date  = models.DateTimeField()
   body = models.CharField(max_length=1000)
   image = models.ImageField(upload_to = user_directory_path_for_item_product)
   likes = models.PositiveIntegerField(default=1)
   product_user = models.ForeignKey(User, on_delete=models.CASCADE)

   def __str__(self):
         return self.title
   def pub_date_pretty(self):
          return self.pub_date.strftime("%b %e %Y")

class Vote(models.Model):

   voter = models.ManyToManyField(User, blank=True)
   item_product = models.OneToOneField(Item_Product, on_delete=models.CASCADE)    

   def __str__(self):
         return "Item ID: "+str(self.item_product.id) + " Item Name:"+str(self.item_product)

这是我的html

def item(request, item_id):
    obj = get_object_or_404(Item_Product, pk=item_id)
    voter_obj = get_object_or_404(Vote, item_product_id=item_id)
    return render(request, 'item.html', {'item':obj, 'voter':voter_obj})

当我像其他用户一样给予时,它显示为

{%  extends 'navbar-footer.html'%}
{% block content %}
<h1>{{button}}</h1>
<h4>This is item number {{item.id}}</h4>
<h4>{{ item.title }}</h4>
<h4>{{ item.body }}</h4>
<button type="button" class="btn {{button}}" onclick="javascript:{document.getElementById('increase_like').submit()}">Like {{item.likes}}</button>
<form action="{%url 'increase_like' item.id%}" method="POST" id='increase_like' >
    {% csrf_token %}
    <input type="hidden" name="" id="">
</form>
<img src="{{item.image.url}}" alt="">
<h1>{{voter.voter}}</h1>
{% endblock %}

而不是显示在该帖子中给出了赞的实际用户的用户名。我该如何显示实际用户名而不是这个?

0 个答案:

没有答案