Django - Mixin with TemplateView - django

I am trying to do something like the following to work, but I keep receiving the error 'RegionsView' object has no attribute 'method'. What am I doing wrong? Thanks
#views.py
class _LanguageMixin(object):
def dispatch(self, request, *args, **kwargs):
self.langcode = kwargs.pop("langcode")
self.language = get_object_or_404(Language, pk=self.langcode)
return super(_LanguageMixin, self).dispatch(self, request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(_LanguageMixin, self).get_context_data(self, **kwargs)
context.update({"language": self.language,
"languages": Language.objects.values_list('code',
flat=True)})
return context
class RegionsView(_LanguageMixin, TemplateView):
template_name = "regions.html"
def get_context_data(self, **kwargs):
context = super(RegionsView, self).get_context_data(self, **kwargs)
regions = #......
context.update({"regions": regions})
return context
#urls.py
url(r'^(?P<langcode>[a-zA-Z-]+)/regions/$', RegionsView.as_view(), name='regions')

return super(_LanguageMixin, self).dispatch(request, *args, **kwargs)
instead of
return super(_LanguageMixin, self).dispatch(self, request, *args, **kwargs)
(request.method is used in the dispatch function, but you use self object)

Related

How to refactor ViewSet that retrieves records created by authenticated user

I need these methods to retrieve/update/delete records created by authenticated user only. I'm clearly violating one of the key principles of web/software development. How can i refactor this viewset?
class AddressViewSet(viewsets.ModelViewSet):
queryset = Address.objects.all()
serializer_class = AddressSerializer
authentication_classes = (JWTAuthentication,)
permission_classes = (permissions.IsAuthenticated,)
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def list(self, request, *args, **kwargs):
self.queryset = Address.objects.filter(user=self.request.user)
return super(AddressViewSet, self).list(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
self.queryset = Address.objects.filter(user=self.request.user)
return super(AddressViewSet, self).create(request, *args, **kwargs)
def retrieve(self, request, *args, **kwargs):
self.queryset = Address.objects.filter(user=self.request.user)
return super(AddressViewSet, self).retrieve(request, *args, **kwargs)
def update(self, request, *args, **kwargs):
self.queryset = Address.objects.filter(user=self.request.user)
return super(AddressViewSet, self).update(request, *args, **kwargs)
def destroy(self, request, *args, **kwargs):
self.queryset = Address.objects.filter(user=self.request.user)
return super(AddressViewSet, self).destroy(request, *args, **kwargs)
try to add get_queryset method and remove filtering by user in other methods:
def get_queryset(self):
queryset = Address.objects.filter(user=self.request.user)
return queryset

Limit Django UpdateView to author

I've got a UpdateView in Django that I need to restrict to only the author. I having trouble grabbing the Author off of the request.
class MyPermissionMixin(LoginRequiredMixin, UserPassesTestMixin):
def dispatch(self, request, *args, **kwargs):
user_test_result = self.get_test_func()()
if request.user != ????.user: #How do I grab the user('Author')??
return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs)
Get Autor instance user through self.get_object()
class MyPermissionMixin(object):
def dispatch(self, request, *args, **kwargs):
if request.user != self.get_object().author:
return HttpResponseForbidden()
return super().dispatch(request, *args, **kwargs)

How do I return a class-based view method with super from a function within that very same class-based view method?

Is it even possible?
I can't find anything or figure it out by myself since I'm a beginner, so I turn to you guys.
Here's an example (don't worry about the use case, I just want to know if it's possible and how):
When I run this code I get that *args is not defined. What's wrong?
views.py:
class MyCreateView(CreateView):
def get(self, request, *args, **kwargs):
slug = kwargs['slug']
helper_method(self, slug)
helpers.py:
def helper_method(self, slug):
if slug == "random":
return super(self.__class__, self).get(request, *args, **kwargs)
You have to define args and kwargs, you just need to add them to your method parameters like this:
def helper_method(self, slug, *args, **kwargs):
if slug == "random":
return super(self.__class__, self).get(request, *args, **kwargs)
class MyCreateView(CreateView):
def get(self, request, *args, **kwargs):
slug = kwargs['slug']
helper_method(self, slug, *args, **kwargs)

django override get - templateview

I want to override get method in templateview:
class ...
def get(self, request, *args, **kwargs):
if request.is_ajax():
#do and return something
return super(self, Clasname).get(request, *args, **kwargs) #when not ajax
This is giving error.
Swap the arguments to super, i.e.,
return super(Clasname, self).get(request, *args, **kwargs)

many views which inherit from templateview

I have two views which inherit from templateview and which requires a login through required_login.
It's simple when I created the views separately:
class AboutView(TemplateView):
template_name = 'app1/about.html'
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(AboutView, self).dispatch(*args, **kwargs)
class HelpView(TemplateView):
template_name = 'app1/help.html'
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(HelpView, self).dispatch(*args, **kwargs)
This does work. The question is Why doesn't the code below work
class StaticTemplateView(TemplateView):
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(AboutView, self).dispatch(*args, **kwargs)
class AboutView(StaticTemplateView):
template_name = 'app1/about.html'
class HelpView(StaticTemplateView):
template_name = 'app1/help.html'
Error here:
super(type, obj): obj must be an instance or subtype of type
Thanks in advance
I think the error lies in the copy/paste :)
Instead:
return super(AboutView, self).dispatch(*args, **kwargs)
Try this:
return super(StaticTemplateView, self).dispatch(*args, **kwargs)