How to pass a string as url's parameter

29 views
Skip to first unread message

I. Dié

unread,
Aug 13, 2015, 3:18:38 PM8/13/15
to Django users
Hi evrybody,

Here is my model:

# fruits/models
    Class Fruit (models.Model):
            name =  models.CharField(max_length = 33,
                               choices = NAME_CHOICES, default = MANGO)
           # and so on...

# fruits/views
def fruit(request, fruit_name):
    fruit = get_object_or_404(Fruit)
    return render(request, 'fruits/fruit.html', {'fruit': fruit})

# fruits/urls
urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^(?P<fruit_name>[-\w]+)/$', views.fruit, name='fruit'),
]

When I used fruit_id, everything was ok.
But I want to know how to use Fruit.name (a string) as url's parameter instead of fruit_id like this: myverybigmarket/fruits/mango better than myverybigmarket/fruits/1

Ps: this is my first python/django project.

Thanks

James Schneider

unread,
Aug 14, 2015, 5:55:58 AM8/14/15
to django...@googlegroups.com

You probably don't want to use the name directly, you should probably look at creating a slug from the name and store it in a SlugField on your model.

https://docs.djangoproject.com/en/1.8/ref/models/fields/#slugfield
https://docs.djangoproject.com/en/1.8/ref/utils/#module-django.utils.text

Your URL regex already looks correct to capture a slug.

At that point it's just a matter of pulling the object via the slug in your view:

fruit = get_object_or_404(Fruit, slug=fruit_name)

Of course that's assuming that your SlugField is named 'slug'. Make sure to read up on slugs since they need to be unique, and generating them may be a challenge.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/404e6ef5-99d7-4e38-8bf1-978b8690d733%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Snezhana Spasova

unread,
Aug 14, 2015, 7:07:17 AM8/14/15
to Django users
If I understand the question correct..

you can do 

fruit = get_object_or_404(Fruit, name=fruit_name) even if its not SlugField.

I dont recommend doing that with field that doesn't have unique=True because error will be raised if there are more than one objects with this name. 
(This equals Fruit.objects.get(name=fruit_name).)

Snezhana Spasova

unread,
Aug 14, 2015, 7:07:17 AM8/14/15
to Django users
You can do 
  fruit = get_object_or_404(Fruit, name=fruit_name)

but i wont recommend doing this with field that is not unique because it basically resolves to Fruit.objects.get(name=fruit_name) which will raise error if there are two objects with the same name... Furthermore you have choices.. which means you have limited values to choose from.. Either change your field to unique=True and remove choices or don't use this method to get objects..
Hope I helped..

-Snejy

On Thursday, August 13, 2015 at 10:18:38 PM UTC+3, I. Dié wrote:

I. Dié

unread,
Aug 14, 2015, 9:02:36 AM8/14/15
to Django users

Thanks you all  Snejy and James.
Regards


Reply all
Reply to author
Forward
0 new messages