Reputation: 1
I am creating a spotify like backend and I am trying to create a track So it would take a POST request I tried bunch of different approaches through stack overflow and any resource I could get to.
POST http://127.0.0.1:8000/music/add/
Content-Type: application/json
Authorization: token 3e356c812614c3d4344e0d06773c27387b1e4c12
{
"title": "Evil Ways",
"duration": "00:03:47",
"release_date": "2023-09-03",
"audio_file": "/media/tracks/Music_Library.musiclibrary.zip",
"artist": 2,
"album": 2,
"genre": 1
}
I am sending the request through a .rest file and I get the response
HTTP/1.1 405 Method Not Allowed
Date: Thu, 08 Feb 2024 09:11:56 GMT
Server: WSGIServer/0.2 CPython/3.9.0
Content-Type: application/json
Vary: Accept, Cookie
Allow: OPTIONS, GET
X-Frame-Options: DENY
Content-Length: 41
{
"detail": "Method \"POST\" not allowed."
}
the end point is in music app's urls.py file
from django.urls import path
from . import views
urlpatterns = [
# path for get requests
path('',views.get_all_tracks, name='get_all_tracks'),
path('<str:track_name>/', views.get_tracks_by_name, name='get_tracks_by_name'),
path('add/', views.add_track),
]
my django app's urls file
from django.contrib import admin
from django.urls import path, include
from .views import (
home_page,
)
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
path('auth/', include('authentication.urls')),
path('music/', include('music.urls')),
path('album/', include('album.urls')),
path('artist/', include('artist.urls')),
path('playlist/', include('playlist.urls'))
]
In my views.py file I have
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from .models import Track, Artist, Genre, Album
from .serializers import TrackSerializer
from rest_framework.decorators import api_view, authentication_classes , permission_classes
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
# ... other views
@api_view(['GET', 'POST'])
@authentication_classes([SessionAuthentication, TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_track(request):
if request.method =="POST":
title = request.data.get('title')
artist_id = request.data.get('artist')
album_id = request.data.get('album')
duration = request.data.get('duration')
release_date = request.data.get('release_date')
genre_id = request.data.get('genre')
audio_file = request.data.get('audio_file')
# Create a new Track object
track = Track.objects.create(
title=title,
artist_id=artist_id,
album_id=album_id,
duration=duration,
release_date=release_date,
genre_id=genre_id,
audio_file=audio_file
)
# Return a JSON response with the created track data
data = {
'title': track.title,
'artist': track.artist.name,
'album': track.album.title,
'duration': str(track.duration),
'release_date': str(track.release_date),
'genre': track.genre.name,
'audio_file': track.audio_file.url, # Assuming 'audio_file' is a FileField
}
return JsonResponse(data, status=201)
else:
return "this page needs a POST request"
the models file looks like this
from django.db import models
from artist.models import Artist
from album.models import Album
class Genre(models.Model):
name = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.name
class Track(models.Model):
title = models.CharField(max_length=200)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
album = models.ForeignKey(Album, on_delete=models.CASCADE)
duration = models.DurationField()
release_date = models.DateField()
genre = models.ForeignKey(Genre, on_delete=models.CASCADE)
audio_file = models.FileField(upload_to='tracks/')
# Add other track-related fields (e.g., lyrics)
def __str__(self):
return self.title
I have a serializer file but I'll keep it out for now. I have been beating my head against the wall for 2 days so any help is appreciated
I am trying to create a new Track and get a Status 201 from the server I need to atleast the server not to return me error 405 so that I can go ahead and complete the POST requests and adding data.
Upvotes: 0
Views: 136
Reputation: 1
in music app url I have
from django.urls import path
from . import views
urlpatterns = [
# path for get requests
path('',views.get_all_tracks, name='get_all_tracks'),
path('<str:track_name>/', views.get_tracks_by_name, name='get_tracks_by_name'),
path('add/', views.add_track),
]
instead it should be
from django.urls import path
from . import views
urlpatterns = [
# path for get requests
path('',views.get_all_tracks, name='get_all_tracks'),
# your add should come before it goes to the next URL in this list
path('add/', views.add_track),
# if the path below comes first no track will get past it as it is a string :-)
path('<str:track_name>/', views.get_tracks_by_name, name='get_tracks_by_name'),
]
Upvotes: 0
Reputation: 56
Try registering path('', home_page) to the last of your urlpatterns list after path('playlist/', include('playlist.urls')). If it still doesn't work then please share all other paths registered in the music app's urls.py and also share all other views of the views.py
Upvotes: 1