file upload form

This commit is contained in:
Priyatham Sai Chand 2021-01-15 14:35:47 +05:30
parent bf1d266890
commit acbca41040
17 changed files with 76 additions and 52 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe"
}

Binary file not shown.

39
auctions/forms.py Normal file
View File

@ -0,0 +1,39 @@
from django import forms
categories = [
(None,'Select one...'),
('Fashion','Fashion'),
('Electronics','Electronics'),
('Home','Home'),
('Sports','Sports'),
('Toys','Toys'),
('Automobile','Automobile'),
('Books','Books'),
('Videogames','Videogames'),
('Others','Others')
]
class BidForm(forms.Form):
bid_value = 0.00
def __init__(self, bid_value,*args,**kwargs ):
super().__init__(*args,**kwargs)
self.bid_value = bid_value + 1
self.fields['bid'].widget.attrs['min'] = self.bid_value
bid = forms.DecimalField(decimal_places=2)
class CreateForm(forms.Form):
title = forms.CharField( max_length=100, widget=forms.TextInput(attrs={'placeholder': 'title'}))
desc= forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder': 'description'}))
starting_bid= forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Starting bid'}))
category = forms.ChoiceField(choices=categories)
photo = forms.ImageField()
def __init__(self, *args, **kwargs):
super(CreateForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-control'

View File

@ -20,7 +20,7 @@ class AuctionListing(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE)
price = models.DecimalField(max_digits = 10,decimal_places = 2)
desc = models.CharField(max_length = 1000)
picture = models.URLField(default="https://www.riobeauty.co.uk/images/product_image_not_found.gif")
picture = models.ImageField();
category = models.CharField(max_length = 64,choices=category_choices)
date_added = models.DateTimeField(auto_now_add=True)
closed = models.BooleanField(default=False)

View File

@ -2,33 +2,15 @@
{% block body %}
<div class="form-group" style="margin-left: 20px">
<h2>Create Listing</h2>
<form action="{% url 'create' %}" method="post">
<form action="{% url 'create' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<input class="form-control" autofocus type="text" name="title" placeholder="title">
</div>
<div class="form-group">
<input class="form-control" type="text" name="desc" placeholder="Description">
</div>
<div class="form-group">
<input class="form-control" type="number" name="starting_bid" placeholder="starting bid">
</div>
<div class="form-group">
<input class="form-control" type="url" name="photo" placeholder="Picture(Optional)">
</div>
<div class ="form-group">
<select class="form-control" name = "category" placeholder = "Category">
<option selected disabled>Select a Category</option>
{% for cat in categories %}
<option value="{{cat}}">{{cat}}</option>
{% endfor %}
</select>
</div>
<input class="btn btn-primary" type="submit" value="Create Listing">
{{ Form.as_p }}
<input class="btn btn-primary" type="submit" value="Create Listing"></div>
</form>
{% endblock %}

View File

@ -20,7 +20,7 @@
</form>
</div>
<img src = "{{Listing.picture}}" width = "50%" height= "50%"></img>
<img src = "{{ Listing.picture.url }}" width = "50%" height= "50%"></img>
<hr>
<p style="font-size: large;">{{Listing.desc}}</p>

View File

@ -5,19 +5,11 @@ from django.shortcuts import render
from django.urls import reverse
from django import forms
from .models import User,AuctionListing,Comment,Bids
from .forms import BidForm,CreateForm
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Videogames', 'Other']
class BidForm(forms.Form):
bid_value = 0.00
def __init__(self, bid_value,*args,**kwargs ):
super().__init__(*args,**kwargs)
self.bid_value = bid_value + 1
self.fields['bid'].widget.attrs['min'] = self.bid_value
bid = forms.DecimalField(decimal_places=2)
def watch(request,title):
"""
@ -170,27 +162,21 @@ def create(request):
"""
Used to create a Listing
"""
if request.method == "POST":
title = request.POST["title"]
desc = request.POST["desc"]
starting_bid = request.POST["starting_bid"]
photo_url = request.POST["photo"]
# alternate photo to display
if photo_url == "":
photo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/No_image_available_600_x_450.svg/1280px-No_image_available_600_x_450.svg.png"
category_value = request.POST["category"]
listing_obj = AuctionListing(title = title, desc = desc, user = request.user,price = starting_bid, picture = photo_url,category=category_value)
listing_obj.save()
bid_obj = Bids(bid_value = starting_bid, listing = listing_obj, user = request.user)
bid_obj.save()
if request.method == 'POST':
form = CreateForm(request.POST, request.FILES)
if form.is_valid():
listing_obj = AuctionListing(title = form.cleaned_data["title"], desc =form.cleaned_data["desc"], user = request.user,price = form.cleaned_data["starting_bid"], picture = form.cleaned_data["photo"],category= form.cleaned_data["category"])
listing_obj.save()
bid_obj = Bids(bid_value = form.cleaned_data["starting_bid"], listing = listing_obj, user = request.user)
bid_obj.save()
return render(request,"auctions/index.html",{
"Listings" : AuctionListing.objects.all()
})
else:
return render(request,"auctions/create_listing.html",{
"categories":categories
})
"Form": CreateForm()
})
def bid(request, title):
"""
Used to bid on a listing

5
commerce/s3_storage.py Normal file
View File

@ -0,0 +1,5 @@
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False

View File

@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
]
MIDDLEWARE = [
@ -120,3 +121,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

View File

@ -15,8 +15,13 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("auctions.urls"))
]
if settings.DEBUG: # new
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB