How can I capture two slugs in one URL pattern?

281 views
Skip to first unread message

Yunus

unread,
Aug 20, 2016, 11:54:56 AM8/20/16
to Django users
Hello,

I want to two slugs in one URL pattern. These slugs from different models. I have a model Link with a many to one relationship with a model category.

Actually these two slugs is working. But one of the slugs is accepting whatever I write in the category_slug section of the url.

Let's say: I write 127.0.0.1:8000/there_is_no_name_like_that_in_the_database/pk/slug this. I am going to this page but there is no category with this name. So, basically is accepting whatever I write. 
#links/views.py
class LinkDetailView(FormMixin, DetailView):
    model
= Link
    context_object_name
= 'link'
    form_class
= CommentForm
    success_url
= reverse_lazy('home')


   
def get_object(self, queryset=None):
       
return get_object_or_404(Link, pk=self.kwargs['pk'], slug=self.kwargs['slug'])


   
...

#links/urls.py
urlpatterns = [


   
...
   
    url
(
        regex
=r'^k/(?P<category>[\w-]+)/(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$',
        view
=views.LinkDetailView.as_view(),
        name
='link_detail'
   
),


   
...
]

voger

unread,
Aug 21, 2016, 5:02:26 AM8/21/16
to django...@googlegroups.com
I barely understand django my self but I hope I am pointing you in the
right direction.

1. I don't understand what the (?:/(?P<slug>[\w\d-]+))?/ part in your
pattern is supposed to do. My regexp-fu is weak so I can't comment on that

2. Your url will match and pass to the view whatever url you give it. So
even if you give a pk and slug that don't exist in database, still the
view will get called as long as the regexp matches.

3. In your view you check the db against the pk and slug and if not
found __the view__ rises 404.

4. You don't check if category exist in database and you don't rise 404
if not. In this case it is assumed it is always right.


On 20/08/2016 06:10 μμ, Yunus wrote:
> Hello,
>
> I want to two slugs in one URL pattern. These slugs from different
> models. I have a model Link with a many to one relationship with a model
> category.
>
> Actually these two slugs is working. But one of the slugs is accepting
> whatever I write in the category_slug section of the url.
>
> Let's say:I
> write 127.0.0.1:8000/there_is_no_name_like_that_in_the_database/pk/slug this.
> I am going to this page but there is no category with this name. So,
> basically is accepting whatever I write.
> *#links/views.py*
> |
> classLinkDetailView(FormMixin,DetailView):
> model =Link
> context_object_name ='link'
> form_class =CommentForm
> success_url =reverse_lazy('home')
>
>
> defget_object(self,queryset=None):
>
> returnget_object_or_404(Link,pk=self.kwargs['pk'],slug=self.kwargs['slug'])
>
>
> ...
> |
>
> *#links/urls.py*
> |
> urlpatterns =[
>
>
> ...
>
> url(
>
> regex=r'^k/(?P<category>[\w-]+)/(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$',
> view=views.LinkDetailView.as_view(),
> name='link_detail'
> ),
>
>
> ...
> ]
> |
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0b8482ee-5e64-4ee8-a664-d1d938767bd6%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/0b8482ee-5e64-4ee8-a664-d1d938767bd6%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

ludovic coues

unread,
Aug 21, 2016, 7:56:57 AM8/21/16
to django...@googlegroups.com
You need to check somewhere that the category exist and that your item
belong to the category.
The get object method seems like a good place to do it. Something like
that should do the trick

def get_object(self, queryset=None):
""" Return a 404 code if
* no link with given slug/id
* no category with given slug
* link isn't part of the right category
""""
link = get_object_or_404(Link,
pk=self.kwargs['pk'],slug=self.kwargs['slug'])
category = get_object_or_404(Category, slug=self.kwargs['category'])
if not category in link.category_set:
raise Http404
return link

2016-08-21 11:01 GMT+02:00 'voger' via Django users
<django...@googlegroups.com>:
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c9904d3f-f162-cfa5-c9e7-b53330225742%40yahoo.gr.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Yunus

unread,
Aug 21, 2016, 1:42:54 PM8/21/16
to Django users
Hm. I get it but it's look like there is no attribute category_set in link.
Given me this error: 'Link' object has no attribute 'category_set'

Here is my models:

#category/models.py
class Category(models.Model):
 name
= models.CharField("Name", max_length=100)
 user
= models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
 time
= models.DateTimeField(auto_now_add=True)
 slug
= models.SlugField(max_length=100)
 objects
= models.Manager()

 
 
def __str__(self):
 
return self.name


 
def get_absolute_url(self):
 
return reverse('category:category_detail', kwargs={"name": self.name,})


#links/models.py
class Link(models.Model):
    title
= models.CharField("Headline", max_length=100)
    submitter
= models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
    submitted_on
= models.DateTimeField(auto_now_add=True)
    rank_score
= models.FloatField(default=0.0)
    url
= models.URLField("URL", max_length=250)
    category
= models.ForeignKey(Category, null=True)
    description
= models.TextField(blank=True)
    slug
= models.SlugField(unique=True)
    img
= models.ImageField(upload_to="thumbnail/", blank=True)
    image_og
= models.ImageField(upload_to="thumbnail/", blank=True, default="/static/images/entry/default.png")
    with_votes
= LinkVoteCountManager()
    objects
= models.Manager()


   
def __str__(self):
       
return self.title


   
def get_absolute_url(self):
       
return reverse('link:link_detail', kwargs={"category": self.category.slug, "slug": self.slug,
                                                   
'pk': str(self.id)
                                                   
})
   
....

ludovic coues

unread,
Aug 21, 2016, 2:20:42 PM8/21/16
to django...@googlegroups.com
Sorry, I might have mixed things a bit.
You can use "link in category.link_set" or "category == link.category".

link_set is set by django when you declared the foreignkey
>> >> <mailto:django-users...@googlegroups.com>.
> https://groups.google.com/d/msgid/django-users/0e73ba29-be8d-46a2-aaa2-04cac94bfffa%40googlegroups.com.

Yunus

unread,
Aug 21, 2016, 2:32:09 PM8/21/16
to Django users
Yes, it's working. Thank you very much :)

Yunus

unread,
Aug 21, 2016, 2:33:01 PM8/21/16
to Django users
Thanks for the answer :)

James Schneider

unread,
Aug 21, 2016, 3:01:24 PM8/21/16
to django...@googlegroups.com
On Sun, Aug 21, 2016 at 4:56 AM, ludovic coues <cou...@gmail.com> wrote:
You need to check somewhere that the category exist and that your item
belong to the category.
The get object method seems like a good place to do it. Something like
that should do the trick

    def get_object(self, queryset=None):
        """ Return a 404 code if
            * no link with given slug/id
            * no category with given slug
            * link isn't part of the right category
        """"
        link = get_object_or_404(Link,
pk=self.kwargs['pk'],slug=self.kwargs['slug'])
        category = get_object_or_404(Category, slug=self.kwargs['category'])
        if not category in link.category_set:
            raise Http404
        return link



This works (using link.category instead of link.category_set), but it is inefficient (both in lines of code and SQL queries). You're using multiple queries to validate the existence of a single object. The raise statement is also unneeded. I would suggest this:

def get_object(self, queryset=None):
        """Retrieve the object using the given PK, link slug, and category slug.""""
        link = get_object_or_404(Link, pk=self.kwargs['pk'], 
                                                     slug=self.kwargs['slug'], 
                                                     category__slug=self.kwargs['category'])
        return link


Here, we validate the category slug (well, all of the captured URL values) using a JOIN between the Link and Category models, rather than running separate queries and comparing the results in Python. There is also no need to 'raise Http404' because that's the point of get_object_or_404() if the object is not found (ie one of the 3 pieces of information we were given doesn't line up). 

Personally I always name my URL capture arguments with a suffix such as _pk or _slug (ie category_slug) to make it a bit more clear as to what is being captured, but that's a personal preference.

-James

Yunus

unread,
Aug 21, 2016, 3:20:18 PM8/21/16
to Django users
You are right this is a good solution. I also named my URL like you. Thanks for the answer.

ludovic coues

unread,
Aug 21, 2016, 5:41:54 PM8/21/16
to django...@googlegroups.com
thanks James for the optimized query :)
It look better than what I did with the two query and the conditionnal
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/28dfd122-a89d-4b11-8fd2-a13c0354bf20%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages