1

Here is the problem: I have an Update view with overridden get_object method :

class MyUpdateView(SuccessMessageMixin, LoginRequiredMixin, UpdateView):

some code here:

    def get_object(self, queryset=None):
        obj = UpdateView.get_object(self, queryset=None)
        if not obj.author == self.request.user:
            redirect(reverse_lazy("app:url")) # or reverse instead  , no difference
        return obj

p.s. object.author tied via foreign key to a user model(self.request.user in our case)

In this view I want to allow to edit entry only to the users who created this exact entry. I know how to do it via get_queryset or via UserPassesTestMixin but both methods raise errors . Instead i need redirect user who is not the author back to the page where he came from or to a certain URL (parametrical) .

In this case(code is bellow) it works fine, 404 appears , so that logic works. I tried it via web site: author able to edit entry, non - author would conjure 404 .


# in this case it forks fine,  404 appears :

 def get_object(self, queryset=None):
        obj = UpdateView.get_object(self, queryset=None)
        if not obj.author == self.request.user:
            raise Http404
        return obj

But in first example redirect doesn't work for some reason.

I have tried - get_queryset or UserPassesTestMixin, but it not what I need

autor = request.user - allow to edit entry

author != request.user - go to the previous page ot to a certain URL + message

If someone has any idea how to use redirect in this case -please answer. have anice day

---solution ---


    def get(self, request, **kwargs):
        if self.get_object().author == self.request.user:
            return UpdateView.get(self, request, **kwargs)
        else:
            messages.add_message(request, messages.WARNING, "You can only change your own entries")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27

1 Answers1

3

You need to go back and then aware the user with a message using the django message framework The proper way to do this is

from django.http import HttpResponseRedirect

def someview(request):
   ...
   return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Iakovos Belonias
  • 1,217
  • 9
  • 25
  • 1
    Thanks a lot mate, it does work. I have added what i have done (def get()) into the original question. – Aleksei Khatkevich Mar 31 '19 at 10:22
  • 1
    @ Iakovos Belonias . By the way, is it any way how to redirect user to even 1 step behind, that is previous page to a previos page??? I mean by using META data? – Aleksei Khatkevich Apr 01 '19 at 09:31
  • Sure check these links. https://stackoverflow.com/questions/35894990/django-how-to-return-to-previous-url https://stackoverflow.com/questions/12758786/redirect-return-to-same-previous-page-in-django https://stackoverflow.com/questions/50006147/how-to-return-redirect-to-previous-page-in-django-after-post-request If you still having trouble tell me. Also it would help a lot if you can upload your project to github(in general) – Iakovos Belonias Apr 01 '19 at 12:04
  • 1
    @ Iakovos Belonias Well, thats not a much of a project. Just trying to learn Django by making as many mistakes as it possible and fixing them in a same chaotic way by different approaches. – Aleksei Khatkevich Apr 01 '19 at 16:38
  • It's ok, uploading your code on github it's a good place to start. I use my github mostly now as project reference since I have encounter most of the problems. The good with github is that you can always come back to check for something. Big/small project it doesn't matter – Iakovos Belonias Apr 01 '19 at 17:53