Cannot resolve keyword 'ir' into field. Choices are: category, category_id, date, description, id, is_on_main, name, price, url

huangapple go评论58阅读模式
英文:

Cannot resolve keyword 'ir' into field. Choices are: category, category_id, date, description, id, is_on_main, name, price, url

问题

以下是您要翻译的内容:

"for some reason it does not see the ir field, although it exists."
"views.py"
"models.py(Product)"
"index.html(part with data)"
"thank you in advance"

英文:

for some reason it does not see the ir field, although it exists.
views.py

def index(request):
    data = (
        Product
        .objects.select_related('category')
        .filter(is_on_main=True)
        .values('pk','name','price','ir')
    )
    categ = Category.objects.all()
    return render(request,'magazin/index.html',{'data' : data,'cat' : categ})

models.py(Product)

class Product(models.Model):
    is_on_main = models.BooleanField(default=False)
    category = models.ForeignKey(Category, on_delete = models.CASCADE)
    name = models.CharField(max_length=50)
    price = models.IntegerField()
    date = models.DateTimeField(null = True,auto_now=True)
    url = models.SlugField(max_length=100,unique=True,null=True)
    description = models.TextField(max_length=2000,default='Описание')
    ir = timezone.now() - datetime.timedelta(minutes=30)
    def __str__(self):
        return self.name
    
    class Meta:
        ordering = '-date', 

index.html(part with data)

{% for i in data %}
        <div>
           <h4><b>НАЗВАНИЕ: </b>{{i.name}}</h4>
           <h4><b>ЦЕНА: </b>{{i.price}}</h4>
           <a href="{% url 'detail' pk=i.pk %}">ПОДРОБНЕЕ</a>
           {% if i.date >= i.ir %}
                <h1 class="r">НЕДАВНО В ПРОДАЖЕ!</h1> 
           {% endif %}                                                                                                                            
           <hr>                      
        </div>

thank you in advance

答案1

得分: 0

Product 模型中的 ir 声明无效。尝试像这样声明它:

ir = models.DateTimeField(default=(timezone.now() - datetime.timedelta(minutes=30)))
英文:

ir declaration in your Product model is not valid. Try declaring it like:

ir = models.DateTimeField(default=(timezone.now() - datetime.timedelta(minutes=30)))

huangapple
  • 本文由 发表于 2023年6月22日 19:18:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定