Re: newforms.CharField now returns unicode, but how to specify encoding?

2007-07-27 Thread Sam Morris

On Fri, 27 Jul 2007 05:42:12 -0700, Gilbert Fine wrote:

> I used django rev. 5559 for some time. After svn up today, I found
> CharField's cleaned_data is unicode. I think it is a good idea.
> Actually, I made this conversion in my source program.
> 
> The only question is how to specify encoding of the string sent from
> client browser? My users almost certainly using gb2312, not utf-8.

HTML4's description of the FORM element's 'accept-charset' attribute[0] 
states:

The default value for this attribute is the reserved string 
"UNKNOWN". User agents may interpret this value as the character 
encoding that was used to transmit the document containing this FORM 
element.

[0] http://www.w3.org/TR/html4/interact/forms.html#adef-accept-charset

So, you can try specifying that you want the submitted data to be encoded 
as utf-8 there, or you can serve the pages containing the FORM as utf-8 
and hope that the browser behaves sensibly and follows suite.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread Sam Morris

On Mon, 23 Jul 2007 16:57:42 -0700, to_see wrote:

> Snippet #26 solved my problem, essentially in two lines.  I could not
> write those lines for myself now, and I'm not certain I'll ever be able
> to do so.

I'm going to assume it's the Python code itself that is confusing you,
rather than the newforms API...

The lines you're talking about are the body of the constructor in the 
following code:

class AddSnippetForm (forms.Form):
  def __init__ (self, *args, **kwargs):
super (AddSnippetForm, self).__init__ (*args, **kwargs)
self.fields['language'].choices = [('', '--')] + [(lang.id, 
lang.name) for lang in Language.objects.all()]

The first line of the constructor is calling the constructor of the
AddSnippetForm's base class, which is forms.Form. This is necessary
so that the work done by the Form class's constructor is still
performed, even though the constructor was overridden by the child
class.

For details, see the definition of the super function in . I *think* (though I am
not certain) that this line could also be written as:

  forms.Form.__init__ (self, *args, **kwargs)

The funny *args and **kwargs arguments simply pass any positional
and keyword arguments that the constructor was called with through
to the constructor of the base class.

The second line is changing the 'choices' property of the 'language' 
field of the form. So far so good -- but what is that weird looking 
expression inside the second set of square brackets on the right hand 
side of the assignment?

Weel, it is a list comprehension. List Comprehensions are just a
concice way of evaluating an expression over each member of a list, and
returning a list of the results. That line could be written out in full 
as:

  x = [('', '--')]
  for lang in Language.objects.all ():
x.append ((lang.id, lang.name))
  self.fields['language'].choices = x

Or by using the map function, as:

  self.fields['language'].choices = [('', '--')] + map (lambda lang: 
(lang.id, lang.name), Language.objects.all ())

More details about List Comprehensions can be found:

 * in the Python tutorial
   <http://docs.python.org/tut/node7.html#SECTION00714>
 * in the Python Language Reference
   <http://docs.python.org/ref/lists.html#l2h-352>
 * in PEP 202 (where List Comprehensions were originally defined)
   <http://www.python.org/dev/peps/pep-0202/>

When learning any new API/framework along with a language at the
same time, it is often difficult to determine whether something
that you find confusing is a feature of the language, or just an
artifact of the API/framework.

In my experience, once you break down the various little bits
(and know where to find the documentation!), code like this will
start to look a bit less magical and a bit more like, well, regular
code. :)

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Feisty-updates now contains Python-2.5.1final

2007-05-27 Thread Sam Morris

On Sun, 27 May 2007 03:40:39 -0700, Vinay Sajip wrote:
> Thanks for the update, Mike, but I can't seem to get this latest version
> - the Python I can get is dated May 2:
> 
> [EMAIL PROTECTED]:~$ sudo apt-get update [snip]
> Get: 5 http://archive.ubuntu.com feisty-updates/main Packages [20.1kB]
> Get: 6 http://archive.ubuntu.com feisty-updates/restricted Packages
> [14B]
> Get: 7 http://archive.ubuntu.com feisty-updates/main Sources [4974B]
> Get: 8 http://archive.ubuntu.com feisty-updates/restricted Sources [14B]
> Fetched 57.8kB in 1s (40.4kB/s)
> Reading package lists... Done
> [EMAIL PROTECTED]:~$ sudo apt-get install python2.5 Reading package
> lists... Done
> Building dependency tree
> Reading state information... Done
> python2.5 is already the newest version. 0 upgraded, 0 newly installed,
> 0 to remove and 0 not upgraded. [EMAIL PROTECTED]:~$ python
> Python 2.5.1 (r251:54863, May  2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu
> 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or
> "license" for more information.
>>>>
>>>>
> It's labelled Python 2.5.1, but are we talking about the same version?

You can check by examining the version of the 'python' package that you 
have installed:

 $ dpkg --status python
 Package: python
 Status: install ok installed
 ...
 Version: 2.5.1-0ubuntu3
 ...

and you can see which versions of 'python' are available with:

 $ apt-cache policy python
 python:
   Installed: 2.5.1-0ubuntu3
   Candidate: 2.5.1-0ubuntu3
   Version table:
  *** 2.5.1-0ubuntu3 0
 500 http://gb.archive.ubuntu.com feisty-updates/main Packages
 100 /var/lib/dpkg/status
  2.5.1~rc1-0ubuntu3 0
 500 http://gb.archive.ubuntu.com feisty/main Packages

Here you can see I have 2.5.1 final installed.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Django, Postgres and Core Dumps

2007-05-26 Thread Sam Morris

On Sat, 26 May 2007 19:28:03 +1000, Malcolm Tredinnick wrote:
> On Sat, 2007-05-26 at 02:20 -0700, Grant D. Watson wrote:
>> Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
>> 
>> > > In an unusually (for Ubuntu) boneheaded move, it
>> > looks
>> > > like Ubuntu doesn't leave core files for packaged programs, even
>> > > after a "ulimit -c unlimited".  Any other way to pull this off?
>> > 
>> > I wouldn't have thought it was possible to permanently disable that,
>> > so
>> > my first thought would be you haven't changed the limits for the
>> > right
>> > process.
>> 
>> Entirely possible, but I ran the "ulimit -c unlimited" in the same bash
>> session that I subsequently ran the sync in.
>> 
>> > Is Python crashing? Or the PostgreSQL server?
>> 
>> Python:
>>   ~$ python project/manage.py syncdb
>>   Segmentation fault (core dumped)
> 
> Oh, right. :-(
> 
> I think you're a little bit doomed here, if Ubuntu ships stripped
> binaries. On a Fedora system I would install the *-debuginfo at this
> point. Then you could try
> 
> gdb python
> ...
> (gdb) run manage.py syncdb
> 
> and at least you will be in gdb when it crashes and can type "bt" to see
> what's going on. Though, as I said, with stripped binaries this is going
> to tell you nuffink.

On Debian, Ubuntu & friends the package is python-dbg. After installing 
that, run 'gdb python-dbg', and then use gdb normally.

BTW, the updated python 2.5.1 package entered feisty-updates recently, so 
assuming you have a line like this in your sources.list:

  ## Major bug fix updates produced after the final release of the
  deb http://gb.archive.ubuntu.com/ubuntu feisty-updates main restricted 
universe multiverse

then a normal upgrade should pull it in. You can check which version
of the python package you have installed with a command like:

$ apt-cache policy python
python:
  Installed: 2.5.1-0ubuntu3
  Candidate: 2.5.1-0ubuntu3
  Version table:
 *** 2.5.1-0ubuntu3 0
500 http://gb.archive.ubuntu.com feisty-updates/main Packages
100 /var/lib/dpkg/status
 2.5.1~rc1-0ubuntu3 0
500 http://gb.archive.ubuntu.com feisty/main Packages

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Upgrade from Python 2.4 to 2.5

2007-05-07 Thread Sam Morris

On Mon, 07 May 2007 09:19:58 -0700, RollyF wrote:

> I am using fedora 6 distribution which comes with Python 2.4.3. I then
> installed Python 2.5.1 and re-installed the  packages I needed for my
> application. I verified that at the terminal, when I typed in "python",
> what's loaded is Python 2.5. I am runnign apache2 and mod_python. I
> recompiled and installed mod_python to python 2.5. When I run my
> application, it's coming back with:
>
> ...
>
> How do I configure Apache2 or mod_python to use Python 2.5?

The packages that Fedora provide will use the system's python 2.4 
libraries and modules. To change this, you will need to rebuild 
mod_python against your python2.5 installation. There are many ways to go 
about this, someone more familiar with Fedora might be able to help you 
with the specifics, but broadly:

 1. Remove the existing package of mod_python that is installed
 2. Grab the mod_python sources from <http://www.modpython.org/>
 3. Follow the installation instructions at <http://www.modpython.org/
live/current/doc-html/installation.html>. Make sure you tell mod_python's 
configure script where to find your local installation of python2.5 by 
using an option like: --with-python=/usr/local/bin/python.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: TIME_ZONE Error? Anyone ever get this?

2007-04-29 Thread Sam Morris

On Sun, 29 Apr 2007 00:43:52 +, Roboto wrote:

> I'm using Piotr Diamanda MyghtyBoard and I keep running into this error
> when I try this 1 particular view...  I've asked Piotr directly and he
> indicated it could be a django/server issue.  Any thoughts?

Set log_min_error_statement = error in your postgresql.conf and then all 
statements that generate an error will be logged; hopefully you'll be 
able to see the statement that caused the transaction to be aborted in 
the first place.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: IEPngFix Hack for Django ?

2007-04-27 Thread Sam Morris

On Fri, 27 Apr 2007 05:23:40 -0700, dbee wrote:
> My site is at flowerhour.biz. I've been at this thing for hours now, and
> I've done everything I can think of to make it run...

$ HEAD http://www.flowerhour.biz/site_media/css/iepngfix.htc | grep -i 
content-type
Content-Type: text/html; charset=utf-8

You need to configure your web server to serve the file with the correct media
type, text/x-component.

After you do this you will have to delete your temporary internet
files (not from inside Internet Explorer--you have to log off,
log on as another user, and totally remove the entire temporary
internet files directory), because MSIE seems to remember the fact
that the iepngfix.htc resource was once served with the incorrect
media type, and ignores it from then onwards.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Must be a bug??? "ordering" error splits field

2007-04-05 Thread Sam Morris

On Thu, 05 Apr 2007 14:10:05 +0800, Russell Keith-Magee wrote:

> On 4/5/07, TaMeR <[EMAIL PROTECTED]> wrote:
> 
>> class Meta:
>> ordering = ('code')
> 
> You have been bitten by a very common Python error:
> 
> ('code') defines a string
> ('code',) defines a tuple containing a single element, that is a string.

Finally, ['code'] defines a list containign a single element. IMO it's 
much easier to type, and easier/less confusing for humans unfamiliar with 
Python to parse and understand. :)

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



djschemadiff - show differences between Django database schemas

2007-03-20 Thread Sam Morris
Tired of manually updating your database schema after changing your
model classes? So was I! I wrote a tool called djschemadiff to
automatically compare your database's current schema to the schema that
would be produced if you started with a fresh, empty database and then
ran syncdb.

Currently it works with PostgreSQL 8.1, but shouldn't be too hard to
extend to work with other database systems.

djschemadiff can be downloaded from
<http://robots.org.uk/src/djschemadiff/>. Feel free to send me any
comments, flames or patches!

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


signature.asc
Description: This is a digitally signed message part


Re: ManyToManyField, limit_choices_to and properties of the request object

2007-03-19 Thread Sam Morris

On Mon, 19 Mar 2007 09:29:08 -0700, quentinsf wrote:
> I need to do something similar.  In a form I want to limit the options
> in a ForeignKey's pull-down select field to objects owned by an
> organisation of which the user is a member.
> 
> I've been trying to work out where this would fit.  Any help much
> appreciated.

It turns out that you can just set the field's choices after the
manipulator has been created:

 m = Post.AddManipulator ()
 m['images'].choices = [(i.id, i) for i in Image.objects.filter (author = 
request.user)]

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



ManyToManyField, limit_choices_to and properties of the request object

2007-03-19 Thread Sam Morris
Is it possible to use a ManyToManyField's limit_choices_to attribute to
limit a user to picking only from related objects that have author =
request.user?

Given that request has no place in the model definition, would it be
necessary to write a custom manipulator?

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


signature.asc
Description: This is a digitally signed message part


Re: how to use .htc file in django

2007-03-12 Thread Sam Morris

On Mon, 12 Mar 2007 05:15:14 -0700, 骛之 张 wrote:

> hi everyone here, how to use .htc file in django
> 
> if in j2ee or nomal html file, i can define in table as  style="behavior:url(/htc_path/js/TableSort.htc) url(/htc_path/js/
> TableHL.htc) url(/htc_path/js/DragDrop.htc);">
> 
> but in django, I cannot do it correctly, help me, plz

What is your issue exactly? The only problem we ran into
was the one described at <http://support.microsoft.com/kb/306231/en-us>.

The fix is webserver specific. The Django built-in development web server
will check /etc/mime.types on Unix, so editing that file is sufficient.

On Windows, you have to do a bit more work (as usual): edit python's
mimetypes.py so that it searches for the mime.types file in a known
location, then create a mime.types file at that location with the correct
contents.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Implementing entity relationships that use compound keys in a Django application

2006-10-03 Thread Sam Morris
Hello everyone! I am new to Django and am currently learning how to use
the model API. I am trying to construct a set of models to represent a
number of people (characters in a game), who can be members of one or
more organisations. Each person in an organisation has a rank in that
organisation. My original sketch of how I would do this directly in a
database looks like this:

Person
Organisation
Rank (*organisation, *rank, name)
Membership (*person, *organisation, rank)

Membership.person -> Person
Membership.(organisation + rank) -> Rank

Asterisks denote primary keys; where more than one field has an asterisk
then the primary key is a compound key.

My problem is that Membership.(organisation + rank) is a compound
foreign key, which Django does not support. My current implementation of
the relationship uses a linkage model:

class OrgMembership (models.Model):
person = models.ForeignKey (person)
organisation = models.ForeignKey (Organisation)
rank = models.ForeignKey (Rank)

class Meta:
unique_together = [['person', 'organisation']]

This allows OrgMembership.rank to be set to any possible rank, but I
only want to allow OrgMemberships where Rank.organisation ==
OrgMembership.organisation.

The solutions I have come up with so far are:

  * Forget the relationship between Membership.rank and Rank -- turn
OrgMembership.rank into a simple integer
  * Create a database constraint to enforce the restriction, and
somehow alter the admin page to only allow legal values (using
limit_choices_to?)
  * Give the OrgMembership model a custom validation rule
(presumably complemented by a manually-created database
constraint)
  * Decompose the relationship somehow

By 'decompose' I mean changing the schema to something like this:

Rank (level, name)
OrgRank (organisation, rank)
OrgMembership (person, org_rank)

The final option is probably the best one, but I thought I would post
anyway to see if any more experienced users could confirm/deny, and to
see if there are any other approaches that I have missed.

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


signature.asc
Description: This is a digitally signed message part