feat: generando vista de compra con formularios de django.
This commit is contained in:
		@@ -1,4 +1,39 @@
 | 
			
		||||
from django import forms
 | 
			
		||||
from django.forms.widgets import DateInput
 | 
			
		||||
 | 
			
		||||
from .models import Sale, SaleLine
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ImportProductsForm(forms.Form):
 | 
			
		||||
    csv_file = forms.FileField()
 | 
			
		||||
 | 
			
		||||
class PurchaseForm(forms.ModelForm):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Sale
 | 
			
		||||
        fields = [
 | 
			
		||||
            "customer",
 | 
			
		||||
            "date",
 | 
			
		||||
            "phone",
 | 
			
		||||
            "description",
 | 
			
		||||
        ]
 | 
			
		||||
        widgets = {
 | 
			
		||||
            'date': DateInput(attrs={'type': 'date'})
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
class PurchaseLineForm(forms.ModelForm):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = SaleLine
 | 
			
		||||
        fields = [
 | 
			
		||||
            "product",
 | 
			
		||||
            "quantity",
 | 
			
		||||
            "unit_price",
 | 
			
		||||
            "description",
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LineaFormSet = forms.models.inlineformset_factory(
 | 
			
		||||
    Sale,
 | 
			
		||||
    SaleLine,
 | 
			
		||||
    extra=2,
 | 
			
		||||
    fields='__all__'
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
<!doctype html>
 | 
			
		||||
<form method="POST">
 | 
			
		||||
    {% csrf_token %}
 | 
			
		||||
        {{ sale_form}}
 | 
			
		||||
  {{ linea_formset.management_form }}
 | 
			
		||||
  {% for form in linea_formset %}
 | 
			
		||||
    <table style="border: solid 1px blue; margin: 10px">
 | 
			
		||||
        {{ form.as_table }}
 | 
			
		||||
    </table>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
    <br/><button name="form" type="submit" >Comprar</button>
 | 
			
		||||
</form>
 | 
			
		||||
@@ -6,6 +6,7 @@ app_name = 'don_confiao'
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
    path("", views.index, name="wellcome"),
 | 
			
		||||
    path("comprar", views.buy, name="buy"),
 | 
			
		||||
    path("comprar_django", views.django_buy, name="django_buy"),
 | 
			
		||||
    path("compras", views.purchases, name="purchases"),
 | 
			
		||||
    path("productos", views.products, name="products"),
 | 
			
		||||
    path("importar_productos", views.import_products, name="import_products")
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
 | 
			
		||||
from django.template import loader
 | 
			
		||||
 | 
			
		||||
from .models import Sale, Product, ProductCategory
 | 
			
		||||
from .forms import ImportProductsForm
 | 
			
		||||
from .forms import ImportProductsForm, PurchaseForm, LineaFormSet
 | 
			
		||||
 | 
			
		||||
import csv
 | 
			
		||||
import io
 | 
			
		||||
@@ -16,11 +16,23 @@ def buy(request):
 | 
			
		||||
    context = {}
 | 
			
		||||
    if request.POST:
 | 
			
		||||
        raise Exception(request.POST)
 | 
			
		||||
    else:
 | 
			
		||||
        raise Exception(request.POST)
 | 
			
		||||
    return render(
 | 
			
		||||
        request, 'don_confiao/purchase.html', context)
 | 
			
		||||
 | 
			
		||||
def django_buy(request):
 | 
			
		||||
    if request.method == "POST":
 | 
			
		||||
        raise Exception(request.POST)
 | 
			
		||||
    else:
 | 
			
		||||
        sale_form = PurchaseForm()
 | 
			
		||||
        sale_linea_form = LineaFormSet()
 | 
			
		||||
    return render(
 | 
			
		||||
        request,
 | 
			
		||||
        'don_confiao/django_purchase.html',
 | 
			
		||||
        {
 | 
			
		||||
            'sale_form': sale_form,
 | 
			
		||||
            'linea_formset': sale_linea_form
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
def purchases(request):
 | 
			
		||||
    purchases = Sale.objects.all()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user