简体   繁体   English

为什么我得到 Django 表单:关键错误

[英]Why does i am getting Django form: Key error

I am creating a creating a auction management system in which i was going to add the bid functionality and know i am facing this error我正在创建一个拍卖管理系统,我将在其中添加出价功能并且知道我正面临这个错误

i am highlighting the line in which i am getting error我突出显示出现错误的行

view.py视图.py

class ItemDetailView(CreateView):
    template_name = "itemdetail.html"
    form_class = BidOnProduct
    success_url = reverse_lazy("auction:item_detail")

    def dispatch(self, request, *args, **kwargs):
        buyer_id = request.session.get("id")
        buyer_obj = Buyer.objects.filter(id=buyer_id)
        if buyer_obj:
            pass
        else:
            return redirect("auction:buyer_login")
        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        item = Item.objects.get(slug=kwargs["slug"])
        bids = Bid.objects.filter(item=item)

        context["item"] = item
        context["bids"] = bids
        return context

    def form_valid(self, form):
        buyer_id = self.request.session.get("id")
        if buyer_id:
            buyer_obj = Buyer.objects.get(id=buyer_id)
            form.instance.buyer = buyer_obj
            form.instance.item = Item.objects.get(slug=self.kwargs["slug"])
            form.save()
            return redirect("auction:buyerhome")
        return super().form_valid(form)

form.py表格.py

class BidOnProduct(forms.ModelForm):
    class Meta:
        model = Bid
        fields = ["price"]

Model.py模型.py

class Bid(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateField(auto_now_add=True)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    price = models.IntegerField()
    biding_time = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.user.username

htmlcode html代码

    {% if request.user.buyer %}
    <form class="form-horizontal" action="" method="post"">
        {% csrf_token %}
        {{form.as_p}}
        <button class=" btn btn-info"> Add product</button>
    </form>
    {%endif%}

this is the error这是错误

These are the line这些是线

Url.py网址.py

 path("item/<slug:slug>/", ItemDetailView.as_view(), name="item_detail"),

In ItemDetailView , you have:ItemDetailView中,您有:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    item = Item.objects.get(slug=kwargs["slug"])  # Error line
    bids = Bid.objects.filter(item=item)

    context["item"] = item
    context["bids"] = bids
    return context

On line with error, you are accessing kwargs that are passed to get_context_data .根据错误,您正在访问传递给get_context_data的 kwargs。 It is not the same as self.kwargs (kwargs from URL), these kwargs are just a shortcut to update context with all passed values.它与self.kwargs (来自 URL 的 kwargs)不同,这些 kwargs 只是使用所有传递的值更新上下文的快捷方式。 You need to use self.kwargs :您需要使用self.kwargs

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    item = Item.objects.get(slug=self.kwargs["slug"])  # Here
    bids = Bid.objects.filter(item=item)

    context["item"] = item
    context["bids"] = bids
    return context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM