M
M
MalekBV2019-09-26 10:49:31
Django
MalekBV, 2019-09-26 10:49:31

How to redirect to the same page in Django 2?

The models are written:

class Article(models.Model):
#....
def get_absolute_url(self):
        return reverse('detail', kwargs={'id': self.id})

Next views
class ArticleCreateView(View):
def post(self, request):
        bound_form = ArticleForm(request.POST)

        if bound_form.is_valid():
            new_article = bound_form.save()
            return redirect(new_article) # ЗДЕСЬ ОШИБКА

        return render(request, 'blogapp/article_create.html', {'form': bound_form})

The error is:
NoReverseMatch at /create/
Reverse for 'detail' with keyword arguments '{'id': 16}' not found. 1 pattern(s) tried: ['(?P[0-9]+)/$']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2019-09-26
@malekBV

Most likely, there is no named parameter id in the pattern, so NoReverseMatch. Try passing in reverse args or setting a named parameter in a pattern.

R
Roman Kitaev, 2019-09-26
@deliro

Wang, that in your urls.py the group is named crooked or the name is crooked, or it doesn’t exist at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question