Skip to content

Commit

Permalink
ModelForm for the Listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekaterina-Vititneva committed Sep 13, 2024
1 parent bc535f0 commit c9f569d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
30 changes: 13 additions & 17 deletions project_2/commerce/auctions/forms.py
Original file line number Diff line number Diff line change
@@ -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€'}))

Expand Down
16 changes: 7 additions & 9 deletions project_2/commerce/auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c9f569d

Please sign in to comment.