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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 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