0

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.

  • Does this answer your question? [How to add image url field to an html form in django](https://stackoverflow.com/questions/68353448/how-to-add-image-url-field-to-an-html-form-in-django) – Stephen P Jul 16 '21 at 00:32

1 Answers1

0

In your view, you send a request to get the image like in this answer, and then save the received binary in the image field so you can render it later.

Mohamed ElKalioby
  • 1,908
  • 1
  • 12
  • 13
  • This doesn't help me, I want to download the image from a url, I know this is possible in Django, the example you showed me is regular python. –  Jul 13 '21 at 11:49
  • You can still use regular Python in Django and as far I know, this can't be done in Django. because imagefield deals with image binary directly. – Mohamed ElKalioby Jul 13 '21 at 11:56
  • I have seen it done with Django before, and I know that it is possible. your answer does not answer my question. –  Jul 13 '21 at 11:59