首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError返回模型条目

TypeError返回模型条目
EN

Stack Overflow用户
提问于 2021-09-24 03:13:28
回答 1查看 36关注 0票数 0

我正在创建一个ebay风格的网站,拍卖物品。我有一个表单,它接受模型所要求的列表的值。表单呈现良好,但当我提交表单时,会得到错误:

代码语言:javascript
复制
<class 'TypeError'> views.py 80 ("l = Listing(title=title, description=description, url=url, user=user_id, category=category)")

forms.py

代码语言:javascript
复制
class NewListingForm(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(max_length=500)
    url = forms.URLField()
    bid = forms.DecimalField(decimal_places=2, max_digits=10)
    category = forms.ModelChoiceField(queryset=Category.objects.all())

views.py **在抛出错误的行周围添加

代码语言:javascript
复制
def newListing(request):
    try:
        if request.method == "POST":
            newListingForm = NewListingForm(request.POST)
            if newListingForm.is_valid():
                title = newListingForm.cleaned_data['title']
                description = newListingForm.cleaned_data['description']
                url = newListingForm.cleaned_data['url']
                bid = newListingForm.cleaned_data['bid']
                category = newListingForm.cleaned_data['category']
                current_user = request.user
                user_id = current_user.id
                **l = Listing(title=title, description=description, url=url, user=user_id, category=category)**
                l.save()
                b = Bid(price=bid, bid_count=0)
                b.listing.add(l)
                b.save()
                return HttpResponseRedirect(reverse("index"))
        else:
            newListingForm = NewListingForm()
            return render(request, "auctions/newListing.html", {
                'form': newListingForm
            })
    except Exception as exc:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
        newListingForm = NewListingForm()
        return render(request, 'auctions/newListing.html', {
            'form': newListingForm
        })

models.py

代码语言:javascript
复制
class Listing(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=500)
    url = models.URLField()
    user = models.ForeignKey('User', on_delete=models.CASCADE, name='author')
    category = models.ForeignKey('Category', on_delete=models.CASCADE, name='category', default="")

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

class Bid(models.Model):
    price = models.DecimalField(decimal_places=2, max_digits=10)
    bid_count = models.IntegerField()
    listing = models.ForeignKey('Listing', on_delete=models.CASCADE, name='listing', default="")

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

回答 1

Stack Overflow用户

发布于 2021-09-24 04:05:44

用户是一个关键字,所以我从中得到了一个错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69309099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档