Sunday, January 2, 2011

Re: New to Django, sheet music organization site

On 3/01/2011 9:56am, Kyle wrote:
> Ok I have a good start. I have run into a problem though. In my
> database, I have an artist field. Most artists have a space in there
> name. When creating my URLs, how can I ignore white space?
>
> For example, take the artist Chris Tomlin. In my database, it will
> show as "Chris Tomlin", but I don't want to type "music/Chris Tomlin"
> to access it, I want to type "music/christomlin" or "music/
> chris_tomlin". How can I do this?

Stick to Chris Tomlin everywhere in your database because that is the
name you want. Most systems with people names split them into surname
and given name columns but that doesn't seem to make sense in your case
unless you think you will want to search millions of records by separate
surname or given names.

The URL however is a different matter. You need to decide how you want
to refer to the artists in a consistent way and put a decorated method
in your artist model something like ...

@models.permalink
def get_absolute_url(self):
return "/music/artist/%s/" % self.slug

... where self.slug is a field on the artist model where you store the
representation of the artist's name in exactly the way you want to type
it in as a URL.

Note that I added /artist/ to your URL because you will probably want to
do something similar for songs and the same principle applies.

If you did something like this in your artist model ...

slug = models.SlugField(unique=True)

... then the admin app would automatically create the slug for you with
hyphens replacing spaces and everything lower-cased. But you need to
specify in the admin app which field is the source for the slug.

Otherwise, you could write a method of your own on the artist model to
massage the name as you prefer and populate the slug field when saving a
record.

hth

>
> Thanks.
>

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment