diff --git a/auctions/forms.py b/auctions/forms.py index c1b3c48..970e002 100644 --- a/auctions/forms.py +++ b/auctions/forms.py @@ -1,21 +1,8 @@ from django import forms +from .models import Listing, Biding -class NewListing(forms.Form): - title = forms.CharField(label="Listing Title", max_length=64) - description = forms.CharField( - label="Listing Description", - widget=forms.Textarea(attrs={'rows': 1, 'cols': 1}) - ) - initial_bid = forms.DecimalField(label="Initial Bid", max_digits=6, decimal_places=2) - photo_url = forms.URLField(label="Photo URL") - - CATEGORIES_CHOICES = ( - ('Fashion', 'Fashion'), - ('Home', 'Home'), - ('Electronics', 'Electronics'), - ('Toys', 'Toys'), - ('Other', 'Other') - ) - - category = forms.ChoiceField(label="Category", choices=CATEGORIES_CHOICES) +class NewListing(forms.ModelForm): + class Meta: + model = Listing + fields = ['title', 'description', 'category', 'photo_url', 'initial_bid'] diff --git a/auctions/views.py b/auctions/views.py index 03ee10e..3f04ade 100644 --- a/auctions/views.py +++ b/auctions/views.py @@ -3,6 +3,8 @@ from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render from django.urls import reverse +from django.contrib.auth.decorators import login_required + from .models import User, Listing from . import forms @@ -75,7 +77,18 @@ def listing(request, listing_id): }) +@login_required def add_listing(request): + if request.method == "POST": + form = forms.NewListing(request.POST) + if form.is_valid(): + obj = form.save(commit=False) + obj.seller = request.user + obj.save() + return HttpResponseRedirect(reverse("index")) + else: + form = forms.NewListing() + return render(request, "auctions/add.html", { - "form": forms.NewListing() + "form": form })