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:

Eleventy Adoption Guide: Overview, Examples, And Alternatives

Eleventy adoption guide: Overview, examples, and alternatives

Eleventy (11ty) is a compelling solution for developers seeking a straightforward, performance-oriented approach to static site generation.

Nelson Michael
May 7, 2024 â‹… 8 min read
6 CSS Tools For More Efficient And Flexible CSS Handling

6 CSS tools for more efficient and flexible CSS handling

Explore some CSS tools that offer the perfect blend of efficiency and flexibility when handling CSS, such as styled-components and Emotion.

Fimber Elemuwa
May 7, 2024 â‹… 7 min read
Leveraging React Server Components In Redwoodjs

Leveraging React Server Components in RedwoodJS

RedwoodJS announced support for server-side rendering and RSCs in its Bighorn release. Explore this feature for when it’s production-ready.

Stephan Miller
May 6, 2024 â‹… 9 min read
Exploring The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 â‹… 13 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