0

So I have one detail view and one function in my views.py. So i have included the form in detail view template list_detail.html. and upon posting the form successfully. It redirects to all page(homepage basically). Now I want it to redirect to detailview page which is like this. and for that I need to pass the slug value of that List models specific object. But can build the logic here. I am new to django.

path('list/<slug:slug>/', TheirDetailView.as_view(),name='list_detail'),
path('all',views.all, name='all'),
path('create_comment',views.create_comment, name='create_comment'),
class TheirDetailView(DetailView):
    model = List
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        modell = Review.objects.all()
        context["modam"] = modell
        return context
def create_comment(request):
    context = {}
    form = ReviewForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('app:all')
    else:
        context['form'] = form
        return render(request, "app/create_comment.html", context)
  • you mean you edited x person's profile and after saving you want to open its updated detailed profile instead homepage right? – Hemal Patel Oct 10 '22 at 05:25
  • Actually detailview is a blog post page. and create_comment is comment function. so I included the comment form in detail page. So say a user comment on a post the upon posting I want it to redirect to that same detailview page that I had posted the comment from. Currently I have set the redirect to "all" page. So it takes to the homepage. Sorry if im bad at explaining. – Ayat Rahman Oct 10 '22 at 05:49

2 Answers2

1
def create_comment(request):
    # context = {}
    form = ReviewForm(request.POST or None)
    id_List = request.POST["List"]
    slug = List.objects.filter(id=int(id_List)).values("slug").first()
    if form.is_valid():
        form.save()
        return redirect("app:list_detail", slug=slug["slug"])
Hemal Patel
  • 878
  • 6
  • 16
  • hey here args=(post id,)) is what? I mean there is no post in here. and the comma looks wrong here. – Ayat Rahman Oct 10 '22 at 07:21
  • you're commenting on specific post right? so to save comment ofc you'll have post id in form. pass that id in args – Hemal Patel Oct 10 '22 at 07:23
  • id is not working. My detailview path takes slug. so i tried this `redirect_url = reverse("app:list_detail", args=(slug,))` it says slug is not defined – Ayat Rahman Oct 10 '22 at 07:29
  • This is my url for list_detail `path('list//', TheirDetailView.as_view(),name='list_detail'),` – Ayat Rahman Oct 10 '22 at 07:35
  • `return reverse("list_detail", kwargs={"slug": 'slug_value'})` – Hemal Patel Oct 10 '22 at 07:56
  • Its going to this link without taking the slugvalue :( `http://127.0.0.1:8000/home/list/slug_value/` – Ayat Rahman Oct 10 '22 at 08:01
  • is it on git? if yes then pls share – Hemal Patel Oct 10 '22 at 08:04
  • sure man. [Link}(https://github.com/ayat1041/theirlist-website.git) – Ayat Rahman Oct 10 '22 at 08:07
  • ok will update you asap – Hemal Patel Oct 10 '22 at 08:08
  • when does it get called. `create_comment` i cant find from ui. what is the flow – Hemal Patel Oct 10 '22 at 08:35
  • @AyatRahman Updated ans. Please check – Hemal Patel Oct 10 '22 at 09:05
  • Sorry for the delay. In my list_detail. html at line 82 i did this = `{% include 'app/create_comment.html' %}` . and you can see in create_comment.html I have the form action = "create_comment". So from list_detail page im posting a comment to create_comment link. and in create_comment view( line 257 ) if form is valid its gonna take us to all url(which means homepage). But thats what I wanna change. I had passed a context form of ReviewForm in my TheirDetailView (line 115). but realized its not doing anything so I removed it. – Ayat Rahman Oct 11 '22 at 04:09
  • after commenting it will redirect to the same page. updated the answer yesterday. have you checked? – Hemal Patel Oct 11 '22 at 06:37
  • Hey man! Extremely sorry I didnt notice that. Just tried. Its perfect! Thats exactly what I wanted. Thanks a lot! – Ayat Rahman Oct 11 '22 at 08:25
0

Hey did some google and found this in stackoverflow link added this line replacing my redirect under my create_comment function if form.is_valid statement

return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

So my function looks like this

def create_comment(request):
    context = {}
    form = ReviewForm(request.POST or None)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    else:
        context['form'] = form
        return render(request, "app/create_comment.html", context)

Weirdly enough its said in the post that it wont work in incognito/private mode but just checked and it does. But even in some articles its discouraged to use this referrer so still looking for a better solution to this. Thanks everyone.