Discussion:
Understanding Django for PHP developer
(too old to reply)
mikegolf
2012-06-03 10:18:07 UTC
Permalink
Hi,
I've started to learn Django recently, however for last 5+ years I've
been developing using PHP (especially Yii) and thus asking if there's
any tutorial / documentation on significant differences in
*thinking*.
What I mean is for example objects' lifecycle - for PHP the life cycle
of object is strictly related to the single request..
I know that for Python / Django developers these are obvious things,
but not for me. Thus I'd love to see an article / document which
points these base and significant differences.
Any recommendations?
Thanks in advance,
mg
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Derek
2012-06-03 16:07:45 UTC
Permalink
Its more of an overview, but picks up on some key points:
http://j4mie.github.com/python-and-django-for-php-refugees/#slide1
Post by mikegolf
Hi,
I've started to learn Django recently, however for last 5+ years I've
been developing using PHP (especially Yii) and thus asking if there's
any tutorial / documentation on significant differences in
*thinking*.
What I mean is for example objects' lifecycle - for PHP the life cycle
of object is strictly related to the single request..
I know that for Python / Django developers these are obvious things,
but not for me. Thus I'd love to see an article / document which
points these base and significant differences.
Any recommendations?
Thanks in advance,
mg
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
mikegolf
2012-06-03 22:53:48 UTC
Permalink
Derek,
I've already googled that one. Unfortunately it is not much helpful as
it compares pure PHP coding to using Django - which is Python web
framework (also, it seems to assume Django is the only Python web
framework around ;) ). For anyone who has been using some serious PHP
frameworks, some of the arguments are no longer true ;)
I'd love to see fair comparison - it means, using PHP MVC framework vs
Django (more "general" points, like "naspeacing", "declarative model
api" etc)
Its more of an overview, but picks up on some key points:http://j4mie.github.com/python-and-django-for-php-refugees/#slide1
Post by mikegolf
Hi,
I've started to learn Django recently, however for last 5+ years I've
been developing using PHP (especially Yii) and thus asking if there's
any tutorial / documentation on significant differences in
*thinking*.
What I mean is for example objects' lifecycle - for PHP the life cycle
of object is strictly related to the single request..
I know that for Python / Django developers these are obvious things,
but not for me. Thus I'd love to see an article / document which
points these base and significant differences.
Any recommendations?
Thanks in advance,
mg
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Doug Ballance
2012-06-03 21:50:31 UTC
Permalink
http://www.twistedcommons.com/2008/12/how-django-works.html

There was a blog post several years ago that followed, in detail, the
life of a response with all the gory details. Unfortunately I can't
locate the link. The above page is brief, but has a nice picture that
is easy to follow.

A huge difference is that php code has access to just about every
thing in the global space of the script. It has access to headers,
cookies, path information, querystrings, and pretty much all the
information it might need. Django bundles that into a request object
and requires that you explicitly pass data about the request to your
code, In php you could access $_GET in an arbitrary function and it
would just work. The request.GET equivalent in Django is only
available if you pass it into the function.

Since code may be reused for the next request, it's possible for
changes to have polluted values from the first request if you
accidentally do something your shouldn't. This requires a little more
caution when writing your code, but is not difficult.

For example consider a function like:

def myfunc(listofstuff=[]):
listofstuff.append('I was called')
return listofstuff

If you call the function in the first request without a parameter
"myfunc()" it will default to an empty list, append "I was called" and
return a list with a single item ["I was called"], and everything will
work fine. The gotcha is that the empty list was defined when the
function was created, so the second request will get the -same- list,
which this time will not be empty, and the second call will return ["I
was called","I was called"].
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Doug Ballance
2012-06-03 22:10:47 UTC
Permalink
I should have looked a little harder, I found the blog post. It is
about a much older version of Django. Some of the references to django
internals are surely a bit different now, but the fundamentals are the
same. It really helped me when I first started. I'd love to see it
updated for the current versions and part of the official docs.

http://www.b-list.org/weblog/2006/jun/13/how-django-processes-request/
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
mikegolf
2012-06-04 08:00:43 UTC
Permalink
Doug, thanks a lot! I've read the article and it really explains a
lot, but what I'm actually looking for is a list of differences like
the one you've pointed out (listofstuff lifetime)... anything? ;)
Post by Doug Ballance
I should have looked a little harder, I found the blog post. It is
about a much older version of Django. Some of the references to django
internals are surely a bit different now, but the fundamentals are the
same.  It really helped me when I first started.  I'd love to see it
updated for the current versions and part of the official docs.
http://www.b-list.org/weblog/2006/jun/13/how-django-processes-request/
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
bruno desthuilliers
2012-06-04 09:28:44 UTC
Permalink
This post might be inappropriate. Click to display it.
mikegolf
2012-06-07 10:13:20 UTC
Permalink
Bruno,
exactly what I've been looking for! Thanks!


On Jun 4, 11:28 am, bruno desthuilliers
Post by bruno desthuilliers
Post by mikegolf
Hi,
I've started to learn Django recently, however for last 5+ years I've
been developing using PHP (especially Yii) and thus asking if there's
any tutorial / documentation on significant differences in
*thinking*.
What I mean is for example objects' lifecycle - for PHP the life cycle
of object is strictly related to the single request..
I know that for Python / Django developers these are obvious things,
but not for me. Thus I'd love to see an article / document which
points these base and significant differences.
Any recommendations?
Well, the answer may not be as simple as it seems, as it first
requires a correct understanding of Python's execution model,
namespaces, scopes and bindings (aka "variables") - and this would
make for a rather long and technical document. Then you have to know
how your django application is deployed.
I think the most important points wrt/ "objects lifcycle" are (overly
* a Python module's top-level code is executed once the first time the
module is imported
* "import", "class" and "function" statements are executable
statements
* the code at the toplevel of a "class" statement is executed once
before the metaclass is called and the class object created
* all this will occur for each of your django server processes
* you can have multiple processes serving the same django application,
and ay request can be mapped to any process (this depends on the front
server and gateway so you have no control over this)
To make a long story short: remember you are in a long running
process, so never modify (mutate or rebind) any module or class
attribute when serving a request.
As an exemple, I once spent quite a few hours debugging a seemingly
intermittent and very very strang problem on a form. The root problem
was a younger co-worker wrote code that was mutating some of the
form's *class* attributes in the class initializer, and depending on
which process would process the form's submission, things would - or
not - get totally mixed up.
A more common mistake is to initialize a class or module date
attribute with the result of a call to datetime.datetime.now() and
wonder why it's not updated on each and every request.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Continue reading on narkive:
Loading...