在Django中成功重定向时出现AttributeError吗? [英] AttributeError on successful redirection in django?

查看:43
本文介绍了在Django中成功重定向时出现AttributeError吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从主机视图成功发送POST请求时重定向.

Redirection on successful POST request from Host View.

def product_page_view(request, prod_slug, color=None, size=None):
    ...
    ...
    prod            = Product.objects.filter(slug=prod_slug).first()
    seller          = prod.seller
    ...
    ....
    order_form = Buy_now_form()
    if request.method == "POST":
        order_form = Buy_now_form(request.POST)

        if order_form.is_valid():
            # Function for processing POST request and return order 
            order = buy_now_view(request, prod.slug, color, size)
            # Redirection 
            return redirect(reverse('product-shipping', kwargs={'order_id':order.order_id}))
    ...

主机视图和目标视图的网址为

url's of Host view and Target View are

    ...
    path('products/<slug:prod_slug>/<str:color>/<str:size>/', product_page_view, name="product-page-view-color-size"),
    path('products/<int:order_id>/shipping/', shipping_view, name="product-shipping"),
    ...

尽管,函数已成功重定向,但是具有AttributeError和随后的追溯.

Though, function is redirecting successfully, but with AttributeError and following traceback.

Traceback (most recent call last):
  File "/home/dixitgpta/byghouz/byghouz_env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/dixitgpta/byghouz/byghouz_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/dixitgpta/byghouz/byghouz_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/dixitgpta/byghouz/project/products/views.py", line 575, in product_page_view
    seller          = prod.seller

Exception Type: AttributeError at /products/54795882652377/shipping/
Exception Value: 'NoneType' object has no attribute 'seller'

产品型号:

class Product(models.Model):
    seller = models.ForeignKey(BygSeller, on_delete=models.CASCADE)

    id = models.IntegerField(primary_key=True)  
    slug = models.SlugField(max_length=250, null=True, blank=True)
    title = models.CharField(max_length=100)
    short_descrition = models.CharField(max_length=150, default=' ')
    date = models.DateField(auto_now_add=True)

    step1 = models.BooleanField(default=False)
    step2 = models.BooleanField(default=False)
    step3 = models.BooleanField(default=False)
    step4 = models.BooleanField(default=False)
    step5 = models.BooleanField(default=False)
    step6 = models.BooleanField(default=False)
    step7 = models.BooleanField(default=False)

    is_verified = models.BooleanField(default=False)
    is_published = models.BooleanField(default=False)

我的目标重定向视图是:

My target Redirected View is :

def shipping_view(request, order_id):
    customer = Customer.objects.filter(usr=request.user).first()
    order = Order.objects.filter(order_id=order_id).first()
    ...

重定向成功后在TargetView上进行跟踪(以防有人发现它有用).

Traceback on TargetView after successful redirection (in case anyone finds it useful).

问题是: 重定向后 ,traceback表示 prod为None ,但是 prod 属于 product_page_view ,而我没有得到callback_kwargs的想法.传递给callback_kwargs 中的 prod_slug的值(如回溯所示)是 order_id ,它属于 shipping_view .还有,

Problem is : After redirection, traceback says that prod is None, howerver prod belongs to the product_page_view and I didnt get the idea of callback_kwargs. Values passed to the prod_slug in callback_kwargs(as the traceback says) is order_id, it belongs to the shipping_view. Also,

为什么回调函数 product_page_view ?是不是 Shipping_view ?

Why is callback function product_page_view? Shouldn't it be Shipping_view?

谢谢.

推荐答案

实际上,您不应直接将 first() filter()方法一起使用.没有记录(数据),它将返回无.因此None对象没有任何属性卖方

Actually you should not use first() directly with filter() method. there is no record(data), It will return None. so None object has no any attribute seller

您可以使用这种过滤方法

you can use filter method like this

...
...
products = Product.objects.filter(slug = prod_slug)
if products.exists():  # True if any record available
     prod = products.first() 
     seller = prod.seller # please try print(seller) after that to check first record

...
...

确保您在模型中具有属性卖方.并且您的模型包含至少1条记录.
您也可以使用count()方法返回包含多少行.

make sure you have attribute seller in models. and your models contains atleast 1 record.
Also you can use count() method which return how many row it contains.

如果有效,请告诉我.

这篇关于在Django中成功重定向时出现AttributeError吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆