import requests
from django.http import HttpResponse
from django.shortcuts import render
from .forms import listProductsForm
from .models import User,listProducts

# Create your views here.
def index(request):

	products = listProducts.objects.all()

	info_all = []
	for product in products:
		list_products = {
			'name':product.name,
			'colt':product.colt,
			'unit':product.unit
		}
		info_all.append(list_products)

	if(request.method == 'POST'):
		form = listProductsForm(request.POST)
		if form.is_valid():
			form.save()
			form = listProductsForm()
			context = {'form':form,'save':'Позиция добавлена'}
			return render(request, 'www/index.html',context)
		else:
			form = listProductsForm()	
			context = {'form':form,'error':'введены не верные данные'}
			return render(request, 'www/index.html',context)
	else:		
		context = {'info':info_all}
		return render(request, 'www/index.html',context)

