Question

I am working on an app that requires a web front-end. All I care about is a HTTP interface for the iOS, Android Apps to talk to. The backend will be using MongoDB.

Do I need to use a Python framework for that? I see that Django wants to generate DB interfaces for me. From a cursory reading of the Django tutorial its not clear why I have to use all those "applications" it comes with like admin, auth etc. I did not see a way to disable the DB interface in Django.

When I wrote php code earlier and all I needed was the php apache module and I got access to the HTTP headers in the php code from where I handled everything myself. Can I not do something like that with python? Why do people use frameworks?

Était-ce utile?

La solution

You can try to use Flask, a lightweight web framework, which can help you to make your simple front-end. An example of simple view can be found on the framework website:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        do_the_login()
    else:
        show_the_login_form()

With Flask, you have no unnecessary module that have Django (admin, auth...) by default, so you can focus on your database management.


If you want an answer that respect your question, yes you can make a website in Python without framework. WSGI might help you for this, but it will be more difficult.

Autres conseils

EDIT:

A good article about Using Django for (mostly) static sites


ORIGINAL:

You mentioned Django in your question, so I'm going to follow that line of interest. Not that there aren't other frameworks out there that can surely do a good job, but as has been commented, without more details about your particular use case, it's hard to be very constructive.

Without any framework, means working directly with WSGI. And that means manually specifying HTTP headers, and all that jazz... I'd take a look at a framework after all. http://wsgi.readthedocs.org/en/latest/frameworks.html


Django is by no means dependent on a database. Sure, it has a lot of built-in goodies that make it easy to work with database(s) but it sure is not necessary.

Just remove any apps from INSTALLED_APPS, and remove url(r'^admin/'... from urls.py. That's it, now you can create your views and (optionaly) templates, unhindered by database :)
Note: not all apps require a database, eg. django.contrib.staticfiles.

A simple view (taken from The Docs):

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

This line in urls.py will link a URL to it:

url(r'^$', 'testapp.views.current_datetime'),  # on domain root
or
url(r'^da-time/?$', 'testapp.views.current_datetime'),  # on domain.com/da-time/

You can use python manage.py runserver to run a test server.

True, you're not using the ORM, what's IMHO the best part of Django, but still, I'd argue that it might be worth it, especially if you are familiar with it, or plan to be. And, you're not writing response_headers = [('Content-type', 'text/plain')] either, which is probably a good thing. Though, of course, manually specifying headers, redirect, and such is just one or two lines of code away.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top