Why does Express serve static files using middleware instead of other ways? - django

I'm a Django developer. In Django we use an application to serve staticfiles. Then I found that in Express we use a middleware. Could anyone explain what are the pros and cons of both methods?

Related

How to use django as backend for cordova?

Not a web developer, but currently playing with cordova and would like to use django to use python to implement backend functionality. As I (vaguely) understand it, cordova manages frontend stuff and django is mostly for backend stuff. So is it possible to use django as a backend for a cordova project (eg. directly use preexisting django templates in a cordova app)? If so, how? Is there some kind of special communicationn that I'd need to code myself?
(My uneducated guess would be to initialize the django project inside the cordova www folder, but this seems wrong). And if this is a totally wrong way to think about this problem, let me know.
You could use Django as your backend and implement a REST like API (urls that accept and return JSON data) in it. There are useful tools/libraries for that, for example django-rest-framework.
Then you would call those endpoints (URLs) from your frontend, which can be written in cordova or any other JS frontend frameworks.
As you already pointed out, I suggest keeping frontend and backend code in separate folders.

Example of configuring Flask + AngularJS + PyJade (or other alternative to Jinja)

I am new to Flask and building a web app that uses Flask and AngularJS. My understanding is that the static directory is the place to store AngularJS files like javascript and templates.
I wonder if there is a stable alternative to Html/Jinja that I can use for templates. And if there is, how can I enable it to work with template files inside the static directory?
I was looking to PyJade but didn't get how to get it working with the web assets pipeline.
Would appreciate any example or recommendation.
You could use Mako. See here how to install and use it. Also you could use Genshi.
Hi #user3745936 I'm Syrus, creator of pyjade. What is the code you are trying to use?

Django app as REST-based service

According to the documentation, an app is a module which deals a well defined operation.
Is it correct to think about an app as a REST-based service? or is it mandatory to use some framework like piston or tastypie to create a RESTful web service around an app?
Generally, no. Django app is really just a python module, with some interfaces to django internals like models, urls, admin discovery etc.
To implement REST, you still have to manage network communications via views, and this is where you either write your own code or use the help of tastypie/piston/etc.
Please do have a look at django-rest-framework, I just stepped over from tastypie to this new framework, works great!
http://django-rest-framework.org/
Especially the class based views and the browsable api! and may other advantages (e..g. to upload images)
And to answer your question:
The rest-base service is an extra entry to your webapp.
I made some api's for some projects using the django-rest-framework, most project members were surprised they got a webapp as an extra, while it was actually the other way around. You make a django app (with views models and urls) and on top off that you make the api.

Why Django static and media folders aren't pre-configured?

I was presenting a Django demonstration to my brother and he asked me the following question(s): "Why Django static and media folders aren't pre-configured? It's purpose aren't to be a convention over configuration framework? Why I am supposed to configure these things every time I start a Django project?"
I couldn't answer to him. Does anyone can?
P.S: I don't mean to compare Django with other frameworks. I'm just trying to understand why these design decisions were made;
Django is very definitely not a convention over configuration framework. Your brother is perhaps thinking of Rails, which does follow that principle - but Django follows the Python principle of "explicit is better than implicit".
Because django shouldn't distribute media files anyway. When you read the documentation you can see that the static files should (and must) be distributed by your server engine and/or by some kind of CDN when you are in production. Django, is just here to process your pages but not your media. And when you are in development, indeed, you have to use django to distribute static files. But you can use a python script to generate it, and manage it. For me, one of the best is: Django mediagenerator. This will let your files like they are in dev mode, but this will optimize it in production.

django handling images and other media content

I am learning Django, using the django book, which is great. However, it is a little down on using the django test server to serve up image files and other media. Obviously, any page has some amount of image content beyond the straight up HTML that you put in templates. And there are various other files that need to be served up such as CSS files and possibly non native files (such as PDF, spreadsheets, RDF, xml and the like.)
Of course I can set all that up in Apache, however, I need to be able to serve most of these (especially gifs, pngs etc.) during the development process. There doesn't seem to me to be an obvious way to configure that.
Can someone tell me how django developers do this? What is the standard practice? Do I have to use Apache as the development server?
Thx.
The documentation is very helpful on this.
There is absolutely nothing wrong with using the development server for development. It's extremely handy, and meant specifically for that purpose.
What the Django developers and Django Book are trying very hard to steer people away from is using the development server for production. Doing that is a VERY BAD IDEA.
You can ask django to serve the media files when used on the development server.
The standard way to do so is to add this in the urls.py
if DEBUG:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/path/to/media'}),
PS: I personally still prefer to serve media from nginx even locally.