From c9f569d3f9d33cdafeab8e4022a286ad3a6e30e5 Mon Sep 17 00:00:00 2001 From: Ekaterina Vititneva Date: Fri, 13 Sep 2024 22:02:04 +0200 Subject: [PATCH] ModelForm for the Listing --- project_2/commerce/auctions/forms.py | 30 ++++++++++++---------------- project_2/commerce/auctions/views.py | 16 +++++++-------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/project_2/commerce/auctions/forms.py b/project_2/commerce/auctions/forms.py index 5459d4b..3687885 100644 --- a/project_2/commerce/auctions/forms.py +++ b/project_2/commerce/auctions/forms.py @@ -1,22 +1,18 @@ from django import forms +from .models import Listing + +class ListingForm(forms.ModelForm): + class Meta: + model = Listing + fields = ['title', 'description', 'imageURL', 'category', 'bid'] + widgets = { + 'title': forms.TextInput(attrs={'class': 'form-control'}), + 'description': forms.Textarea(attrs={'class': 'form-control'}), + 'imageURL': forms.URLInput(attrs={'class': 'form-control'}), + 'category': forms.Select(attrs={'class': 'form-control'}), + 'bid': forms.NumberInput(attrs={'class': 'form-control'}), + } -class ListingForm(forms.Form): - title = forms.CharField(label="Listing title", max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'})) - description = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control'}), label="Description") - bid = forms.DecimalField(label="Starting Bid", widget=forms.NumberInput(attrs={'class': 'form-control'})) - imageURL = forms.URLField(label="Image URL", required=False, widget=forms.URLInput(attrs={'class': 'form-control'})) - category = forms.ChoiceField( - choices=[ - ('Electronics', 'Electronics'), - ('Fashion', 'Fashion'), - ('Home', 'Home'), - ('Toys', 'Toys'), - ], - label="Category", - required=False, - widget=forms.Select(attrs={'class': 'form-control'}) - ) - class BidForm(forms.Form): bid = forms.DecimalField(label="Place Bid", widget=forms.NumberInput(attrs={'class': 'form-control', 'placeholder': '0.00€'})) diff --git a/project_2/commerce/auctions/views.py b/project_2/commerce/auctions/views.py index 8711c36..6843bfc 100644 --- a/project_2/commerce/auctions/views.py +++ b/project_2/commerce/auctions/views.py @@ -69,21 +69,19 @@ def register(request): else: return render(request, "auctions/register.html") +@login_required def create_listing(request): if request.method == "POST": form = ListingForm(request.POST) if form.is_valid(): - listing = Listing( - title=form.cleaned_data['title'], - description=form.cleaned_data['description'], - bid=form.cleaned_data['bid'], - imageURL=form.cleaned_data['imageURL'], - category=form.cleaned_data['category'], - created_by=request.user, - last_modified_by=request.user - ) + listing = form.save(commit=False) + listing.created_by = request.user + listing.last_modified_by = request.user listing.save() + messages.success(request, "Listing created successfully!") return redirect("index") + else: + messages.error(request, "Please correct the errors below.") else: form = ListingForm()