models.py:
class Employee(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.EmailField(max_length=254)
phone_number = models.PositiveIntegerField(default=None)
address = models.CharField(max_length=200)
position = models.CharField(max_length=200)
hire_date = models.DateField(auto_now_add=False)
salary = models.PositiveIntegerField(default=1000)
is_manager = models.BooleanField(default=False)
personal_image = models.ImageField(blank=True, null=True, upload_to='images/')
password = models.CharField(max_length=200)
def __str__(self):
return str(self.pk) + " " + self.first_name + " " + self.last_name
forms.py:
class change_image(ModelForm):
class Meta:
model = Employee
fields = ['personal_image']
exclude = ('email', 'first_name', 'last_name', ' phone_number', 'address', 'position', 'hire_date', 'salary',
'is_manager', 'password')
views.py:
if request.method == 'POST':
form_4 = change_image(request.POST or None,request.FILES or None)
if form_4.is_valid():
change_4 = form_4.cleaned_data
emp = Employee.objects.filter(id=x.id)
emp.update(personal_image=request.FILES['personal_image'])
return redirect('/main')
settings.py:
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'employees/')
URLS.py:
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
HTML код:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group" style="width:500px;">
<label>Upload a new suitable profile picture showing at least your face.</label>
{{form_4}}
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
Что мне делать, чтобы решить эту проблему, я могу изменить ее, но когда Я кликаю на него из базы данных, там написано, что изображение не существует