Writing webapps in python without Django or any framework [closed]
Asked Answered
M

2

8

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?

Maximomaximum answered 15/2, 2014 at 20:47 Comment(5)
There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.Girth
Check out WSGI, also search for HTTP in the Python docs. How do you think frameworks are written?Gabfest
Frameworks are just there to make it easier to use the otherwise unwieldy WSGI modules. Try a lightweight framework like Flask.Macronucleus
A lightweight framework (Flask, Pyramid without the ORM, etc) is typical to use because they're less work than not using one -- but if you don't want to, nothing stops you (besides, hopefully, a desire to not do unnecessary work). Having to do session management, cookie management, and all the other minutia of a web application yourself is error-prone, and means you don't get the benefit of other people implementing best-practice approaches.Echinus
btw, see docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/… for an example of a single-file application using Pyramid.Echinus
S
7

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.
Slowmoving answered 15/2, 2014 at 23:26 Comment(1)
This is exactly what I was looking for and my searches led to Flask as well. I quickly realized that WSGI apache module does not give me the goodies like url management, cookies, templates etc and Flask is exactly what I need. Writing some code in that now and it seems straight forward and simple.Maximomaximum
S
3

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.

Spiffy answered 15/2, 2014 at 22:49 Comment(1)
@fmhr - Thanks for your detailed response. Django appeared too complicated to me. All those commands and the default modules completely threw me off. After reading that tutorial I did not understand how the Django works, I mean, the tutorial instead of explaining the guts/philosophy of Django just is a dummy how to. Also I realized I will be spending a lot of time removing stuff I don't need. But I want to learn it and hence the attempt. Its just not the right one for this project.Maximomaximum

© 2022 - 2024 — McMap. All rights reserved.