【问题标题】:troubleshooting a value error in Python Django解决 Python Django 中的值错误
【发布时间】:2015-11-02 11:02:05
【问题描述】:
error:
ValueError at /productcatalog/

Cannot assign "1L": "CartItem.user" must be a "User" instance.

我正在尝试解决在尝试(本地)查看某些页面时收到的错误

我不确定我错过了什么,我的踪迹似乎特别指向了我的一种观点:

def index(request):
product = Product.objects.all()[:5]
category = Category.objects.all()
basket, created = CartItem.objects.get_or_create(status=0, user=request.user.id)
return render_to_response('index.html', {'category': category,
                          "product": product, 'basket': basket},
                          RequestContext(request))

具体线路

basket, created = CartItem.objects.get_or_create(status=0, user=request.user.id)

这是我的 Views.py 导入:

from django.shortcuts import render_to_response
from django.template import RequestContext
from productcatalog.models import Product, Category, Manufacturer
from django.conf import settings
from basket.models import CartItem

有人能看出我哪里出错了吗?

【问题讨论】:

  • 错误信息很清楚。 Django 想要一个用户类的实例,而不是数字 1。1 不是用户的实例,它是一个数字。您需要传入一个用户实例。

标签: python django-views django-1.8


【解决方案1】:

使用user=request.user 而不是user=request.user.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2019-09-22
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多