On Fri, 2007-07-06 at 11:41 +0000, Bryan Veloso wrote:
> Alright, well let me get the information out of the way first:
> 
> models.py
> --------------------------------------------------
> char_id             = models.IntegerField('Character ID',
> primary_key=True)
> name                = models.CharField('Name', maxlength=90)
> 
> def get_absolute_url(self):
>     return "/characters/%s/" % (slugify(self.name))
> --------------------------------------------------
> 
> urls.py
> --------------------------------------------------
> character_detail_dict = {
>     'queryset': Character.objects.all(),
>     'template_object_name': 'character',
>     'template_name': 'characters/character_detail.html',
>     'slug_field': 'name',
> }
> 
> (r'^characters/(?P<slug>[-\w]+)/$',     object_detail,
> dict(character_detail_dict)),
> --------------------------------------------------
> 
> So, the problem. This is an existing database (ran inspectdb to get
> it) so obviously I can't add a SlugField to the Character model. Now
> I've only been able to get this to work if the character's name is one
> word, without hyphens. Once you start throwing hyphens in there, it
> throws a 404. I know I'm messing something up, but it's either way too
> late for me to think straight, or I just have no idea what to fix. The
> thing that bothers me is that `name` isn't really a `slug_field` as it
> hasn't been slugified, and the only place I slugify is up there with
> the absolute URL.

All of this looks like it should be correct.

However, this assumes that the data in the "name" field of the database
really are in the same form as your URLs. If a field contains "Bob and
Jane", then your get_absolute_url() function (via slugify) willl return
"/characters/bob-and-jane/" and no amount of matching bob-and-jane
against the name field will match the record with "Bob and Jane" in
there. In other words, slugify() is an irreversible. It's also not a
one-to-one mapping, so if you names aren't sufficiently different, you
may get collisions in the URL structure.

You might need to adopt a different approach to URL construction that is
reversible (writing your own version of slugify, possibly?). 

Alternatively, can you just use the "name" values directly in the URL
(and quote them appropriately with django.utils.http.urlquote())?

>  I don't think slug_field can be a property either
> correct?

That's correct. It has to be something you can use in a QuerySet query.

However, it is easy to write your own view that does any pre-processing
you like (by calling a method on your model, even) and then passes
control onto the generic view to do the "real work". See
http://www.pointy-stick.com/blog/2006/06/29/django-tips-extending-generic-views/
 for an example.

Regards,
Malcolm

-- 
A conclusion is the place where you got tired of thinking. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to