【问题标题】:(Django - Python) Hidden Input Request to views.py(Django - Python)对views.py的隐藏输入请求
【发布时间】:2021-01-20 11:39:21
【问题描述】:

我必须创建一个包含拍卖的网站,并且我有一个显示所有活跃拍卖的主页...

我想通过单击相关按钮将用户重定向到拍卖详细信息,但隐藏输入请求存在一些问题,因为它没有向我的函数报告隐藏值( def bid (request,uction) )但我看到了在csrfmiddlewaretoken(id = 1)之后的url栏上,你能帮帮我吗? (我也尝试过 POST 请求...)

这些是我的代码:

  • views.py
 def home(request):

 auctions = Auction.objects.filter(status=True)

 form = detailForm(request.GET or None)

 if request.method == "GET":

     id = request.GET.get("id")

     if form.is_valid():

         [bid(request,auction) for auction in auctions if auction.id==id]

     else:

         form = detailForm()

 return render(request, "index.html", {"auctions":auctions, "form":form})
def bid(request, auction):

user = request.user

form = bidForm(request.POST)

if request.method == "POST":

    bid = request.POST.get("endPrice")

    if form.is_valid():

        if bid > auction.startPrice:

            auctionUpdate=form.save(commit=False)
            auctionUpdate.endPrice=bid
            auctionUpdate.winner=user
            auctionUpdate.save()

        else:

            messages.warning(request, "Devi puntare una cifra superiore a quella vincente!")
    else:

        form = bidForm()

return render(request, "bid.html", {"form":form})
  • forms.py
class detailForm(forms.ModelForm):

class Meta:
    model = Auction
    fields = ("id",)
  • index.html

    {% for auction in auctions %}
    <--! I put all the informations about auctions -->
    <form method="GET">
    {% csrf_token %}
       <input type="hidden" name="id" value={{auction.id}}>
       <input type="submit">
    {% endfor %}
    </form>
    

谢谢大家!

【问题讨论】:

    标签: python django input request hidden


    【解决方案1】:

    不确定我是否正确理解了您的问题,所以基本上我们有一个拍卖列表,当用户点击拍卖的相关按钮时,用户将被重定向到另一个页面。

    在这个模型中,您需要考虑 2 个视图和 2 个模板来处理它们。 ListView 列出您的所有操作,DetailView 处理每个拍卖的详细信息页面。因此,您将有一个 home.html 用于您的 ListView 和一个 bid.html 用于您的拍卖详细信息。

    我认为表单提交应该在你的细节视图中实现(在你的代码中bid函数),细节视图应该呈现一个带有bid.html模板的页面。

    在您的home.html 中,您可能只想留下每个拍卖的链接,例如:

    {% for auction in auctions %}
    
    <a type='button' href="{{ auction.get_absolute_url }}">{{ auction.name }}</a>
    
    {% endfor %}
    

    你需要在你的Auction模型中添加这个get_absolute_url方法,例如:

    # models.py
        def get_absolute_url(self):
            return reverse("auction-detail", kwargs={"pk": self.pk})
    

    也在你的路由中:

    # urls.py
    urlpatterns = [
        path('/', home, name='home'),
        path('/auctions/<int:pk>/', bid, name='auction-detail'),
    ]
    

    您还需要将auction 传递给bid 函数中的上下文变量,以便在模板中引用它。

    def bid (request, auction_id):
        auction = Auction.objects.get_object_or_404(id=auction_id)
        # ...
        context = {'form': form, 'auction': auction}
        return render(request, "bid.html", context)
    

    最后在您的详细视图模板 (bid.html) 中,使用表单提交:

    <form method="POST">
    {% csrf_token %}
       {{ form.as_p }}
       <input type="hidden" name="id" value={{auction.id}}>
       <input type="submit">
    </form>
    

    另一个建议是在这种情况下尝试使用基于类的视图,例如ListViewDetailView,这样可以更轻松地处理您的问题。查看this article了解更多详情。

    【讨论】:

    • 感谢您的回答...是的,就是这个想法,我会尝试您的解决方案,我会让您知道 ^^
    • 谢谢,我做了一些更改,但您的帮助很重要! ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多