Django setup url parameter for certain requests only - python

So I'm trying to setup my Django view such that when they POST to an endpoint they don't need an url parameters, but when they call GET they require it
path('posts/', views.PostView.as_view(), name="post"),
path('posts/<int:count>/', views.PostView.as_view(), name="post"),
The issue with this is that it makes swagger messy and swagger with assume that url/posts/ you can POST and GET from, and url/posts// you can as well, but I only want to be able to POST to the first and GET from the 2nd. Is there a way to do this without creating separate view classes for them? Or is it better practice in this case to create separate view classes?

Related

Consuming External API Using Django Generic CBVs

If I want to consume an external API within a generic class based view in Django, can I add a method to the generic view including a get request? I need to pass parameters from the data model in the request payload in order to illicit a response, and I fundamentally do not understand how to do this. I was hoping someone could point me in the direction of a tutorial or something to help me out, as I have done hours of searching and I cannot find anything that is within an existing CBV.

The responseMessages is not displaying in Django-Swagger integration

I am using DRF YASG to document my API.
I want to remove calls for PUT and PATCH from here. For the views here I have used viewsets.ModelViewSet.
Another app uses APIview for its views.py. How can I exclude methods for a particular url.
My view contains two methods POST and DELETE. But for implementing the methods I have seperate URLs.
The problem is that both the URLs show both methods and the list just gets too big.
The question might be similar but I have a constraint of not making changes in the previous code-base.

Calling REST endpoints in views?

I am using the Django REST framework package. I have several endpoints that I've already built to return data. I am calling these endpoints within Ajax to retrieve data. However, if a user does not have JavaScript enabled and presses let's say a subscribe button or a follow button for example, the action would be handled in the views.py file for that specific POST request. Would it be better to handle the request manually by doing a simple query in the views.py or is it better to just hand it off to the endpoint for that specific request inside the views.py?
I know I can use serialization in views.py, but which would be better using serialization or manually writing the queries to add the rows?

Django middleware advised not to use request.POST why?

"Accessing request.POST inside middleware before the view runs or in process_view() will prevent any view running after the middleware from being able to modify the upload handlers for the request, and should normally be avoided."
This is from the django documentation. First of all, if I just read the POST, without changing that, how does it even know and how does it prevent the view from doing it's business and second, how is a CsrfViewMiddleware different in that sense?
The warning comes from this ticket and this.
Django currently parses the POST data lazily, but middlware might try to access POST on a request and trigger parsing, even though the function itself never touches POST.
That would put high load on the machine if the POST data is rather large ...
It means that the view will be unable to set any custom upload handlers, perform custom parsing of the request body, or enforce permission checks prior to file uploads being accepted.
And the difference about CsrfViewMiddleware is stated clearly right below the said warning in the docs:
The CsrfViewMiddleware ... provides the csrf_exempt() and
csrf_protect() decorators which allow views to explicitly control at what point the CSRF validation should
occur.

Is it possible to call different function of class based views through different actions in template

I am new to django and i ahve gone through all the docs of django. right now if we give some link in template and defined that link in urls.py i.e which view is going to handle that link. like this url(r'^dashboard/gift/$', login_required(CouponPageView.as_view())),
But i have this little doubt can i call different function of a view on clicking different links present in template.
The idea behind a class-based view is not to serve multiple resources (the targets of the links in your template). The idea is that the class-based view implements methods for the various HTTP methods (i.e. get, post, put, delete, head).
So you can server an HTTP GET of a certain URI using the SomeView.get() method, or you can handle a POST to the same resource from the post() method in the same SomeView class. This is helpful to support object oriented code, as the different methods on the object will typically share some resources.
If you want to handle different URL's, write different View classes. If their functionality is similar, use inheritance to prevent code duplication. If their functionality is almost identical, use parameters in the urlpattern.
I think you need to study the URL dispatcher a little more: https://docs.djangoproject.com/en/dev/topics/http/urls/

Categories