into another problem.
When I try to access my Object, I get an error "invalid literal for
int() with base 10". I know it has something to do with ForeignKeys,
but cannot find how to fix it.
**models.py**------------------------------------------------------------------------------------------------------------------
from django.db import models
# Create your models here.
class Artist(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Album(models.Model):
name = models.CharField(max_length=200)
artist = models.ForeignKey(Artist)
def __unicode__(self):
return self.name
class Song(models.Model):
name = models.CharField(max_length=100)
artist = models.ForeignKey(Artist)
album = models.ForeignKey(Album)
date_added = models.DateField('date added')
def __unicode__(self):
return self.name
**views.py**------------------------------------------------------------------------------------------------------------------
from music.models import Song
from django.shortcuts import render_to_response
# Create your views here.
def index(request):
artist = Song.objects.all().order_by('name')
return render_to_response('music/index.html', {'artist': artist})
def artist(request, myartist):
myArgs = Song.objects.all().filter(artist=myartist)
return render_to_response('music/index.html', {'artist': myArgs})
-------------------------------------------------------------------------------------------------------------------------------------
Thanks
On Jan 2, 6:08 pm, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
> 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