Archive for the 'Websites' Category

-image-django one form, two models

This post is a work in progress is now working I am glad to say. I have been working on a django site which needs two models updated for one post. It is actually using models very close to that on django-forums and I have created a forms.py file:


class ThreadForm(forms.ModelForm):
class Meta:
model = Thread
exclude = ('forum', 'sticky', 'closed', 'posts', 'views', 'latest_post_time')

def clean_title(self):
title = self.cleaned_data['title']
if not alnum_re.search(title):
raise forms.ValidationError(ugettext("Titles can only contain letters, numbers and underscores"))

if len(title) < 1:
raise forms.ValidationError(ugettext("Please enter a title"))
return title

class PostForm(forms.ModelForm):
class Meta:
model = Post
exclude = ('thread', 'author', 'time', 'related_item')

def clean_body(self):
body = self.cleaned_data['body']
if len(body) < 1:
raise forms.ValidationError(ugettext("Please enter some body text"))
return body

And then in my views (after importing in the correct models):

@login_required
def groupnewthread(request, slug):
thegroup = get_object_or_404(GroupsOfUser, slug=slug)
if request.method == 'POST':
f = request.POST.copy()
tdata = {
'title': f['title'],
}
pdata = {
'body': f['body'],
}
t = ThreadForm(tdata)
p = PostForm(pdata)
if t.is_valid():
newthread = t.save(commit=False)
newthread.forum = thegroup

if p.is_valid():
newthread.save()
newpost = p.save(commit=False)
newpost.thread = newthread
newpost.author = request.user
newpost.save()

strmessage = 'has created a thread %s‘ % (newthread.get_absolute_url(), newthread.title)
usm = UserStatus(user = newpost.author, message = strmessage)
usm.save()

return HttpResponseRedirect(reverse(’groupdetail’, args=[thegroup.slug]))

else:

t = ThreadForm()
p = PostForm()

objContext = RequestContext(request, {’threadform’: t, ‘postform’: p})
return render_to_response(’groups/group_thread_add.html’, objContext)

Now the bit that is in progress is the returning part of dealing with the form data being bound, but I am going to write a custom handler. This is obviously going to be much easier to handle than an update, which I will have to deal with at a point.

The form HTML looks like this btw:

{% extends "base.html" %}
{% load i18n %}
{% block title %}
	{% trans 'Add a new group thread' %}
{% endblock %}
{% block body %}

{% trans ‘Create a new group thread’ %}

{% if t.errors %}

{% blocktrans count t.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}

{% endif %} {% if p.errors %}

{% blocktrans count p.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}

{% endif %} {{ threadform.as_table }} {{ postform.as_table }}
{% endblock %}

-image-Looks like Django is really taking off

Since the introduction of Google’s appengine which includes support for some of Django’s code, and the template system there has been an incredible amount of press for Django.

At the moment there is no backend support for AppEngine in Django (and DB2 support is still lacking - there was a blog post about a year ago but nothing seems to have happened since, and I would love to be proved wrong on that one) but it must only be a matter of time before this happens so for the moment you have to disable the ORM support. Pity, but I can see why.

But I am glad that Django is getting the attention that it deserves, I have long know that Python is well used in Google (and Yahoo for that matter), and now we should see even more people using Django which can only be a good thing for the ever-growing community.

Here is an excellent write-up on Google App Engine http://www.dougma.com/archives/81


-image-Django and URLS naming

Django URLs naming can be a pain sometimes, unless it is just me but an easy way to debug is to use Django’s shell and import urlresolvers:

[sourcecode language=’Python’]python manage.py shell

>> from django.core.urlresolvers import reverse
>> reverse(’nameofurl’)
[/sourcecode]

If there are arguments in the URLS field (for instance passing in the slug):

[sourcecode language=’python’] >> reverse(’nameofurl’, ’slug’) [/sourcecode]

Then it should resolve the name to the URL path needed. If it doesn’t, well something is wrong. We got a weird Traceback when using Python 2.5 though:

[sourcecode language=’python’]Traceback (most recent call last):
File ““, line 1, in
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 297, in reverse
return iri_to_uri(u’/’ + get_resolver(urlconf).reverse(viewname, *args, **kwargs))
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 282, in reverse
if lookup_view in self.reverse_dict:
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 221, in _get_reverse_dict
self._reverse_dict[pattern.callback] = (pattern,)
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 178, in _get_callback
self._callback = get_callable(self._callback_str)
File “/Users/azimi/Code/Python/Django/django-trunk/django/utils/functional.py”, line 130, in wrapper
result = func(*args)
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 47, in get_callable
lookup_view = getattr(__import__(mod_name, {}, {}, [”]), func_name)
File “/website/xmlrpc.py”, line 8, in

dispatcher = SimpleXMLRPCDispatcher() # Python 2.4
TypeError: __init__() takes exactly 3 arguments (1 given)
>>> reverse(’homepage’)
Traceback (most recent call last):
File ““, line 1, in
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 297, in reverse
return iri_to_uri(u’/’ + get_resolver(urlconf).reverse(viewname, *args, **kwargs))
File “/Library/Python/2.5/site-packages/django/core/urlresolvers.py”, line 284, in reverse
raise NoReverseMatch
NoReverseMatch
[/sourcecode]

Looks like something is broken in support for python 2.5 on OS X with django, but I wonder if it is just us thats found this.


-image-Predictions for 2008

I don’t think its too late to provide some predictions for 2008, so here are mine.

3D Printers
We are going to be seeing a lot more on the subject, although they have been out for a while I think this is going to be the year that we are going to be hearing alot more about 3D printers and 3D printing methods, and seeing a lot more things they can do. Also the price will drop I am sure meaning that there are more around to play with.

3D_printer.gif

Phones
I think the phone market will see some much better models this year, coming from the increased R&D that the iPhone would have produced. Touch screens are already advancing as proved by new models from Samsung, and software will bound to improve. Microsoft will lag behind as usual. The Samsung model SCH-W559 I am particularly interested in checking out as it has a vibrating touch screen (called VibeTonz) that provides feedback to make it feel like a mechanical device.

Samsung-sch-w559.png

OS X viruses or worms.
Apple and OS X are proving increasingly popular due to ease of use and maintenance, but these increase in popularity could mean that we see an increase in nefarious activity on the platform. This is might not be as bad as it seems, it is built on quite a stable secure platform and will lead to increased security on applications and service running on the system.

Facebook backlash.
Facebook is not the elegant social networking site it once was. Too many applications, services and third parties are in on the act and now it looks like a complete mess. It might be just me but it also seems that the hosting is always one step behind. I have heard that the next release they are working on is for collapsable menus and for hiding things, but I have already taken off information from the service.


-image-100 things that you didn’t know about 2007

For once BBC news comes up with something interesting, a list of 100 things that you didn’t know about 2007:

http://www.bbc.co.uk/blogs/magazinemonitor/2008/01/100_things_we_didnt_know_last_3.shtml

Some that I like include:

Newcastle is the noisiest place in England.
Only about half of China’s population can speak the national language, Mandarin.
There is mobile phone reception from the summit of Mount Everest.
Denmark is the happiest country in Europe; Italy the unhappiest.
Cats can be police constables

Incidentally I am now posting this from the latest version of MarsEdit to which I have just upgraded. Best blogging software I have found yet.


-image-John Battelle’s Searchblog: Predictions 2008

This is quite interesting, though I had hoped that he would say Myspace would disappear. Heres hoping.

John Battelle’s Searchblog: Predictions 2008: “This year I am going to organize my predictions by companies (just the big ones) and trends. I’m focusing on advertising and search markets and the largest companies in that space, as that seems to be what’s on our collective minds these days, and it’s what I seem to have focused on in the past, as I read through my past prediction posts.

So what are the trends in 2008?”


-image-Django and OSX

Those who have been here before know that I am a big fan of the Django python webframework, and also a big fan of OS X. So I thought that I would install Django on my MacBook Pro.

I did have it all installed on my G4 laptop, but I am going to be using it with at least two different databases on this machine. I am not going to give a complete, what I typed style install, just an overview because it is fairly easy.

1. Install latest version 2.4.* of Python from http://www.pythonmac.org
2. Check out the development version of Django from http://www.djangoproject.com/download/ (I checked it out in /django - probably not the best place to put it.)
3. Install latest stable version (5.1) of MySQL from http://www.mysql.com/
4. Install latest stable version of MySQL tools http://www.mysql.com/products/tools/administrator/
5. Install MySQLdb from http://www.pythonmac.org/packages/py24-fat/index.html
6. Create a symlink so Python can find django: ln -s /django/django_src/django/ /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django
7. Then I followed some instructions on blog.vixiom.com to add the path that I need.
8. edit your .bash_profile file in your home directory with the following path: /django/django_src/django/bin/:/Library/Frameworks/Python.framework/Versions/Current/bin:

Now that should be everything needed to start running with Django and MySQL. PostGreSQL is of course the better database but for the testing work that I am doing my webhost at the moment doesn’t support PostgreSQL..


-image-upgraded wordpress

Just a quickone, but I have upgraded wordpress on this site to version 2.2.1 which should be the very latest. I had it stuck at a very old version before so it should operate better now, and also I should have all the latest bug fixes.


-image-Langalist

The excellent Langalist now has a blog up at  http://www.langa.com/blog/  to compliment the always excellent email list.

Today I am going to try out the X1 desktop indexed search tool that they recommended.  For more information on that either check out langa blog or visit http://www.x1.com/download/
directly.


-image-YUI and Camino

So I have been adding some AJAX goodness to the new site that I am building (all will be revealed soon after testing!) and after some help choosing a framework I decided upon Yahoo’s YUI.

It helps that some others are already looking at this library, but I am so far having trouble with Camino.

Camino is my favorite browser on the Mac by far, so of the niceness of the Gecko browsing engine along with better Mac integration but should I support it. I want to degrade it nicely, turning it over to my normal XHTML forms but I have yet to find a way. So I am going down the route of trying to find out manually.

Even though its a minor browser I still feel that I should do my best, to find out how I decided to look at the stats for this site over the past 2 months to see using what and this is the result:

Firefox: 52.38%
Internet Explorer: 34.21%
Safari: 6.24%
Opera: 3.53%
Mozilla: 1.89%
Konqueror: 0.72%
Camino: 0.69%
Netscape: 0.24%
Mozilla Compatible Agent: 0.07%
Galeon: 0.03%

So quite minor, but I like Camino so I should, and I like Opera so I just need to check that on a Mac, but I wouldn’t be surprised if that figure is me!

updates to come.


Bad Behavior has blocked 370 access attempts in the last 7 days.