How can I use `return HttpResponseRedirect(reverse("some-path", args=[slug]))` with user_agent?
I'm looking for a way to use (in the views.py)
return HttpResponseRedirect(reverse("some-path", args=[slug]))
where I can define to which template a user will be redirected according to the device (with user_agent
). The problem is related to the Class-based view with the get and post methods. In my post function I have this unsolved problem in my return statement.
I couldn't find an answer anywhere: what's the best approach for this situation?
The problem is that redirection uses just url.py and I don't know where and how can I write the if statement about the devices, like in normal view function:
if user_agent.is_pc:
return render(
request,
'dev/registration-success_pc_tablet.html',
{
'my_email': my_email
})
elif user_agent.is_mobile:
return render(
request,
'dev/registration-success_mobile.html',
{
'my_email': my_email
})
elif user_agent.is_tablet:
return render(
request,
'dev/registration-success_pc_tablet.html',
{
'my_email': my_email
})
I have set my MIDDLEWARE, INSTALLED_APPS for user_agent. The problem is just with this particular case that I described in my question.
The code that I'm struggling with (last line):
def post(self, request, slug):
comment_form = CommentForm(request.POST)
user_feedback = UserFeedback(request.POST)
user_agent = get_user_agent(request)
post = EssayCls.objects.get(slug=slug)
context = {
"post": post,
"post_tags": post.tags.all(),
"comment_form": comment_form,
'comments': post.comments.all().order_by("-id")
}
if user_feedback.is_valid():
user_email = user_feedback.cleaned_data['email']
send_me_message, was_created = SendMeMessage.objects.get_or_create(email=user_email)
post.guest.add(send_me_message)
return redirect('confirm-registration', slug=slug)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.post = post
comment.save()
return HttpResponseRedirect(reverse("some-path", args=[slug]))