Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django along with Flask


Situation:

I am tentatively considering a situation to use Django to

  1. serve HTML (by Django's template)
  2. serve all the static files like CSS, JS from the Django project

and my intent to use Django stops here. After the javascript files are downloaded to client side, they communicates with a Flask backend with RESTful API (the Ajax way).


Why two frameworks? And why in this way?

The frontend guy of this project I am working with knows Django very well, and I think I mainly want to use his CSS / HTML template / jquery skill.

I want to have an independent API server and I feel Flask is an ideal option for my need (from building API service perspective).

I guess people would suggest "why not ask the Django guy use Jinga2 for templating?" (in that way, we can do away with Django) I guess my current answer would be: I don't want him to invest too much time (to learn)

I guess people would suggest "why not use Django to serve the Restful API call?" (in that way, we can do away with Flask) I guess my current answer would be: I (as the person implementing API logic) like Flask.


My question

Short one: is this doable? or does this sound a crazy idea?

Long one: Can you kindly give some guidance?

Thanks,

like image 885
chen Avatar asked Jun 22 '13 01:06

chen


People also ask

Should I use Django or Flask?

Django is considered to be more popular because it provides many out of box features and reduces time to build complex applications. Flask is a good start if you are getting into web development. There are many websites built on the flask and gain heavy traffic, but not as much compared to the ones in Django.

Is Django harder than Flask?

In sum, usually, Flask is easier to learn than Django. Ideally, in the long run, it might be more beneficial to learn both frameworks to make the most out of their advantages and easily overcome their gaps.

Is Django still relevant 2022?

Django and Angular have their fanbase and for the fulfillment of web development trends of 2022, both frameworks are going to be very popular, diverse, and useful.

Why is Django preferred over Flask?

Django is suitable for multiple page applications. Flask is suitable for only single-page applications. -Django-admin is the in-built bootstrapping tool of Django that allows the creation of web applications without any external input. Flask does not come with an in-built bootstrapping tool.


2 Answers

I am a little late to the party but Application Dispatching should help with this. According to the intro in the documentation this is how it goes:

Application dispatching is the process of combining multiple Flask applications on the WSGI level. You can not only combine Flask applications into something larger but any WSGI application. This would even allow you to run a Django and a Flask application in the same interpreter side by side if you want.

like image 150
DrSAR Avatar answered Oct 06 '22 03:10

DrSAR


If I were you I would take the Django templates from the designer and convert them to Jinja2, then build the application 100% in Flask. But since you asked...

is this doable? or does this sound a crazy idea?

Yes to both :)

Can you kindly give some guidance?

Here is a simple way:

You write the two applications, one in Flask and one in Django. Let's assume you solve all the problems you will have when trying to share database or other resources and now you have the two applications running, each with its own web server and each listening for requests on a different port.

You now put a proxy web server as your front web server to the outside world and proxy the requests that come from clients to one or the other application depending on the URL. You can make all the URLs for the Flask application have the format http://hostname/api/..., and then key off of the api in the URL to separate the requests in the proxy server and give them to the proper application.

Because from the outside all requests go to the same hostname and port (that of the proxy server) you will not have any trouble with cross-site scripting.

like image 29
Miguel Avatar answered Oct 06 '22 04:10

Miguel