mainapp/views.py
def create_product(request):
    """
    This view has 2 cases:
    1) POST-method: adds new item to the database and returns renewed list
       of products.
    2) GET-method: opens empty modal form.
    :param request: request object.
    :return: serialized form and list of products.
    """
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            data = {
                'form_is_valid': True,
                'products_html': render_to_string(
                    'mainapp/product_list.html',
                    context={'products': Product.objects.all()},
                    request=request
                )
            }
        else:
            data = {
                'form_is_valid': False,
                'form_html': render_to_string(
                    'mainapp/create_product_form.html',
                    context={'form': form},
                    request=request
                )
            }
    else:
        data = {
            'form_html': render_to_string(
                'mainapp/create_product_form.html',
                context={'form': ProductForm()},
                request=request
            )
        }
    return JsonResponse(data)

    This view has 2 cases:
    1) POST-method: adds new item to the database and returns renewed list
       of products.
    2) GET-method: opens empty modal form.
    :param request: request object.
    :return: serialized form and list of products.
    
This view has 2 cases:
1) POST-method: adds new item to the database and returns renewed list
of products.
2) GET-method: opens empty modal form.
This view has 2 cases : 1 ) POST - method : adds new item to the database and returns renewed list of products . 2 ) GET - method : opens empty modal form .
def create_product(request):
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            data = {
                'form_is_valid': True,
                'products_html': render_to_string(
                    'mainapp/product_list.html',
                    context={'products': Product.objects.all()},
                    request=request
                )
            }
        else:
            data = {
                'form_is_valid': False,
                'form_html': render_to_string(
                    'mainapp/create_product_form.html',
                    context={'form': form},
                    request=request
                )
            }
    else:
        data = {
            'form_html': render_to_string(
                'mainapp/create_product_form.html',
                context={'form': ProductForm()},
                request=request
            )
        }
    return JsonResponse(data)
def create_product ( request ) : if request . method == 'POST' : form = ProductForm ( request . POST ) if form . is_valid ( ) : form . save ( ) data = { 'form_is_valid' : True , 'products_html' : render_to_string ( 'mainapp/product_list.html' , context = { 'products' : Product . objects . all ( ) } , request = request ) } else : data = { 'form_is_valid' : False , 'form_html' : render_to_string ( 'mainapp/create_product_form.html' , context = { 'form' : form } , request = request ) } else : data = { 'form_html' : render_to_string ( 'mainapp/create_product_form.html' , context = { 'form' : ProductForm ( ) } , request = request ) } return JsonResponse ( data )
This view has 2 cases:
1) POST-method: adds new item to the database and returns renewed list
of products.
This view has 2 cases : 1 ) POST - method : adds new item to the database and returns renewed list of products .
"""
    This view has 2 cases:
    1) POST-method: adds new item to the database and returns renewed list
       of products.
    2) GET-method: opens empty modal form.
    :param request: request object.
    :return: serialized form and list of products.
    """
serialized form and list of products.
serialized form and list of products .
request
None