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

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/d7fbe2f8-2548-4134-8014-3433952ef375%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to