Used by Django 1.10.4. I have a Stream model for which I created CreateView. When creating objects through the admin panel, everything works fine, but when I use the CreateView form, the object is created (neither in the admin panel nor in the database are there any differences from others), but attempts to specify a link to it through DetailView result in an error:

NoReverseMatch at /
Reverse for 'detail_stream' with arguments' () and keyword arguments' {'pk': 17} 'not found. 2 pattern (s) tried: ['(? P [0-9]) / $', 'streams / (? P [0-9]) / $']

This error occurs when ListView is displayed, and only for an object created through CreateView.
The place where the error occurs:

{% for item in stream_list %} <a href="/streams{% url "detail_stream" pk=item.id %}"> ... </a> {% endfor %} 

When you try to go directly to the DetailView ( http://127.0.0.1:8000/streams/17 ), an error 404 occurs.
urls.py:

 from django.conf.urls import url from .views import StreamList, StreamDetail, StreamUpdate, StreamCreate, ChannelList, ChannelDetail, ChannelUpdate, ChannelCreate, autocomplete, search, filter, follow, follow_list, following_online urlpatterns = [ url(r'^$', StreamList.as_view(), name='streams'), url(r'^(?P<pk>[0-9])/$', StreamDetail.as_view(), name='detail_stream'), url(r'^(?P<pk>[0-9])/update/$', StreamUpdate.as_view()), url(r'^add/$', StreamCreate.as_view(), name='new_stream'), url(r'^channels/$', ChannelList.as_view()), url(r'^channel/(?P<pk>[0-9])/$', ChannelDetail.as_view(), name='detail_channel'), url(r'^channel/(?P<pk>[0-9])/update/$', ChannelUpdate.as_view()), url(r'^channels/add/$', ChannelCreate.as_view()), url(r'^autocomplete/$', autocomplete), url(r'^search/$', search), url(r'^search?limit=(?P<limit>[0-9])/$', search), url(r'^filter/$', filter), url(r'^follow/$', follow), url(r'^following/$', follow_list, name='following'), url(r'^following_online', following_online) ] 

Also, stream URLs are added to the site's main urlpatterns.
View:

 class StreamForm(ModelForm): class Meta: model = Stream fields = ('streamer', 'name', 'date_start', 'channel', 'tags', 'logo') widgets = {'streamer': HiddenInput} class StreamCreate(LoginRequiredMixin, CreateView): login_url = '/login/' form_class = StreamForm model = Stream def get_initial(self): streamer, _ = Streamer.objects.get_or_create(user_id=self.request.user.id) return {'streamer': streamer } def get_form(self, form_class=None): form = super(CreateView, self).get_form(form_class) form.fields['channel'].queryset = Channel.objects.filter(Q(owner__user=self.request.user) | Q(streamers__user=self.request.user)) form.fields['tags'].widget = SelectMultiple() return form def form_valid(self, form): regex = re.compile('[^a-zA-Z]') newtags = [] for tag in form.cleaned_data['tags']: tag = regex.sub('',tag) newtags.append(tag) form.cleaned_data['tags'] = newtags return super(StreamCreate,self).form_valid(form) def form_invalid(self, form): print(form.errors) return super(StreamCreate,self).form_invalid(form) def get_success_url(self): return reverse('streams') 

I suspect that I did not consider something in CreateView, but I can not understand what.
Answer: https://stackoverflow.com/a/41717961/6757293

    1 answer 1

    The answer is found here: https://stackoverflow.com/a/41717961/6757293 , it's in the pattern

    url(r'^(?P<pk>[0-9])/$', StreamDetail.as_view(), name='detail_stream ')

    which is only suitable for single-digit pk, and you need to add a '+':

     url(r'^(?P<pk>[0-9]+)/$', StreamDetail.as_view(), name='detail_stream`')