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+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto: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
<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 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.

Reply via email to