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