I want to save an image in the database with a Model and a Modelform, but if I save it there is no media folder or database entry.
class Entry(models.Model):
picture = models.ImageField(upload_to='file')
def ImageView(request):
form = ImageForm(request.FILES)
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home:index')
else:
context= {
'form': form
}
return render(request, 'home/image_form.html', context)
class ImageForm(ModelForm):
class Meta:
model = Entry
fields = ['picture']
app_name = 'home'
urlpatterns = [
path('image/', views.ImageView, name="image"),
]
urlpatterns = urlpatterns+static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
<form action="{% url 'home:image' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button class="button btn btn-dark" type="submit">Done</button>
<a href="" class="button btn btn-dark">Cancel</a>
</form>
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')