hello,
I have made a custom adminpannel were the user need to update a picture to a spasefic model by using django forms like so -
model -
class course(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=500)
title_description = models.CharField(max_length=250,default="")
course_slug = models.CharField(max_length=50,default="")
price = models.FloatField()
blocked_by = models.ManyToManyField('course',related_name="blocking",blank=True,default=None)
image = models.ImageField(upload_to="uploads",blank=True,default=None)
course_sections = models.ManyToManyField(course_section,related_name="course",blank=True)
autor = models.ForeignKey(NewUser, related_name='writen_courses',
on_delete=models.DO_NOTHING, blank=True, null=True)
degree = models.ManyToManyField(degree,related_name="courses", blank=True)
display = models.BooleanField(default=True)
model form -
class courseForm(ModelForm):
class Meta:
model = course
exclude=('one_to_one_course','course_sections')
widgets = {
"description": forms.Textarea()
}
html form
{% csrf_token %}
<div class="input_double">
<div class="input-con">
<span class="input-name">שם הקורס</span>
{{course_form.name}}
</div>
<div class="input-con">
<span class="input-name">מספר קורס</span>
{{course_form.course_slug}}
</div>
</div>
<div class="input_single">
<div class="input-con">
<span class="input-name">תיאור הקורס</span>
{{course_form.description}}
</div>
</div>
<div class="input_double">
<div class="input-con">
<span class="input-name">משפט מפתח</span>
{{course_form.title_description}}
</div>
<div class="input-con">
<span class="input-name">מחיר</span>
{{course_form.price}}
</div>
</div>
<div class="input_double extra_margin">
<div class="input-con autor_selected">
<span class="input-name">שם היוצר</span>
{{course_form.autor}}
<span class="input-name">תואר</span>
{{course_form.degree}}
<span class="input-name">מוכן להצגה</span>
{{course_form.display}}
</div>
<div class="input-con selected_div">
<span class="input-name">קורסים חוסמים</span>
{{course_form.blocked_by}}
</div>
</div>
<div class="full_width">
<div class="image_container">
<img style="background-image:url({% static 'main_website\images\graduation.png' %})" id="display_image" alt="">
</div>
<div class="input-con center_on_image">
{{course_form.image}}
<label for="upload"><i class="fas fa-upload"></i></label>
</div>
</div>
<div class="full_width_button">
<button class="cta" method="submit" name="sumbit_course">
<span>עדכן פרטים</span>
<svg width="15px" height="10px" viewBox="0 0 13 10">
<path d="M11,5 L1,5"></path>
<polyline points="4 1 0 5 4 9"></polyline>
</svg>
</button>
</div>
{{course_form.errors}}
</form>
view handling
course_form=courseForm(request.POST)
if course_form.is_valid():
if request.POST["course_id"]!="-1": # not a new course -> need to update exitsting one
updated_course = course.objects.get(id=request.POST["course_id"])
course_form=courseForm(request.POST,instance=updated_course)
new_course = course_form.save()
else: # saving new model
new_course = course_form.save()
return JsonResponse({"name":new_course.name,"id":new_course.id})
else:
print(course_form.errors)
now everything gets updated and the data is stored but when i try to acsses the image that was updated i get an erorr not found (http://127.0.0.1:8000/uploads/graduation.png 404 (Not Found)).
but, if i update it via django admin it is working.
its like the form dosent save the correct url…
and another question related to js and html.
i have an date input, and i want to render it to Date class when it is changed,
so i added an event lister
start_date_search.addEventListener(‘input’,filterList_payments);
and the function dose that
const start_date_filter = Date(start_date_search.value)
but insted of having the correct date, it gives me a random year but the day and month are correct.
any ideas?
thanks alot!