0

models.py

`

class Product(models.Model):
  name = models.CharField(max_length=50 , verbose_name='العنوان') 
  disc = models.TextField (null=True , verbose_name='وصف المنتج')
  price = models.DecimalField (max_digits=5 , decimal_places=0 , null=True , verbose_name='السعر')
  photo = models.ImageField (null=True, upload_to='static\product_images', verbose_name='صورة المنتج')
  active = models.BooleanField(default=True , verbose_name='حالة المنتج')
  category = models.CharField(max_length=50,null=False)
  slug = models.SlugField(blank=True, null=True)

urls.py

`

urlpatterns = [

  path('', views.Product_list , name= 'home'),
  path('product/<int:product_name>/', views.Product_details , name= 'Product_details'),
  path('product/<int:product_name>/order', views.Product_order , name= 'order'),
  path('qa/', views.Question_list , name= 'Question_list'),
  path('annoncement/', views.Blog_list , name= 'Blog_list'),
  path ('about/' , views.about , name='about'),

``

views.py

`

def Product_order (request, product_name):
  if request.method == 'POST':
    order = OrderForms(request.POST)
  return render (request , 'order.html' , {'order' : order,})
  


`

error

UnboundLocalError at /product/2/order cannot access local variable 'order' where it is not associated with a value

solution for my probleme

Mr Amir
  • 13
  • 2
  • Apparently the problem you're having is with the scope of the variable, here's the link to another stackoverflow post that deals with this issue: https://stackoverflow.com/questions/9264763/why-does-this-unboundlocalerror-occur-closure – Matheus Simão Caixeta Dec 17 '22 at 11:38

1 Answers1

0

You have your order variable inside if statement that means it has the local scope. But, you call it outside of its scope. You can return inside if.

def Product_order (request, product_name):
  if request.method == 'POST':
    order = OrderForms(request.POST)
    return render (request , 'order.html' , {'order' : order,})
 // return some errors