43π
β
You need to assign object
to your view using .get_object()
in the post
method of your view.
This is because Djangoβs get_context_data()
function uses the object
to pass it into the context. In case of errors in POST
request, this function will be called and it will look for self.object
which you did not assign, thereby leading to the error.
class CheckoutView(FormMixin , DetailView):
model = Cart
template_name = "carts/checkout_view.html"
form_class = GuestCheckoutForm
...
def post(self , request , *args , **kwargs):
self.object = self.get_object() # assign the object to the view
form = self.get_form()
if form.is_valid():
email = form.cleaned_data.get("email")
return self.form_valid(form)
else:
return self.form_invalid(form)
Also, it would be better to use UpdateView
here instead of DetailView
.
π€Rahul Gupta
2π
For me the solution was to initialize self.object
to []
(empty list) in get_context_data
.
- Static files won't load when out of debug in Django
- Django β Override admin site's login form
- Why is Django's Meta an old-style class?
0π
Remember these are classes and self is like soul of classes.
def get(self, request, id):
user_g = **self**.get_object(id)
π€Lord-shiv
- Resolving AmbiguousTimeError from Django's make_aware
- Django Admin β add collapse to a fieldset, but have it start expanded
- Django: Check for related objects and whether it contains data
- How to limit field access on a model based on user type on Graphene/Django?
Source:stackexchange.com