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
<[email protected]>:
> 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 [email protected]
>> <mailto:[email protected]>.
>> To post to this group, send email to [email protected]
>> <mailto:[email protected]>.
>> 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.
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
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.