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)
Related
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)
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)
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)
Works like a charm:
MyCreateView(CreateView):
template_name = "my_template_name"
form_class = MyModelForm
success_url = "/success/"
But the following doesn't:
MyUpdateView(UpdateView):
template_name = "my_template_name"
form_class = MyModelForm
success_url = "/success/"
I get this error:
MyUpdateView is missing a queryset. Define MyUpdateView.model, MyUpdateView.queryset, or override MyUpdateView.get_queryset().
Why does an UpdateView need model, queryset or get_queryset defined to not cause an error while CreateView doesn't? Shouldn't it be able to automatically derive it from the Model used in the ModelForm?
Currently (django 1.5.1 official release) UpdateView is calling self.get_object() to be able to provide instance object to Form.
From https://github.com/django/django/blob/1.5c2/django/views/generic/edit.py#L217:
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super(BaseUpdateView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super(BaseUpdateView, self).post(request, *args, **kwargs)
And self.get_object method needs one of this properties declared: model, queryset or get_queryset
Whereas CreateView don't call self.get_object().
From https://github.com/django/django/blob/1.5c2/django/views/generic/edit.py#L194:
def get(self, request, *args, **kwargs):
self.object = None
return super(BaseCreateView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = None
return super(BaseCreateView, self).post(request, *args, **kwargs)
You might have a problem in your urls.py file.
What I think you wrote in it is:
url(r'foldername/(?P[0-9]+)/$', views.UpdateView.as_view(), name='update'),
but you have to change UpdateView to MyUpdateView, like this:
url(r'foldername/(?P[0-9]+)/$', views.MyUpdateView.as_view(), name='update'),
For example, I have a class based view which allows both GET and POST method, as below,
class ViewOne(View):
def post(self, request, *args, **kwargs):
...
def get(self, request, *args, **kwargs):
...
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ViewOne, self).dispatch(*args, **kwargs)
Now, both GET and POST are login_required. But what if I want only POST to be login_required?
Hm... Is it not working?
class ViewOne(View):
#method_decorator(login_required)
def post(self, request, *args, **kwargs):
...
def get(self, request, *args, **kwargs):
...
Why don't create two classes, use also django-braces ;)
class ViewOne(View):
def get(self, request, *args, **kwargs):
...
class ViewTwo(LoginRequiredMixin, ViewOne):
def post(self, request, *args, **kwargs):
...