Going crazy with WSGI

45 views
Skip to first unread message

atlastorm

unread,
Feb 27, 2012, 9:40:37 AM2/27/12
to Django users
I am a newbie to Django and web development in general, and I've
completed the django tutorial and am working my way through the
practical django projects book. I have a simple app that I wanted to
try out: basically a form in HTML that takes inputs, reads a database,
calculates and displays an output. I just want to 'get' it, for now.
The goal is to dive in and try to execute a very basic app myself.

I'm running everything on windows: Apache 2.2, Django 1.3, Python 2.7
and Postgres 0.9. The django website recommends I go with mod_wsgi. I
installed it in my Apache folder. My django folder is c:/djangostuff.
All the projects/apps I've gone through so far are from this
directory. I made some changes to my httpd.conf file and created a
file called myapp.wsgi in c:/djangostuff, and also in apache2.2/htdocs

From my browser, localhost/myapp.wsgi returns "Hello World!", from
which I gather it's working, whatever it is. But localhost is pointed
at apache2.2/htdocs in the document root setting of my httpd.conf
file. When I try http://127.0.0.1:8000/myapp.wsgi, I get a Page not
found 404 error.

I have gone through the django project website here:
https://code.djangoproject.com/wiki/django_apache_and_mod_wsgi and the
wsgi instructions for django here: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango.
I've read up on as many articles on wsgi as I can, and the more I
read, the more it confuses me. My questions:

1. I gather WSGI is an interface that is required to pass data between
django/python and Apache, but the hell is it, really?
2. What is a .wsgi file, and in which directory does it go in? Once it
goes in that directory, how does it work?
3. Once I've created a form in Django, how do I use wsgi to pass data
to the application, and back?
4. Why does the instructions at modwsgi mention I store my django.wsgi
under an apache subdirectory? Why is it that my hello world example
works under localhost but not from http://127.0.0.1:8000/myapp.wsgi?

My questions are probably stupid, but I have spent the last three days
reading and I still can't figure out how to proceed. I would
appreciate any explanation that helps me 'get' it. Thanks in advance!

Daniel Roseman

unread,
Feb 27, 2012, 10:39:13 AM2/27/12
to django...@googlegroups.com
You've misunderstood the point of the .wsgi entry. You're not supposed to point your browser at that as a URL. Rather, you use the WSGIScriptAlias directive to send all requests to certain URLs through that script. Typically you'd map the root path, /, to that, although you can mount the script at any point if you want it to only serve certain paths.

As to what it does and how does it work, well all that's covered in the WSGI spec and the mod_wsgi documentation, but from your point of view, your summary is pretty much all you need to know.

Once the WSGIScriptAlias is correct, that's it - you don't need to interact with WSGI any more. You certainly don't need to worry about how to pass data from a Django form: Django itself takes care of that.
--
DR.

atlastorm

unread,
Feb 27, 2012, 11:19:55 PM2/27/12
to Django users
Thanks Daniel, for that explanation.

Let me get this straight - WSGI is a script that connects Django to
Apache. My WSGIScriptAlias is pointed like this: WSGIScriptAlias /
htdocs "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/"
Is this correct? Is this the folder that should house the django.wsgi
script as well?

But what's confusing is all these folders...what happens if I have to
port my application to a linux platform for deployment? I'm sure the
answer is pretty simple, but for the life of me I can't seem to make
the connections. Thanks again for your support.

On Feb 27, 8:39 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> tOn Monday, 27 February 2012 14:40:37 UTC, atlastorm wrote:
>
>
>
>
>
>
>
>
>
>
>
> > I am a newbie to Django and web development in general, and I've
> > completed the django tutorial and am working my way through the
> > practical django projects book. I have a simple app that I wanted to
> > try out: basically a form in HTML that takes inputs, reads a database,
> > calculates and displays an output. I just want to 'get' it, for now.
> > The goal is to dive in and try to execute a very basic app myself.
>
> > I'm running everything on windows: Apache 2.2, Django 1.3, Python 2.7
> > and Postgres 0.9. The django website recommends I go with mod_wsgi. I
> > installed it in my Apache folder. My django folder is c:/djangostuff.
> > All the projects/apps I've gone through so far are from this
> > directory. I made some changes to my httpd.conf file and created a
> > file called myapp.wsgi in c:/djangostuff, and also in apache2.2/htdocs
>
> > From my browser, localhost/myapp.wsgi returns "Hello World!", from
> > which I gather it's working, whatever it is. But localhost is pointed
> > at apache2.2/htdocs in the document root setting of my httpd.conf
> > file. When I tryhttp://127.0.0.1:8000/myapp.wsgi, I get a Page not
> > found 404 error.
>
> > I have gone through the django project website here:
> >https://code.djangoproject.com/wiki/django_apache_and_mod_wsgiand the
> > wsgi instructions for django here:
> >http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango.
> > I've read up on as many articles on wsgi as I can, and the more I
> > read, the more it confuses me. My questions:
>
> > 1. I gather WSGI is an interface that is required to pass data between
> > django/python and Apache, but the hell is it, really?
> > 2. What is a .wsgi file, and in which directory does it go in? Once it
> > goes in that directory, how does it work?
> > 3. Once I've created a form in Django, how do I use wsgi to pass data
> > to the application, and back?
> > 4. Why does the instructions at modwsgi mention I store my django.wsgi
> > under an apache subdirectory? Why is it that my hello world example
> > works under localhost but not fromhttp://127.0.0.1:8000/myapp.wsgi?

Javier Guerra Giraldez

unread,
Feb 28, 2012, 12:02:27 AM2/28/12
to django...@googlegroups.com
On Mon, Feb 27, 2012 at 11:19 PM, atlastorm <ays...@gmail.com> wrote:
> WSGI is a script that connects Django to
> Apache.

not really

WSGI is just a standard, a document that says "the web server will
call the app as a function with such and such parameters, the app will
return such and such values with the response"

armed with that, any Python developer can write a web app just
following the 'app' part of the standard, and any web server that
wants to call those apps do the calls following the other part.

specifically, mod_wsgi is the apache plugin that launches a Python
interpreter and do all the calls according to the standard, and Django
is a framework that follows the WSGI standard.

besides, some WSGI-compliant servers need a little extra information
to specify exactly what web app to call, and in mod_wsgi case, it's
done with 'the wsgi file'. but this is specific to mod_wsgi, other
servers do that differently.

--
Javier

atlastorm

unread,
Feb 28, 2012, 5:58:03 AM2/28/12
to Django users
Thanks Javier for the explanation. I've done some more reading, some
things have become clearer, but:

Right now I'm practicing Django by running the Django server
(manage.py runserver) and everything works. Apache also runs but I
have no clue what its doing. If I close the Django server, how do I
run my application? If I save a django.wsgi file in mysite/apache/
django.wsgi will things happen automatically?

When I practiced CGI with python, I had to import the cgi module and
use that to get the inputs from an html form. Do I have to do
something similar with Django?

Thanks again for the help. Appreciate it.

On Feb 28, 10:02 am, Javier Guerra Giraldez <jav...@guerrag.com>
wrote:

Javier Guerra Giraldez

unread,
Feb 28, 2012, 9:46:19 AM2/28/12
to django...@googlegroups.com
On Tue, Feb 28, 2012 at 5:58 AM, atlastorm <ays...@gmail.com> wrote:
> Right now I'm practicing Django by running the Django server
> (manage.py runserver) and everything works. Apache also runs but I
> have no clue what its doing.

nothing.

the Django development server (the one that runs with the runserver
command) is an intentionally-limited web server. you don't need
Apache for development. but this server will absolutely not be
appropriate for real world serving, no matter how light the load.

> If I close the Django server, how do I
> run my application? If I save a django.wsgi file in mysite/apache/
> django.wsgi will things happen automatically?

you need the mod_wsgi docs for that. the Django page about deployment
in mod_wsgi should be enough to get you running in the simplest case.


> When I practiced CGI with python, I had to import the cgi module and
> use that to get the inputs from an html form. Do I have to do
> something similar with Django?

no. Django manages everything between WSGI and your apps. you
shouldn't need any extra Python code besides what you run under the
development server. in fact, the development server uses WSGI too,
so if your code already runs there, it should also run on
Apache/mod_wsgi once you get that configured.


--
Javier

atlastorm

unread,
Feb 28, 2012, 11:42:27 PM2/28/12
to Django users
Thanks a lot, Javier. Things are a lot clearer now. Appreciate it.

On Feb 28, 7:46 pm, Javier Guerra Giraldez <jav...@guerrag.com> wrote:
> On Tue, Feb 28, 2012 at 5:58 AM, atlastorm <ays...@gmail.com> wrote:
> > Right now I'm practicing Django by running the Django server
> > (manage.py runserver) and everything works. Apache also runs but I
> > have no clue what its doing.
>
> nothing.
>
> the Django development server (the one that runs with the runserver
> command) is an intentionally-limited web server.  you don't need
> Apache for development.  but this server will absolutely not be
> appropriate for real world serving, no matter how light the load.
>
> > If I close the Django server, how do I
> > run my application? If I save a django.wsgifile in mysite/apache/
> > django.wsgiwill things happen automatically?
>
> you need the mod_wsgi docs for that.  the Django page about deployment
> in mod_wsgi should be enough to get you running in the simplest case.
>
> > When I practiced CGI with python, I had to import the cgi module and
> > use that to get the inputs from an html form. Do I have to do
> > something similar with Django?
>
> no.  Django manages everything betweenWSGIand your apps.  you

Sells, Fred

unread,
Mar 1, 2012, 1:34:14 PM3/1/12
to django...@googlegroups.com
I paid my dues, here are a few working files from my system that may
help you.

Note that I use flex for my frontend and thus don't use all the django
features as intended.

Some of these files may be out of date with latest practice and current
docs. So use them as you will.


test1.wsgi
test2
wsgi.conf
Reply all
Reply to author
Forward
0 new messages