Yes, you need to create the slug in your save() method. Something
like:

class Review(models.Model):
...
def save(self):
        # add slug if there isn't one already
        if not self.slug:
            from django.template.defaultfilters import slugify
            self.slug = slugify(self.title)
            super(Review, self).save()


Note that this will not guarantee unique slugs. If you want your slugs
to be unique, here is a helpful article: 
http://code.djangoproject.com/wiki/SlugifyUniquely

On Nov 5, 2:11 pm, "William Purcell" <[EMAIL PROTECTED]>
wrote:
> I think my problem might be that I'm not saving the slug. When I save a
> review I do...
>
> review = Review(.....) #<---- Everything but a slug gets passed as a kwarg
> review.save()             #<--- Then I save the review.
>
> How do I save the slug when I save the review. Does Django automajically do
> this for me or do I need to do something with a review.save() method?
>
> Thanks again,
> Bill
>
> On Wed, Nov 5, 2008 at 3:51 PM, William Purcell
> <[EMAIL PROTECTED]>wrote:
>
> > I am trying to create an application to help some classmates and myself
> > team up on journal article reviewing. I have everything working nicely
> > except being able to create a slug from the title of the journal article and
> > then using it in urls.py and creating links in templates. The application is
> > called journal_review. Journal_review.models has a class called Review and
> > in it I have...
>
> > class Review(models.Model):
> >      ....
> >      slug = models.CharField(max_length=60)
> >      .....
>
> > and then in journal_review.admin I have ...
>
> > class ReviewAdmin(admin.ModelAdmin):
> >     ...
> >     prepopulated_fields = {'slug': ('title',) }
> > admin.site.register(Review, ReviewAdmin)
>
> > Then in a template I want to do something like...
> >    {% for review in latest_review_list %}
> >     <li> {{review.pub_date.ctime}}|
> >          <a href={{review.link}} > {{ review.title }} </a>|
> >       {{ review.review }} |
> >           {{ review.username}} |
> >          * {{ review.slug }} *|
> >       {% if review.tags %}
> >          {% for tag in review.tags %}
> >             {{tag}}
> >          {% endfor %}
> >       {% endif %}
> >     </li>
> >     {% endfor %}
>
> > In the above, review.link is a method of journal_review.models.Review and
> > it looks like this ...
>
> >     def link(self,obj):
> >         return self.get_absolute_url()+self.slug
>
> > I don't think that any slugs are being generated because {{ review.slug }}
> > renders as a blank line and {{ review.link }} renders as the current page.
>
> > My urls.py for this is....
> >     (r'^journal_review/$','testproject.journal_review.views.index'),
> >     (r'^journal_review/(?P<review_slug>[a-zA-Z0-9_.-]+)/$',
> > 'testproject.journal_review.views.detail'),
>
> > Excuse my ignorance as I am fairly new to django and I appreciate any help
> > or direction.
> > Thanks,
> > Bill
>
> > P.S. I am using Django 1.0
--~--~---------~--~----~------------~-------~--~----~
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