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).)
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+unsubscribe@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/ac426d9c-ea3b-4d8c-bd59-4f1b2fd5c7d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment