2024-03-13
3098
#django
Gaurav Singhal
32278
Mar 13, 2024 â‹… 11 min read

How to create a REST API with Django REST framework

Gaurav Singhal Gaurav is a data scientist with a strong background in computer science and mathematics. As a developer, he works with Python, Java, Django, HTML, Struts, Hibernate, Vaadin, web scraping, Angular, and React.

Recent posts:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 â‹… 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 â‹… 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 â‹… 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 min read
View all posts

9 Replies to "How to create a REST API with Django REST framework"

    1. If you really know Django then you should appreciate drf because the only major difference is the serializer just like you would use modelform and forms.

  1. I passed the proper details into each of the views, but I am lost at the detailview page. My api is working perfectly save for the detailview page. I honestly am confused. Any help, please?

    # Project’s View.py
    class CommentGet(DetailView):
    model = Post
    template_name = “post_detail.html”

    def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context[“form”] = CommentForm()
    return context

    class CommentPost(SingleObjectMixin, FormView):
    model = Post
    form_class = CommentForm
    template_name = “post_detail.html”

    def post(self, request, *args, **kwargs):
    self.object = self.get_object()
    return super().post(request, *args, **kwargs)

    def form_valid(self, form):
    comment = form.save(commit=False)
    comment.post = self.object
    comment.save()
    return super().form_valid(form)

    def get_success_url(self):
    post = self.get_object()
    return reverse(“post_detail”, kwargs={“pk”: post.pk})

    class PostDetailView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
    view = CommentGet.as_view()
    return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
    view = CommentPost.as_view()
    return view(request, *args, **kwargs)

    # BlogAPi Views.py
    class CommentGet(DetailView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer

    def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context[“form”] = CommentForm()
    return context

    class CommentPost(SingleObjectMixin, FormView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer

    def post(self, request, *args, **kwargs):
    self.object = self.get_object()
    return super().post(request, *args, **kwargs)

    def form_valid(self, form):
    comment = form.save(commit=False)
    comment.post = self.object
    comment.save()
    return super().form_valid(form)

    def get_success_url(self):
    post = self.get_object()
    return reverse(“post_detail”, kwargs={“pk”: post.pk})

    class PostDetailView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
    view = CommentGet.as_view()
    return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
    view = CommentPost.as_view()
    return view(request, *args, **kwargs)

  2. I got this error “‘model’ object is not iterable” when accessing Todo_api/

    Had to use .filter instead of .get

    def get_object(self, todo_id, user_id):
    ”’
    Helper method to get the object with given todo_id, and user_id
    ”’
    try:
    return Todo.objects.filter(id=todo_id, user = user_id)
    except Todo.DoesNotExist:
    return None

Leave a Reply