file upload form
This commit is contained in:
parent
bf1d266890
commit
acbca41040
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe"
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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'
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ class AuctionListing(models.Model):
|
||||||
user = models.ForeignKey(User,on_delete = models.CASCADE)
|
user = models.ForeignKey(User,on_delete = models.CASCADE)
|
||||||
price = models.DecimalField(max_digits = 10,decimal_places = 2)
|
price = models.DecimalField(max_digits = 10,decimal_places = 2)
|
||||||
desc = models.CharField(max_length = 1000)
|
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)
|
category = models.CharField(max_length = 64,choices=category_choices)
|
||||||
date_added = models.DateTimeField(auto_now_add=True)
|
date_added = models.DateTimeField(auto_now_add=True)
|
||||||
closed = models.BooleanField(default=False)
|
closed = models.BooleanField(default=False)
|
||||||
|
|
|
@ -2,33 +2,15 @@
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
|
||||||
|
<div class="form-group" style="margin-left: 20px">
|
||||||
<h2>Create Listing</h2>
|
<h2>Create Listing</h2>
|
||||||
|
|
||||||
|
|
||||||
<form action="{% url 'create' %}" method="post">
|
<form action="{% url 'create' %}" method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="form-group">
|
|
||||||
<input class="form-control" autofocus type="text" name="title" placeholder="title">
|
{{ Form.as_p }}
|
||||||
</div>
|
<input class="btn btn-primary" type="submit" value="Create Listing"></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>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -20,7 +20,7 @@
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<img src = "{{Listing.picture}}" width = "50%" height= "50%"></img>
|
<img src = "{{ Listing.picture.url }}" width = "50%" height= "50%"></img>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<p style="font-size: large;">{{Listing.desc}}</p>
|
<p style="font-size: large;">{{Listing.desc}}</p>
|
||||||
|
|
|
@ -5,19 +5,11 @@ from django.shortcuts import render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django import forms
|
from django import forms
|
||||||
from .models import User,AuctionListing,Comment,Bids
|
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):
|
def watch(request,title):
|
||||||
"""
|
"""
|
||||||
|
@ -170,27 +162,21 @@ def create(request):
|
||||||
"""
|
"""
|
||||||
Used to create a Listing
|
Used to create a Listing
|
||||||
"""
|
"""
|
||||||
if request.method == "POST":
|
if request.method == 'POST':
|
||||||
title = request.POST["title"]
|
form = CreateForm(request.POST, request.FILES)
|
||||||
desc = request.POST["desc"]
|
if form.is_valid():
|
||||||
starting_bid = request.POST["starting_bid"]
|
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"])
|
||||||
photo_url = request.POST["photo"]
|
listing_obj.save()
|
||||||
|
bid_obj = Bids(bid_value = form.cleaned_data["starting_bid"], listing = listing_obj, user = request.user)
|
||||||
# alternate photo to display
|
bid_obj.save()
|
||||||
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()
|
|
||||||
return render(request,"auctions/index.html",{
|
return render(request,"auctions/index.html",{
|
||||||
"Listings" : AuctionListing.objects.all()
|
"Listings" : AuctionListing.objects.all()
|
||||||
})
|
})
|
||||||
else:
|
|
||||||
|
else:
|
||||||
return render(request,"auctions/create_listing.html",{
|
return render(request,"auctions/create_listing.html",{
|
||||||
"categories":categories
|
"Form": CreateForm()
|
||||||
})
|
})
|
||||||
def bid(request, title):
|
def bid(request, title):
|
||||||
"""
|
"""
|
||||||
Used to bid on a listing
|
Used to bid on a listing
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
from storages.backends.s3boto3 import S3Boto3Storage
|
||||||
|
|
||||||
|
class MediaStorage(S3Boto3Storage):
|
||||||
|
location = 'media'
|
||||||
|
file_overwrite = False
|
|
@ -38,6 +38,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'storages',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
@ -120,3 +121,6 @@ USE_TZ = True
|
||||||
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
|
|
||||||
|
MEDIA_URL = '/media/'
|
||||||
|
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||||
|
|
|
@ -15,8 +15,13 @@ Including another URLconf
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
from django.conf import settings
|
||||||
|
from django.conf.urls.static import static
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path("", include("auctions.urls"))
|
path("", include("auctions.urls"))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if settings.DEBUG: # new
|
||||||
|
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
BIN
db.sqlite3
BIN
db.sqlite3
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 |
Loading…
Reference in New Issue