Django DeleteView\uuu str\uuuuuuu返回了nonstring(type model name)

2024-05-19 02:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现DeleteView。但是,我得到以下类型错误:

__str__ returned non-string (type Fund) 

我怎样才能解决这个问题?你知道吗

提前感谢大家

代码:

基金模式:

class Fund(models.Model):
 name = models.CharField(max_length=100)
 slug = models.CharField(max_length=100, default = 'default')
 amount = models.DecimalField(max_digits=20, decimal_places=0)

 def __str__ (self):
    return self.name

你知道吗视图.py地址:

class CashFlowDelete(DeleteView):
 model = CashFlow
 success_url = reverse_lazy('fds:fds')

你知道吗网址.py地址:

path('deletecashflow/<int:pk>/', views.CashFlowDelete.as_view(),name = "delete_cashflow"),

基金_详细信息.html你知道吗

<a href="{% url 'fds:delete_cashflow' pk=cashflow.pk %}">Delete</a>

现金流确认_删除.html你知道吗

<form method="post">
 {% csrf_token %}
 <p>Are you sure you want to delete "{{ object }}"?</p>
 <input type="submit" value="Confirm">
</form>

Tags: name基金modelstypedeletelengthmaxclass
2条回答

全部, 设法解决了这个问题。事实上,我的模板中有一个问题。你知道吗

这是正确的模板:

<form method="post">
 {% csrf_token %}
 <p>Are you sure you want to delete "{{ form }}"?</p>
 <input type="submit" value="Confirm">
</form>

祝你一切顺利,谢谢大家帮助我

__str__ must return string and your __str__ is return other than string.

例如 在这里,价格被转换成字符串使用f'strings

class Product(models.Model):
    name = models.CharField(max_length=128)
    price = models.FloatField()

    def __str__(self):
        return f'{self.price}'

You can format string in may ways some of them are. Check Python version while converting to string

  • str(self.price)
  • "{}".format(self.price)
  • "%s" % (self.price)
  • f"{self.price}"f-string in python 3.6+ only

相关问题 更多 >

    热门问题