Reputation: 1654
I am simply trying to GET or PUT to my Django api. It all compiles fine but everytime I try to PUT or GET through the web interface i get the following messsage:
HTTP 404 Not Found
Allow: GET, PUT, PATCH, DELETE, OPTIONS
Content-Type: application/json
Vary: Accept
{
"detail": "Not found."
}
I very new to Django and I am not sure where I went wrong, here is my views.py file:
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions
from SecMeRe.models import Patient
from SecMeRe.serializers import PatientSerializer
# Create your views here.
class PatientViewSet(viewsets.ModelViewSet):
queryset = Patient.objects.all()
serializer_class = PatientSerializer
My modesl.py
from __future__ import unicode_literals
from django.db import models
import uuid
class Patient(models.Model):
firstName = models.CharField(max_length=255)
lastName = models.CharField(max_length=255)
dob = models.DateField()
firstRecorded = models.DateField()
uniqueHash = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
and my serializers.py
from SecMeRe.models import Patient
from rest_framework import serializers
class PatientSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Patient
fields = ('firstName', 'lastName', 'dob', 'firstRecorded', 'uniqueHash')
Upvotes: 0
Views: 734
Reputation: 804
You can't GET anything until there is a record in the database. It's hard to say without seeing your urls.py
definition as well, but I'm assuming you set up /api/SecMeRe/patient/
to be the list view of patients.
You would need to PUT at the detail view of patient, which would be the url /api/SecMeRe/patient/353/
where 353 is the new primary key of your new Patient record.
Similarly, you could then call GET on that URL: /api/SecMeRe/patient/353/
If you want to not care about choosing IDs then use POST instead of PUT, and you can hit the original url of /api/SecMeRe/patient/
.
The reason PUT requires you to call out the ID every time is because it's an idempotent resource. You could call PUT on the url 100 times, and it would have the same result, since you are specifying the ID for both creating it and updating it.
Upvotes: 1