【问题标题】:django view not redirectingdjango视图没有重定向
【发布时间】:2020-07-16 05:56:52
【问题描述】:

我有两个函数product_page_viewbuy_now_view

问题是,在成功发帖order_form之后。 buy_now_view 将返回 product_page_view 而不重定向,如果我这样做的话:

def product_page_view(request, prod_slug, color=None, size=None):
    ...
    ...
    order_form = Buy_now_form()
    if request.method == "POST":
        order_form = Buy_now_form(request.POST)

        if order_form.is_valid():
            buy_now_view(request, prod_slug, color, size)
    ...
    ...
 

def buy_now_view(request, prod_slug, color, size):
    ...
    ...
    return redirect(reverse("product-shipping", args=(order_id,)))


def shipping_view(request, order_id):
    ...

如果我这样做:

def buy_now_view(request, prod_slug, color, size):
    ...
    ...
    return order_id


def product_page_view(request, prod_slug, color=None, size=None):
    ...
    ...
    order_form = Buy_now_form()
    if request.method == "POST":
        order_form = Buy_now_form(request.POST)

        if order_form.is_valid():
            order_id = buy_now_view(request, prod_slug, color, size)
            return redirect(reverse("product-shipping", args=(order_id,)))
    ...
    ...


def shipping_view(request, order_id):
    ...

它给了我不想要的结果。 Link to the Question

我的网址是:

# INDIVIDUAL PRODUCT VIEW PAGE COLOR & SIZE
    path('products/<slug:prod_slug>/<str:color>/<str:size>/', product_page_view, name="product-page-view-color-size"),

# INDIVIDUAL PRODUCT VIEW PAGE SIZE
    path('products/<slug:prod_slug>/<str:size>/', product_page_view, name="product-page-view-size"),

# INDIVIDUAL PRODUCT VIEW PAGE COLOR
    path('products/<slug:prod_slug>/<str:color>/', product_page_view, name="product-page-view-color"),

# INDIVIDUAL PRODUCT VIEW PAGE
    path('products/<slug:prod_slug>/', product_page_view, name="product-page-view"),

# SHIPPING VIEW
    path('products/<int:order_id>/shipping/', shipping_view, name="product-shipping"),

product_page_view:

def product_page_view(request, prod_slug, color=None, size=None):

    order_form = Buy_now_form()
      if request.method == "POST":
          order_form = Buy_now_form(request.POST)

          if order_form.is_valid():
              order_id = buy_now_view(request, prod_slug, color, size)
              return redirect(reverse('product-shipping', args=(order_id,)))

    prod            = Product.objects.filter(slug=prod_slug).first()
    seller          = prod.seller
    
    step_vital_info = Vital_info.objects.filter(product=prod).first()
    step_variation  = Product_variation.objects.filter(product=prod)
    step_offer      = Offer.objects.filter(product=prod).first()
    step_images     = Images.objects.filter(product=prod).first()
    step_desc       = Description.objects.filter(product=prod).first()

    variation_theme = {}
    variation_theme_img = {}
    var_prod_color = set()
    for i in step_variation:
        variation_theme.setdefault(i.color, []).append(i.size)
        var_prod_color.add(i.color)
        variation_theme_img.setdefault(i.color, []).append(i.var_image)

    if step_offer.sale_price:
        offer_price = step_offer.price
        discount = round(((offer_price - step_offer.sale_price)/offer_price) * 100)

    query_form = Query_Form()

    queries = {}
    query_ques = Query.objects.filter(product=prod) 
    for i in query_ques:
        queries.setdefault(i, []).append(Query_ans.objects.filter(question=i).first())    
        queries.setdefault(i, []).append(Vote.objects.filter(question=i).first())    


    return render(r...

这里到底发生了什么?

【问题讨论】:

  • 你也可以添加你的 urls.py。并说明您的页面在操作后重定向到哪里。
  • 感谢您抽出宝贵时间@Pax。我已经添加了 URL,product_page_view 被重定向到shipping_view。我在上面提到了两种情况,它们都有不同的重定向问题。
  • 你在urls.py中写了app_name变量吗?
  • 我实际上不知道该怎么做。你能分享一些细节吗?谢谢。
  • 嗯,它实际上是 django 中的一个约定。如果您还没有使用过app_name,那么这可能不是您问题的解决方案。假设您有 2 个应用程序,并且都使用name="home" 设置了路径。如果您尝试重定向到home,URLConf 将会对去哪个家感到困惑。所以如果我们定义app_name="App1"。你可以通过说redirect("App1:home") 告诉 django 重定向哪个页面。如果您已经定义了 app_name。有必要保持 app_name:name 重定向。

标签: django django-models django-forms django-views django-templates


【解决方案1】:

其实你忘了从product_page_view返回重定向请求。 buy_now_view 已将重定向请求返回到 product_page_view。现在它为了重定向,product_page_view 也必须返回值。

def product_page_view(request, prod_slug, color=None, size=None):
    ...
    ...
    order_form = Buy_now_form()
    if request.method == "POST":
        order_form = Buy_now_form(request.POST)

        if order_form.is_valid():
            return buy_now_view(request, prod_slug, color, size)

【讨论】:

  • 我试过return buy_now_view(request, prod_slug, color, size),但没有用。
猜你喜欢
  • 2021-04-16
  • 2015-11-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 2021-07-28
  • 2021-09-25
  • 2013-08-27
相关资源
最近更新 更多