use Django views and DRF views together - django

I am new to Django REST Framework. i describe my question with a example.
for example, i have a api view function that can handle GET method and return a list of Users in json. the url of this api is www.exp.com/api/users
now i want a view that get list of Users, send them to a template and render the template. the url of this view is www.exp.com/users
logics of above views are same.
how can i do like above example?
should i write two seprate view (api and django view)?

If you are using Django to render front-end pages than you need to write two views. Otherwise, you can use other front-end technologies for rendering front-end pages such as React.js, Angular.js, Vue.js and so on.
You can use generic ListAPIView for listing users.
You could refer to Classy Django Rest Framework for more class based APIViews.

Related

On what level in the django MTV architecture does the Django Rest Framework work?

I understand that django is an MTV architecture. I also understand that M is models, T is templates (which are views in MVC) and V is views (which are controllers in MVC).
I want to understand if the django serializers and views are views or templates according to the MTV when using Django Rest Framework.
There's no perfect 1:1 mapping there.
A DRF serializer is vaguely like a template in that it describes how the data should be serialized "onto the wire" when a DRF view outputs it, but unlike templates, serializers can also be used for data ingestion.
DRF views (and viewsets, which group views) map to MVC controllers or Django views in that they describe how the API endpoint works.
A DRF app doesn't necessarily need to work with Django models at all, but if it does, that bit is unchanged from how Django works.

How to use Django Rest API in Django normal project for autocomplete using Select2?

In my Django project, I have a model named Product. The model consists of products which have following entities:
name, id, price and so on.
In my project, an admin can add a new/old product anytime.
Now, for searching, I want to add autocomplete. I want to use Select2.
So users don't have to memorize the name of the products. To do that I found out here in the Select2 doc
that :
Select2 comes with AJAX support built in, using jQuery's AJAX methods
With this, I can search an API and fetch the data to show the users in autocomplete search field.
My Question:
Should I create a Django rest API and use that API to store products and fetch the data?
1.1 Would it be wise?
1.2 Is it possible to make a rest API within a normal Django project? If not then how to do that?
Or should I just use a normal urls.py and querying the result from
Select2 ajax function to that urls.py and to a custom query.py and
fetch the data directly from the database?
I don't think using rest framework with normal Django project would cause any problem. You are just adding some extra urls, that's all. It won't cause any problem to your project. Moreover, you can use the API to get json data of various models.
Hope this helps.

how to use django restframework as a backend for mobile apps

I need to built an "API". Using django restframework. API has to support multiple platforms like mobile apps, webapps.
API will be used as a backend which will store all information. But my problem is how do I access users information using API. I mean normally django has user model. And we access user related stuff using request.user. But how do I access request.user information using API. Please pardon me for asking such question. But as I am new to developing API for Mobile apps. I am facing difficulty.
I think the easiest way to think about it, is that Django Rest Framework will (normally) return or process JSON data, rather than an HTML page / HTML form data.
Your models stay the same.
If you use Django's ModelForms then DRF's ModelSerialzers are very similar in use.
Likewise, using Django's class based generic views, are very similar to DRF's generic views are very similar - except rather than processing POST data from an HTML forms, they will receive JSON data. The generic views cover the same things - create via POST, update via PUT, delete via DELETE.
Like I say the main difference is that you will be dealing with JSON in place of HTML.
(You could easily use bog standard Django views without the rest-framework and return or process JSON. DRF takes a fair bit of the boilerplate code out of the process).

Dealing with Django model forms + REST Framework with Angularjs Single Page Application

I'm trying to implement a SPA in my existing Django project with Angularjs. I have setup the start of a API using Django Rest Framework to retrieve my data in angular.
Some of my django model forms have a lot of complexity in them and would love to somehow pass it to the client side.
I could be wrong here but since I'm setting this project up as a SPA, once I send the user to the project from my django urls.py, then the angular routing via $routeProvider take over and talk to all my angular stuff to get data (controllers, services, etc.,) via the REST api. This then prevents me from sending my form over via context.
Every example I've seen with angularjs and django forms always explicitly writes out each field in the form. Is there anyway for me to get my django model form that has a bunch of code in its __init__ method, over to the client side without having to set each field manually?
I've never tried this, but here's how I'd start on trying to do it.
You can use the in-built as_p(), as_ul() and as_table() methods of the form to pass the form HTML from the REST API, e.g.
class MyApiView(ApiView):
def get(self, request, format=None):
form = MyForm()
response['form_html'] = my_form.as_ul()
return Response(response)
You would then need to build the form skeleton and submit buttons in Angular, then insert form_html as the form body just as you would do in a standard django template.
Disclaimer: It's been a while since I've properly used django-rest-framework so the syntax in my example mught not be correct, but the basic idea is there

Backbone.js and Django (Without Tastypie)

I'm using Django for my site and trying to incorporate backbone.js. Backbone encourages using Tastypie - but I would rather not. Is there a way to use backbone.js and django without tastypie? Are there any examples out there of how to do this?
I've been were you are. Needed to just make a custom API for backbone to read for the specific instances.
All that really means, is making custom views in your views.py and attaching them to custom urls in urls.py for backbone. Your views will have to return a JSON version of the object or objects
So you end up with friendly looking urls that backbone likes
For example if I had a model of boxes and I want to write a url and a view that sends all the boxes in my database to my frontend delivering them to backbone - I could make a url like this /api/v1/box/all/ really anything you want. In your view you just need to remember to return JSON.
Remember - you need update views to to update from backbone syncings (tastypie PUTS)
something like /api/v1/box/3/update?updatedinfodata
Let me know if you would like me to expand or show some code.
It is possible to bot use TastyPie and just build your own API.
You just need to know Backbone sends to the API and data it expects to receive.