Hi,

A couple of pointers:

1. If you are using class based views and want a Form, try using the
FormView - you don't need to write that much code yourself (regarding
validation of form and so on).
2. When you populate your context - you use a form class and not a form
instance - you should be instantiating the form instance itself in the
context (or change to a FormView, that will do it automatically).

Now to the program that you are having - the reason you are getting it is
because you are sending an invalid form to the post method. And the
method then doesn't return anything,

I would write it as follows:
def post(self, request, *args, **kwargs):
    list_product = self.form_class(request.POST)
    if list_product.is_valid():
        list_product.save()
        return redirect('products:product')    # <- Shouldn't there be an
ID here for going to the newly created product page?

    messages.error(request, "There was an error in the form, please correct
it below")
    return self.get(request, *args, **kwargs)

Now this is only an example - you will need to handle the fact the the form
need so be added to returned request as well.

Regards,

Andréas


Den fre 8 maj 2020 kl 09:54 skrev cosmos multi <elmasacrad...@gmail.com>:

> I am trying to populate a database, but when I send it it gives me the
> following error didn't return an HttpResponse
>
> this is the form
> class ProductForm(forms.ModelForm):
> name_product = forms.CharField(
> max_length=25,
> widget=forms.TextInput(
> attrs={
> 'class': 'form-control',
> 'id': 'name_product',
> }
> )
> )
>
> def clean_name_product(self):
> name_product = self.cleaned_data.get('name_product')
> if Product.objects.filter(name_product=name_product).exists():
> raise forms.ValidationError('El nombre del producto ya existe')
> return name_product
>
> class Meta:
> model = Product
> fields = (
> 'name_product', 'description', 'price', 'category', 'state', 'image'
> )
> labels = {
> 'name_product': 'Nombre del Producto',
> 'description': 'Descripcion',
> 'price': 'Precio',
> 'category': 'Categoria',
> 'state': 'Estado',
> 'image': 'Imagen del Producto',
> }
> widgets = {
> 'name_product': forms.TextInput(
> attrs={
> 'class': 'form-control',
> 'id': 'name_product',
> }
> ),
> 'description': forms.TextInput(
> attrs={
> 'class': 'form-control',
> 'id': 'description',
> }
> ),
> 'price': forms.NumberInput(
> attrs={
> 'class': 'form-control',
> 'id': 'price',
> }
> ),
> 'category': forms.SelectMultiple(
> attrs={
> 'class': 'custom-select',
> 'id': 'category',
> }
> ),
> 'state': forms.CheckboxInput(),
> }
> and this is the model
>
> class Product(models.Model):
> name_product = models.CharField(max_length=25)
> description = models.CharField(max_length=250)
> price = models.IntegerField()
> category = models.ForeignKey(Category, on_delete=models.CASCADE)
> state = models.BooleanField(default=True)
> image = models.ImageField(upload_to='products/')
> created_at = models.DateField(auto_now=True)
>
> def __str__(self):
> return self.name_product
>
> this is the view
>
> class ProductView(View):
> template_name = 'products/product.html'
> model = Product
> form_class = ProductForm
>
> def get_queryset(self):
> return self.model.objects.filter(state=True)
>
> def get_context_data(self, **kwargs):
> context = {}
> context['product'] = self.get_queryset()
> context['list_product'] = self.form_class
> return context
>
> def get(self, request, *args, **kwargs):
> return render(request, self.template_name, self.get_context_data())
>
> def post(self, request, *args, **kwargs):
> list_product = self.form_class(request.POST)
> if list_product.is_valid():
> list_product.save()
> return redirect('products:product')
>
> the error is. The views products.views.ProductView didn't return an
> httpresponse object. it returned none instead
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/211be261-f187-4ac4-a374-261480dd2696%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/211be261-f187-4ac4-a374-261480dd2696%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCdAs6%2BNiHHPuqZ6K51LDTvuTFY65Tqg7CS1dg-1DD96XA%40mail.gmail.com.

Reply via email to