How to store images that was select in a form

16 views
Skip to first unread message

kheinri...@gmail.com

unread,
Oct 11, 2018, 5:24:55 AM10/11/18
to Django users
Hi,


I have a problem about storing images
that I selected through a Form  into some random folder.

How can I do that?

Also I want to show this selected image on the website.

What should I do?


Here my code:


#Views.py
from django.shortcuts import render
from django.views.generic import TemplateView
from .forms import menuForm
from .models  import ImageMenu


class HomeView(TemplateView):
   
   
def get (self,request):
        frm
= menuForm()
        ima
= ImageMenu.objects.all()
       
       
return  render(request, 'base.html',   {'form':frm, 'im1' : ima})

   
def post(self, request):
        image
= menuForm(request.POST)
       
if image.is_valid():
            form
.save()
            image
= form.cleaned_data['im']
        args
= { 'image' : image}
       
return  render(request, 'base.html', args )
   


#urls
.py
---------------------------------------------------------
from django.urls import path
from ledapp.views import HomeView

urlpatterns
= [
    path
('', HomeView.as_view(), name = 'home' ),
   
]
----------------------------------------------------------

#settings.py
----------------------------------------------------------
STATIC_URL
= '/static/'


MEDIA_URL
= '/content/'


MEDIA_ROOT
=os.path.join(BASE_DIR,  'content' )
----------------------------------------------------------


#forms.py
------------------------------------------------------------
from django import forms

class  menuForm(forms.Form):
    im = forms.ImageField(max_length = 20)
   
   
   
   
class mForms(forms.Form):
        ims= forms.ImageField(max_length = 20)

------------------------------------------------------------       
   

#html-File
   
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto auto auto;
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}


.grid-container .item6 {
  background-color: #2196F3;
  text-align: left;
  padding: 15px 0;
  font-size: 20px;
}


.grid-container .item7 {
  background-color: #2196F3;
  text-align: left;
  padding: 15px 0;
  font-size: 20px;
}
.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 20px;
}

.item2 {
  grid-area: 2 /1 / 2/ 1;
}
.item4 {
  grid-area: 2 /4 / 2/ 4;
}


.item6 {
  grid-area: 5 / 3 / 5 / 2;
}
.item7 {
  grid-area: 6 / 3 / 5 / 3
}

.item8 {
  grid-area: 1 / 2 / 5 / 2;
}
.item9 {
  grid-area: 1 / 3 / 5 / 3;
}

</style>
</head>

<body bgcolor = 00003f>

<body>

<h1>Menu</h1>

<div class="grid-container">
  <div class="item1">Senden</div>
  <div class="item2">Aktivieren</div>
 
  <div class="item3">Helligkeit</div> 
  <div class="item4">Bildwechselfrequenz</div>

  <div class="item6">
                 <form enctype="multipart/form-data" method="post"
                  {{form }}                  {% csrf_token %}
                <button type "submit">Submit</button>                     
         
                  </form>
  </div>
 
  <div class="item7">  </div>
  <div class="item8">  {{image.pic.url}} </div> 
  <div class="item9">9</div>
 
</div>

</body>
</html>









PASCUAL Eric

unread,
Oct 11, 2018, 6:51:39 AM10/11/18
to Django users

Hi,


Did you study the documentation about the media and how to handle them ? It is quite explicit and if I remember well there are a couple of code examples.


Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of kheinri...@gmail.com <kheinri...@gmail.com>
Sent: Thursday, October 11, 2018 11:24:55 AM
To: Django users
Subject: How to store images that was select in a form
 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f6d7b970-8256-40ad-8b63-a5e266d416ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kheinri...@gmail.com

unread,
Oct 11, 2018, 8:45:48 AM10/11/18
to Django users
yes,

i read a lot about that topic

but it doesn't help me.
Reply all
Reply to author
Forward
0 new messages