Hi,
I tried to create a form that can save images.
All the text input is working and they are saved into the ‘admin’.
But for some reason, the image is not saved.
Here are my codes in app models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
STATUS = (
(0,"Draft"),
(1,"Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE, related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
image = models.ImageField(null= True, blank=True, upload_to="images/")
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
def get_absolute_url(self):
from django.urls import reverse
return reverse ('blog')
My app forms.py
class PostCreateForm(forms.ModelForm):
class Meta:
model = Post
fields = [
'title',
'content',
'author',
'status',
'image',
]
widgets = {
'title': forms.TextInput(attrs= {'class':'form-control'}),
'content': forms.Textarea(attrs= {'class':'form-control'}),
'author': forms.Select(attrs= {'class':'form-control'}),
'status': forms.Select(attrs= {'class':'form-control'}),
'image': forms.FileInput(attrs= {'style':'display: none;','class':'form-control', 'required': False,})
}
my settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
my app urls.py
from blog import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.PostList.as_view(), name='blog'),
path('view/<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('edit/<slug:slug>/', views.PostUpdateView.as_view(), name='post_edit'),
path('create/', views.PostCreateView.as_view(), name='create'),
path('delete/<slug:slug>/', views.PostDeleteView.as_view(), name='post_delete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my html file if you need it
<div class="container-fluid pt-5">
<h2 class="text-center padding text-primary">Create new Article</h2>
<form method='POST' enctype="multipart/form-data" class="main-form"> {% csrf_token %}
<div class="row">
<div class="col">
{%for field in form%}
<div class="form-group">
<label class="mylabel">{{ field.label }}</label>
{{field}}
</div>
{%endfor%}
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label"> Upload image {{form.image}}</label>
</div>
</div>
<input type='submit' value='Post Article' class="btn btn-primary" >
</div>
</div>
</form>
</div>
Note that all of the forms are working fine except uploading the image.
When clicking, I can still choose file, and it seems the image name is there. But after I clicked save, it is not stored.
Thank you in advance.
Edit: Removed the spacing