Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest shows TemplateDoesNotExist

I am trying to learn django rest on a existing blog project. My project has a model named "BlogPost". There I have created an endpoint to GET the blogpost object. I can create a blog successfully from django admin, but whenever I give the url "http://127.0.0.1:8000/api/postings/1/", it shows the error "TemplateDoesNotExist at /api/postings/1/", while there isn't any error in my terminal. it shows "GET /api/postings/1/ HTTP/1.1" 500 85630. Can anyone help me where am I doing wrong?

from django.conf import settings
from django.db import models
from django.urls import reverse

class BlogPost(models.Model):
    # pk aka id --> numbers
    user        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 
    title       = models.CharField(max_length=120, null=True, blank=True)
    content     = models.TextField(max_length=120, null=True, blank=True)
    timestamp   = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.user.username)

views.py:

from rest_framework import generics
from postapp.models import BlogPost
from .serializers import BlogPostSerializer

class BlogPostRudView(generics.RetrieveUpdateDestroyAPIView):
    lookup_field = 'pk'
    serializer_class = BlogPostSerializer

    def get_queryset(self):
        return BlogPost.objects.all()

Here is the serializer:

from rest_framework import serializers   
from postapp.models import BlogPost

class BlogPostSerializer(serializers.ModelSerializer):
    class Meta:
        model = BlogPost
        fields = [
            'pk',
            'user',
            'title',
            'content',
            'timestamp',
        ]

My urls.py inside the api

from .views import BlogPostRudView

from django.urls import path

urlpatterns = [
    path('<int:pk>/', BlogPostRudView.as_view(), name='post-rud')

]

and my urls.py inside the project:

from django.contrib import admin
from django.urls import path, include

app_name = "testproject"

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/postings/',include('postapp.api.urls')),

]
like image 970
Proteeti Prova Avatar asked Jan 22 '26 22:01

Proteeti Prova


2 Answers

Make sure to add rest_framework to INSTALLED_APPS in your project's settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    ................

You may also need to run migrations after adding it.

Everything about your code seems alright

like image 117
crazychukz Avatar answered Jan 25 '26 12:01

crazychukz


Django rest framework will provide a template by default. i wonder if something is not done right when setting up the application? Possibly template paths? Have you tried hitting the endpoint with a REST client or curl? Something like:

curl -X "GET" "http://127.0.0.1:8000/api/postings/1" -H "Accept: application/json"

I would expect the above to work. In that case have a look at your template paths (although the HTML template is not required for a REST API.

like image 43
Giannis Avatar answered Jan 25 '26 11:01

Giannis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!