Package and module imports are a lot saner in Python. I'm glad I'm no longer messing with require_once, autoloaders, and all sorts of functions polluting the global namespace.
Python having first class functions and classes makes a whole lot more sense to me. PHP's call_user_func_array and passing around string names of classes and functions for callbacks is just so ugly.
In terms of web framework experience I was using Symfony in PHP and switched to Django for Python. I liked both, but I've found that Django is easier to use. I'm not a big fan of Django's templating language though compared to Symfony (which was just straight PHP).
Django's templates aren't bad though, even if a little awkward at times.
Seems to me like any designer smart enough to get comfortable with Django's templating language probably wouldn't have any trouble getting comfortable with Python itself!
It's not like you'd ever do anything particularly complicated (programming wise) from within your templates anyway.
edit: more digging:
http://docs.djangoproject.com/en/dev/releases/1.2-alpha-1/#s...
Python is a great little language but even after multiple thousands of lines it still looks like the guardrails on the highway are missing, I never knew I was so addicted to curly braces.
I also find it very hard to switch from the one to the other and back again after sticking to one of the two for a while.
My 'preferred' language/framework for more complicated stuff actually does not seem to exist. I can list a whole series of problems with just about any development stack out there at the moment, at least the ones that I've used.
For quick and dirty stuff (throwaway sites or quick tests or prototypes) I still prefer PHP, but mostly because I can rely on a framework that I wrote years ago that makes it very easy to throw something together. It's far from perfect but it's the devil that I know best.
I've made three sites in python/django, it works but the ORM is very limiting and I keep hearing that forms are hell (but I rolled my own forms, so that wasn't a big deal).
I wished there was a clear winner amongst all these technologies, everybody seems to have built just enough to satisfy their particular itch.
Next on the list of stuff to try is werkzeug, another python framework.
Huh. That's not been my experience at all. I guess if you're trying to build full-strength webapps that might be the case, but we've built a decent number of fairly complex user-content-managed type sites, and Django has not caused any trouble at all. Very occasionally we step out of the ORM (usually in places we should be stepping out of RDMS in general, anyway, like tracking ad-clicks). (The ORM does now support aggregation, too - SUM(), AVG(), ad-hoc calculations etc.)
And the Forms lib is great if you're happy with default html-generation (it can do paras or tables); things get kinda fiddly when you're trying to customise layout and appearance, but it's still nice having validation, saving to the db etc. all in one place.
You can simply forget that using django, you'll have to export all the stuff you want to query in a format that exactly matches Djangos field name magic.
And better not get any of that wrong or you'll be tearing your hair out, an underscore in the wrong spot and it will look just fine but it will never work and the error messages you'll get are much less than helpful.
Here is a comment from the models.py file in one of my little django applications:
# caveat, the related_name parameters of parent and thread
# should not be parent_id or thread_id, this leads to
# an 'add() argument after * must be a sequence' error
# when transforming the template. Apparently the names
# clash with the underlying field names that django
# uses for these fields.
So, effectively, what this is all about is that django names a bunch of fields in the queries as aliases, but does not check if there are real fields with those names.And sure, cross-database and cross-server are weak points for Django. I guess it depends on the scale and complexity of what you're building… I build reasonably conventional websites for a living, and haven't hit too much pain with Django.
The idea was that both could talk to the same database to check performance, and to get an idea about how much code was required in each to accomplish this.
This would have worked if I had started from python/django, but writing the PHP part first and then the python part got me caught up a couple of times.
Now for me it was no big deal to change the underlying field names, but if this would have been embedded in to something larger a restriction like that could even turn in to a dealbreaker.
What's a minor nuisance to a lone programmer can be a very big issue in a corporate setting, where there might be lots of other software talking to that same db.
In general I think it is a bad idea to resort to tricks like these.
Indeed, the __ is another one, and the fact that you can't prefix field names with database names is another.
you might be interested in the 'db_column' option to Django fields, e.g.
content = models.TextField(db_column='a_different_column_name')
That will allow you to have 'content' on the model, but 'a_different_column_name' in the database. It can be quite helpful for talking to legacy systems.
You should try it for yourself and form your own opinion. I've found the declarative nature of the forms in Django to be extremely pleasant. My biggest complaint is the need for writing forms at all!
So I rolled my own forms, thinking at the time that it must be possible but not knowing how.
Now I hear - from someone in a HN comment - that AJAX and django don't play particularly nice.
Here is the thread, I dug it up:
A form is,
0. a representation of a HTML form. 1. Knows how to validate and sanitize that data. 2. Know what to do with the data. In my apps, a lot of logic is in forms, (which people generally put in views.)
Its written by me, but I think is explains some interesting ways to use forms which people miss and do the hard way.
http://uswaretech.com/blog/2010/01/doing-things-with-django-...
I probably should.
Do anything even slightly complicated (say validations dependent on other field values) and you end up having to extend the framework in truely monstrous ways just to get it to happen. (On a side note, using the validate decorator in Pylons is NEVER the great idea it initially seems to be).
In the end I replaced the form system with my own that let me work procedurally instead of declaratively (without the need to memorise the multi-tiered internal form handling API) but I could still plug into the existing declarative system (no need to re-invent the wheel).
In that regard Rails form handling is a pleasure to work with. Validation is just as easy for simple and complex scenarios. Integration with the ORM is fantastic removing a tonne of manual pain when constructing forms.
5.
Prob. You want to create a field which has cross field validations.
Ans. Create a field with a .clean
class UserForm(forms.Form):
username = forms.CharField()
password1 = forms.PasswordField()
password2 = forms.PasswordField()
def clean(self):
data = self.cleaned_data
if "password1" in data and "password2" in data and data["password1"] != data["password2"]:
raise forms.ValudationError("Passwords must be same")After moving to Python and Django, everything changed. Now there is a clear, simple structure for every application, defined by the framework itself. There is a good balance between models, views and templates, because Django enforces templates to be simple. There is no weird magic like ZF's autoloading or CodeIgniter's library loading, just basic Python imports.
It's really cool that whether you download a third-party Django app or use your own, you always have the familiar app structure of xxx.models, xxx.views, xxx.forms, xxx.admin and so on. This makes it really easy to understand how any code works.
I did go through a learning curve. First I tried to tweak Django to my own ideas of a good architecture. But slowly I realized that it actually makes sense to follow the conventions to the detail. Especially when you have a team of people that needs to follow the same principles, in order to understand and reuse each other's code.
I also used Python for several years as a shell scripting language before building real websites with it. Looking back, some of the code is pretty horrible and un-pythonic, more resembling Java or PHP, and not using Python's features properly.
So anybody new to Python should expect to go through a period of unlearning their old programming habits. Your code will shrink from the old 10-line-for-loops into 1-liners, you won't write much of your own classes any more, and eventually you'll start metaprogramming with decorators and metaclasses, to get rid of 90% of the remaining (boilerplate) code you thought you needed. :-)
When you reach Zen, you will no longer have to code. Instead you will declare your Django applications as data models, admin definitions, URL mappings, view decorators, HTML templates, and just a few lines of logic in the view functions.
I believe the rule is, if you don't know when to not use metaclasses, you shouldn't be using them at all.
I have noticed that the same sometimes applies to __getattr__ and similar Python features. You easily shoot yourself in the foot unless you are very careful. So all that stuff is best left to carefully debugged and maintained libraries, and out of everyday quickly-written application code.
Web dev wise, it helps if you had some experience with PHP frameworks (though most of them are modeled more on Rails rather than Django or other Python frameworks). Also, the libraries aren't built in as in PHP and documentation isn't as good as PHP's (I'm not aware of any language that has online docs as good as PHP's. Django has great docs, though).
I recommend starting with Google App Engine. It basically takes care of the deployment for you, which is often a sore point in many environments. It's free and includes a basic framework that resembles web.py with bits of Django. In fact it's worth picking up Python even just for the sake of making use of GAE.
I hate to say it, but that was what I encountered. The paucity of the documentation on Django is one of my primary irritations with it.
I still strongly recommend checking out Python and Django. Excellent and productive environments.
MSDN and MSDN Social has some decent doc and comments.
In general, the Python language and tools tend to steer you in the direction of writing cleaner, better-structured code. Working with Django also gives you access to a lot of decent 3rd-party applications that you can integrate into your project, and the Python stdlib absolutely dominates PHP in terms of scope, quality, and consistency. Having the bytecode compiler and libraries that provide better data structure and algorithm in app servers also opens up the door to a whole bunch of performance optimizations that are difficult to accomplish with PHP.
That's not to say that the Pythonic way is always better, of course. PHP encourages a very iterative approach to development -- at a basic level, you're creating a static page, then incrementally adding dynamic behavior until it does what you need. Python apps tend to put the code front-and-center, and output HTML (or JSON, or XML, or whatever) as a last step in the process. In the long term, that's usually a Good Thing, but it can make the prototyping and learning processes longer for some people.
I went from PHP-Nuke, to pure PHP, to Python, to handwritten Python site engines, to Google App Engine, and now finally hunkering down and getting Django under my belt, for real this time.
And that's my first tip: GAE makes a great stepping stone to Django (and even includes a sizable chunk of Django).
I have a very bad taste in my mouth from prior experience with Joomla, but I found Drupal to be very easy to use. But once I made the decision to finally learn a real framework, Python and Django made the work... almost trivial. Looking at the code for Drupal modules now makes me wonder why I let myself do all of that extra work that Django doesn't require.
I'm with jacquesm, I still use PHP from time to time for minor things, simply because there are a good amount of hosting providers that don't offer Django. But I think my experience with Python has made me a better PHP programmer. However that works. However I've never really had issues with forms, at least not with GAE, but it IS a little wonky.
self.request.get('form_field_name')Here are a few things that I got tripped up by:
- I was searching for a substr? function. there is none; instead you do things like str[0:1]
- Dont Count on autocompletion (at least not in eclipse). I was hoping I could duck type things like i had done in php Eclipse and get object info autocompleted. This is not possible, so you have to keep a lot more info about objects in your head. Perhaps one way around this is to try keep all your classes in one module.
- Test with IDLE. I was used to testing php code by whipping up a single page and browsing to localhost. With appengine, and possibly your web server, this isn't justifiable when Python ships with a pretty cool cmd IDE.
hth!
Tbh, PyDev for Eclipse is pretty darn good. Doesn't fail me on autocompletion, function arguments etc, except for places where objects are created dynamically. Also, I find it very handy being able to navigate to any one places in my $PYTHONPATH with one keystroke on F3 and get the documentation + the implementation to read through. Keeps me away from most documentation sites and inside the IDE.
I can honestly say I'd be surprised if you didn't thank yourself for jumping over to Python. Just make sure to give yourself the time and resources required for the shift in process and thinking.
- JavaScript, JQuery and Google's Closure for the client.
- NGINX for the infrastructure if needed, for media hosting for example.
(That said, I'm using more and more Clojure for my personal stuff.)
Once you do this, you'll be very annoyed every time you need to go back to using php. This change will be practical and eye opening for you. Enjoy.
Dirty command line scripting, and embedding in templates. You can create custom templating languages but they just end up growing in complexity as you decide you need more features. (Kind of analoguous Greenspun's 10th rule of programming: Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of CommonLisp. :P)
Aside from that it really does come down to personal preference. Some people like it one way, others just don't.