Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread ludovic coues
thanks James for the optimized query :)
It look better than what I did with the two query and the conditionnal

2016-08-21 21:20 GMT+02:00 Yunus :
> You are right this is a good solution. I also named my URL like you. Thanks
> for the answer.
>
> On Sunday, August 21, 2016 at 10:01:24 PM UTC+3, James Schneider wrote:
>>
>>
>>
>> On Sun, Aug 21, 2016 at 4:56 AM, ludovic coues  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
>
> --
> 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 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.
>
> For more options, visit https://groups.google.com/d/optout.



-- 

Cordialement, Coues Ludovic
+336 148 743 42

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEuG%2BTZaew_x4HK_do91RvOOM9b%2BBS2JSA3wrFvU5SHnpPEb4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread Yunus
You are right this is a good solution. I also named my URL like you. Thanks 
for the answer.

On Sunday, August 21, 2016 at 10:01:24 PM UTC+3, James Schneider wrote:
>
>
>
> On Sun, Aug 21, 2016 at 4:56 AM, ludovic coues  > 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
>

-- 
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 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.
For more options, visit https://groups.google.com/d/optout.


Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread James Schneider
On Sun, Aug 21, 2016 at 4:56 AM, ludovic coues  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

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWj6RPWw0Hh78hWhNgz-kUZC%2Bow%3DLAqwU6FNTBbcUrzTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread Yunus
Thanks for the answer :)

On Sunday, August 21, 2016 at 12:02:26 PM UTC+3, voger wrote:
>
> 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[\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[\w-]+)/(?P\d+)(?:/(?P[\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  
> > . 
> > 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/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_source=footer>.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c54347a8-42fa-4359-8ecc-6498f602f351%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread Yunus
Yes, it's working. Thank you very much :)

On Sunday, August 21, 2016 at 9:20:42 PM UTC+3, ludovic coues wrote:
>
> 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 
>
> 2016-08-21 19:42 GMT+02:00 Yunus : 
> > 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) 
> > }) 
> >  
> > 
> > 
> > On Sunday, August 21, 2016 at 2:56:57 PM UTC+3, ludovic coues 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 
> >> 
> >> 2016-08-21 11:01 GMT+02:00 'voger' via Django users 
> >> : 
> >> > 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[\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') 
> >> >> 
> >> >> 
> >> >> 

Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread ludovic coues
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

2016-08-21 19:42 GMT+02:00 Yunus :
> 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)
> })
> 
>
>
> On Sunday, August 21, 2016 at 2:56:57 PM UTC+3, ludovic coues 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
>>
>> 2016-08-21 11:01 GMT+02:00 'voger' via Django users
>> :
>> > 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[\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[\w-]+)/(?P\d+)(?:/(?P[\w\d-]+))?/$',
>> >> view=views.LinkDetailView.as_view(),
>> >> name='link_detail'
>> >> ),
>> >>
>> >>
>> >> ...
>> >> ]
>> >> |
>> >>
>> >> --
>> >> You received this message because you 

Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread Yunus
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)
})



On Sunday, August 21, 2016 at 2:56:57 PM UTC+3, ludovic coues 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 
>
> 2016-08-21 11:01 GMT+02:00 'voger' via Django users 
> : 
> > 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[\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[\w-]+)/(?P\d+)(?:/(?P[\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  
> >> . 
> >> To post to this group, send email to django...@googlegroups.com 
>  
> >> 

Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread ludovic coues
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
:
> 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[\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[\w-]+)/(?P\d+)(?:/(?P[\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+unsubscr...@googlegroups.com
>> .
>> To post to this group, send email to django-users@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
>>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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 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

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEuG%2BTZRUhFmsc8ZAt1Jb8kPLDxRzDwDvBH09xGD1NhETzqDbQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I capture two slugs in one URL pattern?

2016-08-21 Thread 'voger' via Django users
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[\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[\w-]+)/(?P\d+)(?:/(?P[\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+unsubscr...@googlegroups.com
.
To post to this group, send email to django-users@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
.
For more options, visit https://groups.google.com/d/optout.


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


How can I capture two slugs in one URL pattern?

2016-08-20 Thread Yunus
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[\w-]+)/(?P\d+)(?:/(?P[\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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.
For more options, visit https://groups.google.com/d/optout.