2022-03-03
1917
#django
Gaurav Singhal
32278
Mar 3, 2022 â‹… 6 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:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 â‹… 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 â‹… 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 â‹… 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 â‹… 8 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