S
S
sergey199408082017-05-19 17:07:44
Django
sergey19940808, 2017-05-19 17:07:44

Django, how to add images to database?

Hello everyone, how to add adding images to the table? Here is the table:

class OurFoto(models.Model):
        image = models.ImageField(height_field=200, width_field=200)
        name = models.CharField(blank=False, max_length=100, unique=True)
       date_added = models.DateTimeField(auto_now_add=True)

       def __str__(self):
       return self.image

View:
def add_foto(request):
        title = 'Добавление изображения'

        form = None
        if request.method == 'GET':
            form = AddFotoForm()

        if request.method == 'POST':
            form = AddFotoForm(request.POST, request.FILES)

            if form.is_valid():
                form.save()
                return HttpResponseRedirect(reverse('index'))

    context = {'title': title, 'form': form}
    return render(request, 'repository_our_fotos/add_foto.html', context)

Where am I making a mistake? I can't add an image to the database, I send a POST request, returns to the add page and reports an error you didn't select a file

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question