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:

TypeScript logo over a pink and white background.

Drizzle vs. Prisma: Which ORM is best for your project?

Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.

Temitope Oyedele
Nov 21, 2024 â‹… 10 min read
Practical Implementation Of The Rule Of Least Power For Developers

Practical implementation of the Rule of Least Power for developers

It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.

Timonwa Akintokun
Nov 21, 2024 â‹… 8 min read
Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 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