am creating a donation web application. Users are able to fill out a form and there donation is submitted into the database, I was wondering how I can have the user submit a url of an image and save it in the database, and then render it on another page. For example, if the user fills out the form and submits a url like https://images.unsplash.com/photo-1481349518771-20055b2a7b24?ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8cmFuZG9tfGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80, how would I save this to the database as an Image and not a URL? I should then be able to render it out on another page.
Html Form:
<label class="label-input100" for="image">Image</label>
<div class="wrap-input100">
<input id="phone" class="input100" type="text" name="image" placeholder="Please enter the url of your image" required>
<span class="focus-input100"></span>
</div>
My view: (There is a lot of stuff here that do not pertain to my question, I just included it in case it is needed)
def donate(request):
if request.method == "POST":
title = request.POST['donationtitle']
phonenumber = request.POST['phonenumber']
category = request.POST['category']
quantity = request.POST['quantity']
location = request.POST['location']
description = request.POST['description']
date = datetime.datetime.now().date()
ins = Donation(title = title, phonenumber = phonenumber, category = category, quantity = quantity, location = location, description = description, user=request.user, date = date )
ins.save()
# New part. Update donor's stats.
UserDetail.objects.filter(user=request.user).update(donations=F('donations') + 1)
UserDetail.objects.filter(user=request.user).update(points=F('points') + (quantity * 2))
return HttpResponseRedirect( '/thankyou/', )
return render(request,'donate.html')
I know there has to be stuff saved in the model, my model is down bellow. It contains data from the form and has nothing started on the image yet.
class Donation(models.Model):
title = models.CharField(max_length=30)
phonenumber = models.CharField(max_length=12)
category = models.CharField(max_length=20)
quantity = models.IntegerField(blank=True, null=True,)
location = models.CharField(max_length=50, blank=True, null=True,)
description = models.TextField()
date = models.CharField(blank=True, null=True, max_length=999)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
blank=True,
null=True,
)
Thank you to everyone who helps me with my question, I really can't figure it out and I appreciate all help.