Django API and Front-End Separation - python

I am using Django for REST API. Currently, I am generating primitive web pages from the same Django project. I think it's in my interest to separate API logic and front end.
What is a best way to do this? Should I create another Django app, that just calls this API and generates web pages from template or should I use some other framework for this?
Thanks

You should use front-end framework like Angular, React or Vue for the front end.
So you are going to call the Django-api built from the front end.
You are not supposed to create another Django app.
Here is a link for example of using React front-end with Django-rest api.
https://hackernoon.com/creating-websites-using-react-and-django-rest-framework-b14c066087c7

Related

Is there any way to use a react js frontend to call flask security features?

I have an application which uses flask and flask-security-too in the rest layer. Since flask-security contains some nice, out-of-the-box solutions for user signup, registration etc. including some override-able views. I would really like to use it if possible.
However, I would instead like to plug in a react frontend to get the SPA things going. I read in the documentation for flask-security that the views are indeed possible to override but not sure if/how I would manage to get this working for a built react app?
To summarize my question: Can I use React.js still benefit from flask-security features?
Please read:
https://flask-security-too.readthedocs.io/en/stable/spa.html
basically - you will be using the JSON api rather than forms.

Good Implementation : Isomorphic react app + Python backend

What is the best way to implement isomorphic react app using webpack with python as backend to serve
Following features are required to be implemented
1. React router
2. Redux store
By Isomorphic, I understand you mean both rendered on the server & on the client - so the first access of the site will return static HTML and a javascript bundle which then replaces the static HTML with the react-rendered version.
Usually this would mean running javascript on the server too with Node.js. This means you can render your JSX / React templates / components on the server and send them as static HTML to the user. If you're working with Python on the server, you cannot use your JSX / Javascript templating & logic on the server, so you'll need to duplicate it in Python.
So since you have to do it all twice - you first need to work out which one you want to do first. Either in Python first, or Javascript.
You can either start by building the whole application server-side-rendered, and then have javascript take over, or build a fully javascript application, and render the first views with Python.
Personally, for content-driven sites, I prefer building a server-side rendered application with Django (the most used Python Web Framework), and then adding Javascript on top once it's all fully working as a javascriptless site. This has the advantage of working even when javascript is disabled, and ensures you have good URLs, etc.
Of course, if it's a really complex application in terms of user interaction, you'll need to do it the other way.
I recomend looking into Django first, Here is a great tutorial from django-girls. However, if you want to go the mainly-JS route, here's some ideas:
Javascript First
Probably the best way is to first design your data structure(s), and make a REST api for your data. (e.g. /api/1.0/cars/rustbucket_94 which sends JSON data.)
Next, figure out your user-facing URL schema, and work out which REST endpoints need to be pulled in to get those pages. (How the URLs should look to the end users. e.g. /cars/rustbucket_94.html)
Design your React app as normal, using those REST / JSON endpoints, and the react router to show it correctly.
Once you have your whole react app working, you can now build a server-side rendered version of the whole thing, which you'll need to re-implement from scratch, and can either access the data through the JSON endpoints (slow) or by making the queries itself inside the pages.
The Python side
Which Python Framework to use on the backend?
I'd recommend Django to start with. It's very capable and can do ANYTHING you want it too. You probably need the Django REST framework too. There are other options available, but this will be the quickest way to get something secure and sane running.
Getting Django to work nicely with your webpack / react workflow is not too complex. There are projects like Django-Webpack-Loader and various tutorials about it online showing how to use it.
Good luck.

How to use classic Django app with Django REST framework?

I want to use Django REST framework for my new project but I am not sure if I can do it efficiently. I would like to be able to integrate easily classical Django app in my API. However I don't know how I can proceed to make them respect the REST framework philosophy. Will I have to rewrite all the views or is there a more suitable solution?
"Normal" Django views (usually) return HTML pages.
Django-Rest-Framework views (usually) return JSON.
I am assuming you are looking for something more like a Single page application.
In this case you will have a main view that will be the bulk of the HTML page. This will be served from "standard" Django view returning HTML (which will likely include a fair bit of JavaScript).
Once the page is loaded the JavaScript code will makes requests to the DRF views. So when you interact with the page, JavaScript will request Json, and update (not reload) the page based on the contents of the JSON.
Does that make sense?

Using Django-Wiki with Angular

I'm building a large application with Django and Angular. One portion of the app should be a fully-fledged wiki. Rather than reinvent the wheel, I looked at Django-Wiki as a good wiki implementation.
The problem is I need to incorporate it into my single-page Angular app, which makes data requests to Django's REST Framework. How can I make Django avoid connecting to the Angular single page app for requests to certain URLs, say "/wiki"?
If there is an Angular plugin that creates a wiki interface, that would also be really helpful.

Can Django be used for web services?

My friend said, "Pylons is so much better for web services."
My other friend said, "You can modify Django in a way to do exactly whatever you like."
In Django, what is necessary to be modified (urls.py? models classes? settings?) in order to do "web services" with APIs and REST and versioning, etc etc.?
Yes Django can be used for web services. Check out django-piston:
A mini-framework for Django for
creating RESTful APIs.
Piston is a relatively small Django
application that lets you create
application programming interfaces
(API) for your sites.
Yes, we use it directly. We have view functions that respond to some URI's with serialized JSON or XML objects instead of pretty HTML pages.
Absolutely.
Django is in fact made to be used on server side. But first you need to understand that it is a framework.
A framework is a set of tools which get the common activities done in web development (e.g Login or Sign up module).

Categories