best way to deal with JSON in django - django

I am getting a JSON feed from a server and today I convert it to python object and thus to django view. We are now making an update of our site. whereby
the browser client should parse json using jQuery
also we will have a adobe-air app which will consume JSON directly
However I am not so keen on exposing my back-end server directly to browser/adobe client. How best way to go via django? any existing django-app?
regards
django-newbie

You can use certain built-in elements of Django but I've always found that SimpleJSON makes things so much easier.
Why? With straight serialisation, you don't want to show everything. So with the built-in methods, you have to cut a lot out. With SimpleJSON, you built a dict, fill it with only what you want shown and pump it through the SimpleJSON lib. I find inclusion a lot more secure than exclusion when it comes to exposing APIs.
It's also a lot more versatile for consuming data as your client isn't going to be a django site, it's an AIR app with its own ideas about how to format data (even within a spec like JSON there can and probably will be differences).
Oh and remember that there isn't a date type in JSON. (I only mention it because it caused me pain in the past)
Edit: (Thanks Cide) Django ships SimpleJSON in django.utils.simplejson but it might not be there forever. Regardless, you can download it separately from Pypi

Related

Do we need to render templates when using ReactJS as a front-end?

So I just created a website's front-end using ReactJS. Now all I need is a backend database that I will fetch data from using requests.
The question is whether I need to render templates using my backend or just use my server to make requests (eg get, post etc)
PS. I will be using Django as my backend.
Thank you everyone who will help me out.
Doing both is recommended. Based on the requirements and use cases we must use both ways to render.
For example, Some products use initial html as a Server side rendered page with all essential data required inserted as scripts and so on. This helps in loading primary content faster. If we are not following this in applications that require data initially. Then it might take more time to fetch React chunks, scripting and after seeing an API makes request, and then getting data and then displaying the primary content. So when a page needs more data (like More API calls) then server side rendering might be a good way.
For other scenarios like getting user details, All these can be done using React.
No, because you will use DRF (Django Rest Framework) to communicate between frontend and backend. Basically you will write your own APIs in the views.py that will respond with JSON data, at least in major of cases this will be enough. So, you don't need templates, since template are really Djangos' frontend, that you will not be using at all.
But, this heavily depends on what you are doing and what is your setup.

Using ETag for optimistic locking in a Django REST application

I'm trying to select a REST framework for Django that will allow me to easily use ETags for optimistic locking. I'm planning on examining Django-pistons and the Django Rest Framework libraries, but I'm open to any non-GPL solution (corporate licensing requirements prevent my using those).
My application is vending data from a SQLAlchemy model (not a Django model) in JSON/YAML form, and modulo the ETag issue is working just fine with the Django Rest Framework. However, I can't see an easy way to apply the ETag headers on my views.
For my views, I want to do this:
Given a response, easily add an ETag to the response headers I'm sending out on success. This must be calculated by me, since it will be model dependent; it's not enough to hash the response value or anything like that.
On POST/PUT, ensure that the ETag I'm receiving matches the one I sent out, or reject the request.
It's step 1 that is giving me a bit of trouble; I'm not sure which REST framework will make this easiest, nor am I sure what the best way to accomplish it is.
Django supports ETags through decorators (#etag, #last_modified, #condition) or middleware - you can check the docs. If you want to use those decorators with Django Rest Framework, you can use django-rest-framework-condition.
Choice of framework does not matter. In any case, you have to maintain an etag in cache(memcache or redis) representing the state of the resource. You can use Generational Caching Algorithm (https://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works). Then you can easily write a mixin that extracts the etag of every resource and sends it. In my personal experience, Django Rest Framework would be ideal for it due to its heavy flexibility and well-written code.

Is it hacky to manually construct JSON and manually handle GET, POST instead of using a proper RESTful API for AJAX functionality?

I started building a Django app, but this probably applies to other frameworks as well. In Backbone.js methods that call the server (fetch(), create(), destroy(), etc.), should you be using a proper RESTful API such as one provided by Tastypie or Django-Piston? I've founded it easier and more flexible to just construct the JSON in my Django Views, which are mapped to some URLs that Backbone.js can use. Then again, I'm probably not leveraging Tastypie/Django-Piston functionality to the fullest.
I'm not ready to make a full-fledged RESTful API for my app yet. I simply would like to use some of the AJAXy functionality that Backbone.js supports.
Pros/Cons of doing this?
Remember, REST does not equal JSON. If I require your representation in text/html, you should be able to provide me with that, or else throw a 415.
A better solution, then you are currently using, is to use the middleware functionality that Django provides. Whatever your view replies, use Djangos middleware functionality for the response to encode into JSON, XML or whatever.
I personally prefer defining my own ajax views and json objects. Using some already apis developed may be or may not be of much use. Some don't exactly fulfill the requirements some may have features which are redundant (And I don't like a code to be present which is not being used).
Also writing ajax functionality is not that difficult either. The inbuilt serializers / request.is_ajax features are there for your help.
Some examples for ajax implementation with django/jquery: http://webcloud.se/log/AJAX-in-Django-with-jQuery/ (You most probably have seen it already)

Pass array from HTML to Django application

I developed an application in JSPs and Servlets involving drop down menus that kept growing with how many authors per publication their were.
This was done in JavaScript and then in my application iterated through them using a loop. Is this possible using Django? This would be useful in my application.
This link might help you out if you don't want to dive into javascript (too much)
http://www.dajaxproject.com/
Or have a look at this stackoverflow question/awnser:
What is the best AJAX library for Django?
In any case, you need to serialize your array to a JSON string.
Then pass the JSON with an XMLHTTPRequest (ajax) to the server.
Add the javascript tag to your question if you don't mind more JS solutions.
Otherwise look for a Django Ajax framework to do the heavy lifting for you.

CodeIgniter & Datamapper as frontend, Django Admin as backend, database tables inconsistent

I created a database for a site i'm doing using Django as the admin backend. However because the server where the site is hosted on, won't be able to support Python, I find myself needing to do the front end in PHP and as such i've decided to use CodeIgniter along with Datamapper to map the models/relationship.
However DataMapper requires the tables to be in a specific format for it to work, and Django maps its tables differently, using the App name as the prefix in the table. I've tried using the prefix & join_prefix vars in datamapper but still doesn't map them correctly.
Has anyone used a combination of this? and if so how have the fixed the issue of db table names being inconsistent? Is there anything out there that i can use to make them work together?
----edit: clarification on backend---
Let me clarify: i'm going to be running the admin from a subdomain pointing to a python ready server. However i can't move the main domain name from the php only webserver because of certain constraints/binding contracts the company got itself in. and don't want to use cloaking/masking because of seo purposes.
i'm using the django admin because i'm using some packages to make a pretty/functional admin, such as grappelli for the admin template, along with its editor for editing news stories, etc. also using photologue to manage photos/galleries. etc.
If your problem is simply making Django use the same tables as your other software, use the db_column and db_table parameters in the models.
http://www.djangoproject.com/documentation/models/custom_columns/
Two apparent solutions:
Instead of hacking one or both to work well with each other, emulate the Django admin in PHP/CodeIgniter code. **
Get a server that supports Django. Make the frontend in Django.
Time-wise, either one of those solutions will be less involving than trying to make two different frameworks using different programming languages mesh well together. I can't imagine the future maintenance required to ensure everlasting compatibility and interoperability.
Also, I assume by saying:
I created a database for a site i'm doing using Django as the admin backend
You really mean that you modeled your apps using Django, and that you also intend on administrating the database that has resulted from this modeling in the Django admin. (In which case you already have your Models layer complete and should just try building the rest of the site in Django)
If that's the case then in your models you are going to need to define the exact column names (db_column) that DataMapper will expect, as well as manually define the table names (db_table), including M2M tables.
You may also have to define all of your primary keys manually, if DM expects something named differently.
Also:
If the server can't support Python, where are you going to be running your backend? Different server? Locally? This plan just isn't making a lot of sense.
** I would not suggest trying this. I had been attempting to make a CI backend that actually shared much of the same ideas as Django's admin, before I knew about Django's admin. And of course once discovering Django, I dropped the CI work immediately and continued on with what I have found to be a much more amazing framework that is much faster to develop on.
So as I understand you plan on using Django just because of django-admin, and you are trying to use CI for the actual site because the server runs PHP, right?
So why don't you use framework that generates something like Django's admin but that you can run on your server?
The Symfony Framework has a really nice admin generator, in the spirit of Django's and you might be able to run it on your server. This would save you from the maintainance nightmare that might come later as #jonwd7 answered