So many displays available these days are short and wide. yet pep 8 instructs us to write code that is tall and thin (<80's chars wide)
Next monitor I get will be one that pivots or just be more squarish. This wide format is a mismatch for development. I have to scroll too much, and half of my screen is displaying nothing.
I know what you'll say, just resize your window to show 80 chars wide, and then stick something else like documentation or log windows in the extra space. My answer to that is, yes, I do that now. But, sometimes I just want to see as much code as I can. And I put the docs and logs in a second display.
-
I tried pivoting monitors for a while. One problem is that this changes subpixel ordering from horizontal to vertical, and even if you change the font rendering settings to consider that, anti-aliased fonts look subtly different as a result. My mind interprets that as "ugly fonts ugh".
Put me in the vertical splits with multiple 80-column files side-by-side camp, please.
- Marius Gedminas -
I used to think so too. Later, I've found how easy it is to have two files opened side-by-side (vertical split) - e.g., code on one side, its test code on the other.
- Oren
I'd vote for 110 chars. -
Wide monitors are pretty handy while looking at two files side-by-side (i.e. during code review, comparing changes in specific revision and so on). In fact, ignoring 80 characters limit can be really annoying during that activities.
BTW, don't you think, that in general, text readability is much better when text has 60-70 letters per line? For me it works. For TeX people also :)
- gryf -
PEP-8 is great for readability especially if I wan't to print the code. As a teacher it's easier to discuss and comment someones code on paper and the character limit helps to keep the code inside the margins without reflowing -- not that everyone follows the recommendation...
- Carl Holmberg -
The displays aren't actually shorter than those from the past. However, they are much wider. Personally, I like to be able to look at two files side-by-side, which the wide displays accommodate nicely. Even so, I just admit relaxing the PEP-8 limit to 100 characters for my own purposes.
- David -
80 characters seriously isn't that bad. I guess if your code has stuff in it like getMagicConfoxulationSperzoidDoxolator then yeah, OK...
Also I don't know if the stuff about tall monitors is joking or what, but I've seen setups just like this - dual vertical monitors in fact; it's pretty amazing.
- yoyoyo -
Get an external monitor that can rotate 90 degrees. Problem solved!
Now, seriously, do you think we should increase the char limit?
- Name -
The 80 character line limit is the only part of PEP 8 that really gets under my skin. It's as if we're making sure that we're ready if punch cards or VT-52s come back into vogue.
- Lars -
I use my laptop display and an "extra wide" Dell monitor at work, and it works out great. The only problems I have with it are that Fedora doesn't remember that I have that display tilted when I disconnect and reconnect, quick means a couple extra clicks, and this particular monitor has poor contrast/saturation when tilted...in other words, when horizontal, the contrast appears consistent top to bottom and left to right, but when tilted, it appears darker on the right side. A minor annoyance.
I call it the code-o-tron.
- Robin N.
Wing4.0 is a Django release. Here are the details. You should be able to find out what you need there. So I think I'll just put up some pictures.
Normal view method with a quick test class T to put in the context
Awesome!!! Breakpoints in a template.
While parked on that breakpoint, use the Debug Probe (pictured) and Call Stack (not pictured)
Note the test_func actually was called
-
With respect: you need THOUSANDS of USD to have breakpoints, simple 100USD is not enough. For THAT money I would prefer to pay Windows Server license for life and have Visual Studio with breakpoints + IronPython + MVC2. That's a joke, I just spent 30 minutes of my life checking this C R A P out and this is basically "disappointing of the decade"
- Anna -
Damn it, i was not aware of Wing4 ! This is what i've been looking for.
- YngwieMalmsteen -
very awesome
- Daz
Overview
The graph_models command of Django-Extensions will draw you a nice DB schema for your defined models.py. It's simple to use this to rapidly model your DB.
Steps
- In a Django application, make some changes to models.py
- Run this in an application directory (change 'yourapp') in your Django project to generate and display a graph
python manage.py graph_models -o yourapp_models.png yourapp
- Now you can view the graph with a png viewer - firefox will work
firefox myapp_models.png
- make changes to models.py, and repeat
Error: One or more models did not validate:
myapp.m1_to_m2: "type": CharFields require a "max_length" attribute that is a positive integer.
-
Great app !
- YngwieMalmsteen
Overview
When customizing the Django Admin, for example a change_list.html template, you might want to add some JavaScript of your own. If you just chuck in your jQuery code, using the $ alias, to the {% block extrahead %} you'll probably see $ is not a function in your browser's Error Console.
If you just intend to use the jQuery that the admin is loading, you'll find it is loaded in the {{block.super}} line below. But, Django also clears the $ alias to jQuery when loading and sets up the only jQuery alias to django.jQuery.
Reclaiming the $ alias
Consider this block of code
{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
function setClickEvents() {
$('a').click(function(event) {
alert('Clicked');
});
$(function() {
setClickEvents();
});
{% endblock %}
This will result in $ is not a function on a page reload. Add the line
var $ = django.jQuery to bring it back.
{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
function setClickEvents() {
$('a').click(function(event) {
alert('Clicked');
});
var $ = django.jQuery;
$(function() {
setClickEvents();
});
{% endblock %}
Final thoughts
This works for me, but I can imagine there might be a case when you don't want to redfine $ like this. Afterall, Django is clearing it for a reason. This is alternative solution.
$(document).ready(function($) {
setClickEvents();
});
})(django.jQuery);
-
I'd like to add that some external plugins are already wrapping their code into an anonymous function, which, rather than "$", assumes "jQuery" to be available.
In that case you have to define "var jQuery = django.jQuery;" in a script block that precedes the external reference to that file.
Just found that out a minute ago :-)
- Danny Adair
http://stackoverflow.com/questions/5484547/how-to-provide-to-third-party-external-jquery-plugins-in-django-admin -
What I've seen around about Django redefining $ is that it does to prevent conflicts with prototype. Cause prototype also uses $(). ;)
- Jayme -
Thank You!!
It helped so much!
I was stock for hours trying to figure out why $ can't work!!Many Thanks Again.
- R
Description
When you are coding these days, you might be using Mercurial or git for source control. Since these are distributed systems, there is no central repo. So you can't cheat and use it as your release procedure, like you could with cvs, and svn. I prefer to use Mercurial. When I'm ready to push code to a server, I put something like this my .bashrc file.Brief Explanation
- This sends the Django project test (and all files subdirectories) in the virtualenv directory 'test' to the server as an update (only altered files)
- Any file that has been deleted locally, is deleted on the server.
- Exclude the setting.py, so you don't stomp on the production one which is probably different.
- Exclude .pyc files and anything that starts with .
Notes
- Fabric is a cool product, and I might start using that too to restart servers.
- Of course, if you are collaborating with a team, you probably have a git or hg hub. If so, you can still use this copy files from a testing area to a release area. So, you don't need a working directory on production.
- Of you have more than one server, you could certainly add more lines to that function.
If you want to use the python method locale.currency() to format numbers, you need to make sure you aren't set to the 'C' locale.
You can use the following and serve the code up with Django runserver with no problem. The Python documentation says that this call '...sets the locale...user’s default setting (typically specified in the LANG environment variable).'
locale.setlocale(locale.LC_ALL, '')
locale.currency(10.50)
If you use Apache, and WSGI to serve up your Django App, you need to include the locale value explicitly. Otherwise you may find the error: Currency formatting is not possible using the 'C' locale.
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
locale.currency(10.50) # $10.50 USD
Incidentally, for my code to be internationalized, I will need to set the locale based on a User preference anyway, so relying on the server environment's setting would not be correct even if it worked.
-
I've found a more complete locale example that might be worth looking at: http://djangosnippets.org/snippets/1825/
- EyePulp -
Just what I needed. Thank!
- Vasili
Overview
A programmer using South is able to make rapid changes to the DB schema of a project over a short period of time. When it's time to check-in work, as a programmer, you may want to collapse the migrations you have created into one
Checking in all your migrations isn't a problem, but if you find you prefer to have a clean migration history you can have your programmers follow this procedure.
The steps
- Reset your migration history for the app up to the last checked in migration.
./manage.py migrate appname --fake MIGRATION_NUMBER--fake means don't touch the application's DB. Just remove all migration rows from south_migrationhistory up to that migration number.
- Remove all migration files up to that number
rm appname/migrations/ALL_MIGRATIONS_OLDER_THAN MIGRATION_NUMBER
- Recreate the "next" migration to match your current DB state.
./manage.py schemamigration appname --auto
- Apply the migration to create the DB objects
./manage.py migrate appname
Alternatively, you might want to completely rebuild your DB. In this case, do the following
- In Step 1. Make MIGRATION_NUMBER the string 'zero'
./manage.py migrate appname zeroThis removes all migration history, and the DB. If this fails. You can use --fake, and then remove the DB manually.
- In Step 2, remove all the migrations
rm appname/migrations/*
- In Step 3, do the following instead
./manage.py schemamigration appname --initialThis creates the 0001 migration based on your models.
- Apply the migration to create the initial DB objects
./manage.py migrate appname
-
@Carl There is one very important reason: testing speed. Right now, I'm having about 60 migrations that need to run before the actual tests can start and this takes a lot of time.
- Passy -
I changed this post to a form that allows you to collapse the migration history to any point. However, it does require you do it before you have distributed your migrations.
- Gene (response) -
Hi Carl, It's a bit cleaner to have one migration for a group of migrations when only 1 is necessary. The ease of schema updates going forward during development, can cause quite a lot of migrations to be created in a short period of time. It's my preference to collapse the migrations down at release points.
- Gene -
What's the actual reason for doing this? None is given. Having those earlier migrations around doesn't hurt anything; you run them once on the newly-deployed production system, and you're done. Why not just keep them and avoid extra work? Why the rush to erase history?
If you do what's recommended above, you have to make sure every developer with a development copy of the system also does step 2 and runs the new initial migration with --fake.
- Carl Meyer -
I'm not aware of any way to collapse a subset of migrations in to one.
I think you could release version 1 with migration 001, and then keep that revision file somewhere other than your migrations/ directory.
Then, if you have to go back to version 1 source, to fix a bug in a live system that is on version 1, for example, you could check out the source and copy the migration for the version 1 release into migrations/ and then migrate 001.
I'm assuming since there's a production system in this example, you could get the data to populate the DB from that system.
I'm sure there other other options here. Anyone care to chime in?
- Gene (response) -
What about subsequent releases?
The way I understand the instructions - thanks btw! - the reset basically contracts all the development migrations into one, which is used to set up the initial database on the production system.
So as an end result, the production system will have its database set up, and a 0001_initial in its "migrations" directory. So far so good. A clean slate.
Now the developers go back and for the next release, there are another 40 migrations which have been made in the course of the development. It would be nice if they too could be contracted into one (technically otherwise identical) migration.
How could that be achieved?
- Danny Adair
Delete all migrations except the first, clear them also from the table south_migrationhistory, and then run schemamigration to create that one big step?
I just returned from a weeks holiday in Cairns with the family.
In the middle of the Cairns Night Market, along the Promenade, you will find Peter Nicholson with his pet Pythons. For a donation you can handle the snakes, and ask all sorts of questions.
If you are in Cairns, I highly recommend a visit. You can take as many photo's as you like.
One of the powerful features of WingIDE is that you can debug python code, even web applications and Django, with an interactive debugger that is fast and very feature full.
In this article I discuss how I use it with Django. Suppose you have a unit test that hits a relational DB. With Django you can run python manage.py test, to run these tests. When you test this way, Django will do the following
- Create a test database, syncdb your tables, and load any data fixtures
- Run the tests
- Drop the test database
WingIDE has a Unit Testing interface, but it only allows you to run the tests. In other words, it only runs Step 2 above. I have found no easy way to have Wing do Step 1, then run your selected tests within Wing's Testing system, then do Step 3. I'm working on a way to do this, and will blog about it someday. For now, I run the Django Test system through Wing Debug system, and not through the testing system. Like this.
- In your Django project, in Wing, Choose to edit the file properties for manage.py.
- In the 'Debug' tab of this dialog, and the 'Run Arguments' box type test (and optionally the app name to run tests just for that app).
- Now choose to Debug manage.py by right clicking on that file and choosing Debug Selected
This will cause WingIDE to run the Django test framework. Output will go into the Debug I/O panel. Wing will even stop at breakpoints and enter the debugger.
The advantages of this method
The tests can be run either on the commandline or within WingIDE without any changes or hacks to the source and it is simple to set up.
The disadvantages of this method
You don't get to use the rich Wing interface for running tests which would allow you to run individual tests on demand. Beware too that you might be using manage.py to run the Django runserver within WingIDE. That is also configured in the manage.py Debug dialog. You can only configure one argument to manage.py at a time.
-
Debugging unit tests is very useful.
- YngwieMalmsteen
I do like Wing but Django's integration is too weak. Running tests should be more ergonomic and template tags should be colorized.
btw thank you for sharing your knowledge :)
Overview
Do you have a Django application that uses the Admin Site and you would like to add custom fields to the User object? Below I discuss a object oriented way of implementing this.
Explanation
Subclass User in models.py.
class MyUser(User):
bio=models.TextField(null=True,blank=True)
clubs=models.ManyToManyField(Club,null=True,blank=True)
This allows us a place to add custom fields. For example, I've added a bio TextField and a relationship to a Club model object (not shown) to record the clubs to which the user belongs. For More information consider this explanation from scottbarnham.com
Subclass UserAdmin in your admin.py file.
class MyUserAdmin(UserAdmin):
def __init__(self,*args,**kwargs):
super(MyUserAdmin,self).__init__(*args,**kwargs)
fields = list(UserAdmin.fieldsets[0][1]['fields'])
fields.append('bio')
fields.append('clubs')
UserAdmin.fieldsets[0][1]['fields']=fields
admin.site.unregister(User)
admin.site.register(MyUser,MyUserAdmin)
This subclass allows us a way to add our fields to the standard fieldsets in UserAdmin. After coding this, and updating your database, load up your Django Admin, and have a look. Because MyUser does everything that User does, and more, we can register MyUser and unregister User.
-
perfect.. thanks!
- ark -
nice...
- Arsl
If you want to build PDF's with a python program, then ReportLab software is what you need. Go get it. Set aside some time to read the manual: Reportlab Userguide.
If you have read up on ReportLabs and specifically Playpus, you'll probably think it's hugely powerful, but slightly confusing. The manual does a pretty good job, but I think it leaves out one important point.
When you are laying out your frames, they go across the page, and wrap if necessary. It is not natural to control their exactly placement, and it is not clear how to force a break in the layout. Here's an example
:
elements.append(Paragraph('Innovating Programming,some-style))
elements.append(Paragraph('Picante Solutions',some-other-style))
:
:
References
-
You have really made a commendable effort in making your blog a success. keep posting for many more such great blogs. keep posting. :)
- AustinScott
http://www.worldpharmarx.com -
Hey nice and informative blog please write more and more content Thanks Dude
- Paul Zand
http://www.realviagrarx.net/ -
Keep it up...Come up with good content and make your blog bigger on the portal..All The Best
- Susan Parker
By - http://www.trustviagra.com/ -
Nice click and nice writing liked your post.
- tad
http://www.medexpressrx.com -
<a href="http://www.iservepharmacy.com/">viagra online</a> have you ever heard something about it? I think you should add something smarter than this post. isn't it?
- Ashes -
Hi Mike thanks. It's a "Salty" from Hartley's Crocodile Farm north of Cairns, AU
- Gene (response) -
Is that a crocodile, alligator or Gharial is that photo? Whatever it is, I like it!
- Mike -
Looks good so far. I've bookmarked it and will check it regularly.
- Jim Tubman -
Good work, Gene and fantastic pic of Mt. Taranaki!! Did I spell that right?
- Laurel -
Where is the photo of Mt Taranaki taken from?
- Daz -
Congrats on getting up and running.
- Dero

I generally try to just mock everything up using backend model interaction to recreate the expected conditions.
Fixtures are OK for hard coded invariant data but that's rare. If you have a 20K row table in production and have a edge case that needs testing, generally, I want the smallest test case possible so that my test runner maintains some speed since it's all "manual" test kick offs for me.
- Chris Green