Basic commerce site developed with Python, HTML, and CSS, using Django Framework
This project is a commerce site where users have the ability to register, login, view all current listings made by themselves or other users, create a listing (which can include uploading an image file), view individual listings, bid on each listing as well as comment, and add listings to a watchlist
Personal Favorite Code Block - This required me to handle a variety of post requests that added rows to the database and find solutions that would lead me to the correct topbid and topbidder handling different possibilities for each along the way
Commerce-Project/auctions/views.py
Lines 76 to 207 in 12a88d4
def listing(request, pk): | |
#Remove from watchlist and handle anonymous user | |
title_of_watch = Listing.objects.filter(id=pk) | |
nouser = str(request.user) | |
if nouser == "AnonymousUser": | |
forwatch = Watchlist.objects.filter(user=None) | |
if nouser != "AnonymousUser": | |
forwatch = Watchlist.objects.filter(user=request.user) | |
forwatchlist = [] | |
for x in forwatch: | |
forwatchlist.append((x.id_of_listing).title) | |
if Listing.objects.filter(id=pk).values('title')[0].get('title') in forwatchlist: | |
remove = True | |
else: | |
remove = False | |
#display proper bid | |
bidnumber = Bid.objects.filter(id_of_listing=pk).count() | |
active = True | |
firstbid = Listing.objects.get(id=pk).starting_bid | |
if Bid.objects.filter(id_of_listing=pk).aggregate(Max('bid'))['bid__max']: | |
topbid = float("%.2f" % Bid.objects.filter(id_of_listing=pk).aggregate(Max('bid'))['bid__max']) | |
topbidder = Bid.objects.get(bid=topbid).user_id | |
bid = 0 | |
if firstbid > topbid: | |
bid = firstbid | |
else: | |
bid = topbid | |
newbid = Listing.objects.get(id=pk) | |
newbid.starting_bid = topbid | |
newbid.save() | |
overbid = "%.2f" %(bid+.01) | |
bid = "%.2f" % bid | |
you = "" | |
if topbidder == request.user and Listing.objects.get(id=pk).isactive == True: | |
you = "You are the top bidder" | |
active = Listing.objects.get(id=pk).isactive | |
print(active) | |
if topbidder == request.user and Listing.objects.get(id=pk).isactive == False: | |
you = "You won this auction!" | |
active = Listing.objects.get(id=pk).isactive | |
print(active) | |
else: | |
bid=Listing.objects.get(id=pk).starting_bid | |
overbid = "%.2f" %(float(bid)+.01) | |
you="" | |
# handle post requests (bidding, comments, changing wether a page is active, and watchlist) | |
listing_fields = Listing.objects.get(id=pk) | |
unsorted_comments = Comment.objects.filter(id_of_listing=pk) | |
comments = sorted(unsorted_comments, key=lambda x: x.createtime, reverse=True) | |
if request.method == "POST": | |
if 'bid' in request.POST: | |
bidform = BidForm(request.POST) | |
if bidform.is_valid(): | |
obj = bidform.save(commit=False) | |
obj.id_of_listing = Listing.objects.get(id=pk) | |
obj.user_id = request.user | |
obj.save() | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
if 'comment' in request.POST: | |
commentform = CommentForm(request.POST) | |
if commentform.is_valid(): | |
obj = commentform.save(commit=False) | |
obj.username = request.user | |
obj.id_of_listing = Listing.objects.get(id=pk) | |
obj.save() | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
if 'changeactive' in request.POST: | |
isactiveform = IsActiveForm(request.POST) | |
if isactiveform.is_valid(): | |
obj = isactiveform.save(commit=False) | |
obj.id = Listing.objects.get(id=pk).id | |
obj.creator_id = request.user | |
obj.starting_bid = bid | |
obj.title = Listing.objects.get(id=pk).title | |
obj.description = Listing.objects.get(id=pk).description | |
obj.image = Listing.objects.get(id=pk).image | |
obj.category = Listing.objects.get(id=pk).category | |
obj.isactive = False | |
obj.save() | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
if 'addtowatchlist' in request.POST: | |
watchlistform = WatchListForm(request.POST) | |
if watchlistform.is_valid(): | |
obj = watchlistform.save(commit=False) | |
obj.user = request.user | |
obj.id_of_listing = Listing.objects.get(id=pk) | |
obj.addtime = Listing.objects.get(id=pk).createtime | |
obj.save() | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
if 'removefromwatchlist' in request.POST: | |
title_of_listing = Listing.objects.get(id=pk) | |
Watchlist.objects.filter(id_of_listing=title_of_listing).delete() | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
else: | |
commentform = CommentForm() | |
bidform = BidForm() | |
return render(request, "auctions/listing.html",{ | |
'comments':comments, | |
'commentform':commentform, | |
'bidform':bidform, | |
"title":listing_fields.title, | |
"description":listing_fields.description, | |
"price":listing_fields.starting_bid, | |
"creator":listing_fields.creator, | |
"category":listing_fields.category, | |
"image":listing_fields.image, | |
"bid":bid, | |
"bidnumber":bidnumber, | |
"you":you, | |
"remove":remove, | |
"active":active, | |
"overbid":overbid | |
}) | |
Extra: This was my first time using Django's Modelforms and it was really interesting discovering how they work and the power that Django has right out of the box!
Commerce-Project/auctions/views.py
Lines 301 to 313 in 12a88d4
def create(request): | |
if request.method == "POST": | |
createform = ListingForm(request.POST, request.FILES) | |
if createform.is_valid(): | |
obj = createform.save(commit=False) | |
obj.creator = request.user | |
obj.save() | |
return HttpResponseRedirect('/') | |
else: | |
createform = ListingForm() | |
return render(request, "auctions/create.html",{ | |
'createform':createform, | |
}) |
Video demonstration: https://www.youtube.com/watch?v=W50ChueT1zM