Jul-12-2020, 11:59 AM
I change my code to incorporate crispy_forms.
Anyways, this is what my final code looks like so far:
models.py:
after clicking the Add button, it gives me an error saying no file was uploaded for the album_logo field, this field is required. I don't know why it is complaining as this is not true, I did upload an image from my desktop into this field.
This is the form:
Anyways, this is what my final code looks like so far:
models.py:
from django.db import models from django.urls import reverse # Create your models here. class Album(models.Model): artist = models.CharField(max_length=250) album_name = models.CharField(max_length=500) genre = models.CharField(max_length=100) album_logo = models.ImageField() def get_absolute_url(self): return reverse('music:detail', kwargs={'pk':self.pk}) def __str__(self): return self.album_name + " - " + self.artist class Song(models.Model): album = models.ForeignKey(Album, on_delete=models.CASCADE) file_type = models.CharField(max_length=100) song_title = models.CharField(max_length=250) is_favourite = models.BooleanField(default=False) def __str__(self): return self.song_titleviews.py:
from django.views import generic from .models import Album, Song # Create your views here. class IndexView(generic.ListView): template_name = 'music/index.html' queryset = Album.objects.all() context_object_name = 'all_albums' class DetailView(generic.DetailView): model = Album template_name = 'music/detail.html' class AlbumCreate(generic.CreateView): model = Album fields = ['artist', 'album_name', 'genre', 'album_logo'] def form_valid(self, form): return super().form_valid(form)album_form.html:
{% extends 'music/base.html' %} {% load crispy_forms_tags %} {% block title %}Add a New Album{% endblock %} {% block body %} <form method="post"> {% csrf_token %} {{ form|crispy }} <input type="submit" value="Add"> </form> {% endblock %}The issue I am having now is that when I fill in the form and upload a .jpg image in the album_logo field,
after clicking the Add button, it gives me an error saying no file was uploaded for the album_logo field, this field is required. I don't know why it is complaining as this is not true, I did upload an image from my desktop into this field.
This is the form: