AttributeError on successful redirection in django?

谁说我不能喝 提交于 2021-02-11 06:42:37

问题


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"),
    ...

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'

Product model :

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()
    ...

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

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,

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

Thanks.


回答1:


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

...
...

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.

if it works, please let me know.



来源:https://stackoverflow.com/questions/62849045/attributeerror-on-successful-redirection-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!