1

I am writing a small blog and I will like to have a page through which I will be updating the website without going to the admin panel but it seems am not getting something right. I've written a view function but it is not responding yet. Am getting an error saying: The view music.views.upload didn't return an HttpResponse object. It returned None instead. Below is my models.py file

class UserUpload(models.Model):
    artist = models.CharField(max_length=300)
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(default='', blank=True, unique=True)
    thumbnail = models.ImageField(blank=False)
    audio_file = models.FileField(default='')
    music_tag = models.ManyToManyField(MusicTag)
    uploaded_date = models.DateTimeField(default=timezone.now)
    page_views = models.IntegerField(default=0)
    moderation = models.BooleanField(default=False)

    class Meta:
        ordering = ['-uploaded_date']

    def save(self):
        self.uploaded_date = timezone.now()
        self.slug = slugify(self.title)
        super(UserUpload, self).save()

    def __str__(self):
        return self.title + ' by ' + self.artist

    def get_absolute_url(self):
        return reverse('music:detail', kwargs={'slug': self.slug})

This is my views.py file

def upload(request):
    if request.method == 'POST':
        AUDIO_FILE_TYPE = ['wav', 'mp3', 'ogg']
        IMAGE_FILE_TYPE = ['png', 'jpg', 'jpeg']
        form = UserMusicForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            form.save(commit=False)
            form.artist = request.POST.get('artist')
            form.title = request.POST.get('title')
            form.thumbnail = request.POST.FILES('thumbnail')
            form.audio_file = request.POST.FILES('audio_file')
            form.music_tag = request.POST.get('music_tag')
            messages.success(request, 'has been successfully uploaded')

            if form.thumbnail.url.split('.')[-1] not in IMAGE_FILE_TYPE:
                context = {
                    "form": form,
                    "message": "Image file must be PNG, JPG, or JPEG"
                }
                return render(request, "music/upload1.html", context)

            if form.audio_file.url.split('.')[-1] not in AUDIO_FILE_TYPE:
                context = {
                    "form": form,
                    "message": "Audio file must be WAV, MP3, or OGG"
                }
                return render(request, "music/upload1.html", context)
            print("Remaining save")
            form.save()
            return redirect('/')
    else:
        form = UserMusicForm(request.POST or None, request.FILES or None)
        context = {
            "form": form,
            "title": "Upload Your Song",
        }
        return render(request, "music/upload1.html", context)

This is my html file

<div class="login-box">
    <h2>Upload Your Song</h2>
    <form action="" method="POST">{% csrf_token %}
        <div class="user-box">
            <input type="text" name="artist" required="required">
            <label>Artist</label>
        </div>
        <div class="user-box">
            <input type="text" name="title" required="">
            <label>Title</label>
        </div>
        <div class="user-box">
            <input type="file" name="thumbnail" required="">
            <label>Thumbnail</label>
        </div>
        <div class="user-box">
            <input type="file" name="audio_file" required="">
            <label>Audio file</label>
        </div>
        <div class="user-box">
            <input type="text" name="music_tag" required="">
            <label>Music tag</label>
        </div>
        <button style="background: linear-gradient(360deg, transparent, #03e9f4);" type="submit">Submit</button>
    </form>
</div>

Below is my form

class UserMusicForm(forms.ModelForm):

    class Meta:
        model = UserUpload
        fields = ('artist', 'title', 'thumbnail', 'audio_file', 'music_tag')

1 Answer 1

1

You don't have an else statement for if form.is_valid():, causing the code to return nothing (None) when your form is invalid. You should return an error response in that case. You can access the errors using Form.errors (docs).

Sign up to request clarification or add additional context in comments.

2 Comments

Why is the form returning None? What am I not getting right
Because your form is invalid, which means that the data you send to the server is not correct according to the form. Either manually check the body that you are sending or use the error info as outlined in my answer to see what's wrong with the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.