I want to find out analytics and create an user story each time different views (Django rest framework views) are called (as endpoints) by a client (iPhone app)
I found out here : http://mixpanel-celery.readthedocs.org/en/latest/introduction.html to do this using mixpanel-celery
So, it involves me having to write a few lines of almost the same code (i.e. the name of the view as the name of the event (like FeedView or NotificationView or ProfileView, the id and name of the request.user). Is there any way to follow DRY and refactor this code somewhere, or does a library like this exist in conjunction with django views /rest framework views?
result = EventTracker.delay(
'my_event',
{'distinct_id': 1},
token='YOUR_API_TOKEN',
)
result.wait()
Related
I am currently at the planning stage of an app that will consist of standard
Django part for supervisors that can perform all CRUD operations on employee users mostly add, delete and view statistics - viewed in browser (no frontend framework just using Djangos server side rendering), two step email authentication on each login, session based auth
DRF part for employees - API connected to mobile app, authentication based on device ID. (no username, or password)
DRF part for clients to contact the supervisors if employees do something wrong - Token or JWT based authentication using passcode delivered by mail.
I am not used to splitting Django projects into multiple sub-projects (or using same database for different projects) but it feels like every part of the project should be a standalone app due to different authentication type and the fact of simultaniousily using DRF with standard Django
Can anyone who had similar problem or has some experience, advise what should I do considering different authentications and overall different user types in this project? What would be pros and cons of going into solo or multiple projects? Thanks in advance!
You're asking for opinions, so don't be surprised if the question gets closed, but I'll answer with facts:
A split over different projects using the same database has the following issue: shared migrations. They all use built-in users, so require some standard apps to be enabled that have migrations and they won't run on the 2nd and 3rd project.
You're going to need a custom user model to support the device id authentication method: You need information that is not on the standard user model to be available at authentication time - the number 1 reason to create a custom user model. Ties into migrations and also a synchronization hell code-wise.
Django's Authentication Backends system allows for different authentication methods to exist at the same time, so there is no need to split anything. If you're worried about security, you can always use different hostnames and the Sites framework to add an extra layer of protection, but they would still use the same code.
DRF started as an addition to Django's view-based approach, not a replacement to make part of a project's code available as an API. While current usage is more "DRF or templates" this is a result of people increasingly becoming binary ("this" or "that") and wanting to be in the cool club, but has nothing to do with technical reasons. They can and always will be able to co-exist as they solve different problems. In fact, DRF's generic views make use of Django's CBV's and the built-in browsable API makes use of templates. Also, the admin is template/view based and it's convenient to develop the app or manage data with the built-in admin.
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.
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).
I am very new to the world of web development and MVC architectures. I am currently working on django which i believe is an MVC framework. if i am right, for a web application MVC implies
views is the front end
models is the backend
controllers are the glue between frontend and backend
well if the above is true and views are the only frontend parts what exactly is the function of frontend frameworks like backbone, angular? how exactly do they deploy the mvc concept? Also when building a simple blogging site which framework would be preferrable? and also are there instances of websites working on both frontend and backend mvc frameworks? please give examples. for clarification i went through this question: In the Model-View-Controller principle, what is the Frontend and what is the Backend? but couldnt understand fully. thank you
There is no one-on-one analogy of front- and backend and the MVC model. For example, the admin of a (Django) site is generally considered to be part of the backend - it is not the part of the site the user will see - but part of the admin is definitely the View part of the MVC model. Anything a normal web user sees and/or directly interacts with is part of the frontend, everything else is part of the backend.
Now what is the MVC framework used in Django? We have:
The Model: this is the part of the application that holds the state of the application. In Django, a big part of this is the database and it's abstraction layer, the Django models. Other parts are user sessions and the request variable.
The View: this is the part of the application that presents the state of the application to the user. Django views and templates are responsible for this. Any data you see when you open up the website is the View part of MVC. The overall presentation is also part of this.
The Controller: this is the part of the application that represents any action you, the user, takes. Django is not truly a separated MVC framework because the View part and the Controller part are so tightly interwoven: any link, form or button you see on the site is a controller. It tells the site to do an action, such as present a different view (e.g. a link), or change the state of the model (e.g. an edit form).
What about Backbone or Angular? Why do you need two different MVC frameworks in a single application?
Django is a server-side framework. Every action happens on the server. If you click a link or submit a form, you send a request to the server and the server sends back a complete, static response (static in the sense that the page doesn't change once it's in your browser). You can't use Django to use logic client-side, as it is a python framework that runs on your server, not in your client's browser. Instead, it's Javascript's job to add any client-side logic, e.g. to reorder a list of items on the page or to dynamically add a new one. Now each page can be seen as some kind of mini-application.
Backbone and Angular are examples of MVC frameworks for such client-side applications. It supplies the client-side application logic that a server-side framework such as Django lacks, and surprisingly the people who like an MVC framework to develop a server-side application generally also like to use an MVC framework to develop a client-side application.
Django is a kind-of hybrid version of the Model-View-Controller model. The Django documentation generally describes it as a Model-View-Template model instead. Generally, the template (Django HTML with template tags, etc.) is generally matched to the normal View, providing the user's view in the way of web pages. The view in Django generally takes the place of the Controller, as it works between the Model, taking data from databases and defining new objects, and the View, which in this case is the Template. The Model is the same as normal in Django, providing the definition for different objects. So while the Model-View-Controller is normally the model for most languages, it is more so a Model-View-Template model, with the View being different than it is normally. Read more below:
http://jeffcroft.com/blog/2007/jan/11/django-and-mtv/
I am looking at Django, and patterns for web development.
What I am favoring at the moment(2014-01) is.
Use Django (restful/json) as the MC, Model/controller, or backed data and logic.
The controller part in Django refers to business rules and access control.
then use a javascript framework and bit of html as the client side code. View/controller.
In practice, the client/browser loads a javascript program, View/controller that then does restful queries to the backed model/controller
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.