E
E
Evgeny Abaev2021-01-19 10:48:21
Django
Evgeny Abaev, 2021-01-19 10:48:21

How to redirect the user to the current page?

There is a controller that is responsible for adding comments to the post

#views.py

class AddResponse(View):
    """Отклики"""
    def post(self, request, pk):
        form = ResponseForm(request.POST)
        post = Bb.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            form.user = request.user
            form.post = post
            form.save()
        return redirect('index')


It after successful addition redirects me to the main page. Please tell me how to make it redirect to the current page of the post

#models.py
class Bb(models.Model):
    title = models.CharField(max_length=50, verbose_name="Суть вопроса")
    content = models.TextField(null=True, blank=True, verbose_name="Детали вопроса")
    rubric = models.ForeignKey('Rubric', null=True, on_delete=models.PROTECT, verbose_name="Предмет")
    price = models.FloatField(null=True, blank=True, verbose_name="Максимальная цена")
    published = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name="Опубликовано")
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, default=User, verbose_name="Пользователь")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-19
@bacon

What's the problem with reading the docs first instead of running straight to the toaster? https://docs.djangoproject.com/en/3.1/topics/http/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question