How to store images that was select in a form

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to store images that was select in a form

kheinrichdjango
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>









--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: How to store images that was select in a form

PASCUAL Eric

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: [hidden email] <[hidden email]> on behalf of [hidden email] <[hidden email]>
Sent: Thursday, October 11, 2018 11:24:55 AM
To: Django users
Subject: How to store images that was select in a form
 
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>









--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/VI1P193MB0432BACD6A8F00031C18DCA08CE10%40VI1P193MB0432.EURP193.PROD.OUTLOOK.COM.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to store images that was select in a form

kheinrichdjango
In reply to this post by kheinrichdjango
yes,

i read a lot about that topic

but it doesn't help me.

Am Donnerstag, 11. Oktober 2018 11:24:55 UTC+2 schrieb [hidden email]:
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>









--
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 [hidden email].
To post to this group, send email to [hidden email].
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/1713441f-cbd5-4a28-8e44-e63309936979%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.