turndjango class based view dispatch ValueError - python

My view class is like this:
class RecoedView(View):
isbn = None
def dispatch(self, request, *args, **kwargs):
self.isbn = '9754654323456'
super(RecordView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return HttpResponse('This is get')
def post(self, request, *args, **kwargs):
return HttpResponse('This is post')
When i run my view with dispatch method overridden i get rhe following error.
ValueError: The view ils.views.RecordView didn't return an HttpResponse object. It returned None instead.
Why am i getting this. Plese make me understand

You need to actually return the result of the super call.
return super(RecordView, self).dispatch(request, *args, **kwargs)

Related

Why can't I access request attribute inside a decorator?

I'm using request.POST.get('...') inside my Django decorator (#save_post_request) whenever my form is submitted, on each tentative I get this same error
(error with request.<anything>):
AttributeError: 'collectData' object has no attribute 'POST'
My decorator is called on top of a post() function inside CollectData classBasedView.
#views.py
class collectData(View):
template_name = 'collect_data.html'
context = {...}
def get(self, request, *args, **kwargs):
...
return render(request, self.template_name, self.context)
#save_post_request
def post(self, request, *args, **kwargs):
...
return redirect(reverse('collectData'))
#decorators.py
def save_post_request(function):
def wrap(request, *args, **kwargs):
title = request.POST.get('title') # <---
...
return function(request, *args, **kwargs)
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
I'm not sure if a decorator can be called like so using classBasedViews, but I think it should be right, what is my mistake?
First argument of inner function should be self:
def save_post_request(function):
def wrap(self, request, *args, **kwargs):
title = request.POST.get('title') # <---
...
return function(self, request, *args, **kwargs)
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap

Django UpdateView, get the current object being edit id?

I am trying to get the current record being edited's id, but am failing thus far. my view is as per the below:
views.py
class EditSite(UpdateView):
model = SiteData
form_class = SiteForm
template_name = "sites/site_form.html"
#method_decorator(user_passes_test(lambda u: u.has_perm('config.edit_subnet')))
def dispatch(self, *args, **kwargs):
self.site_id = self.object.pk
return super(EditSite, self).dispatch(*args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy("sites:site_overview", args=(self.site_id,))
def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs()
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['SiteID']=self.site_id
context['SiteName']=self.location
context['FormType']='Edit'
return context
and the error:
File "/itapp/itapp/sites/views.py" in dispatch
890. self.site_id = self.object.pk
Exception Type: AttributeError at /sites/site/edit/7
Exception Value: 'EditSite' object has no attribute 'object'
ive tried:
self.object.pk
object.pk
self.pk
The view's object attribute is set during get/post by BaseUpdateView:
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)
So it is not yet available in the dispatch method.
But it will be available in the get_success_url and get_context_data methods as those happen after get/post. So you could do:
from django.contrib.auth.mixins import PermissionRequiredMixin
class EditSite(PermissionRequiredMixin, UpdateView):
model = SiteData
form_class = SiteForm
template_name = "sites/site_form.html"
permission_required = 'config.edit_subnet'
def get_success_url(self, **kwargs):
return reverse_lazy("sites:site_overview", args=(self.object.site_id,))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['SiteID']=self.object.site_id
context['SiteName']=self.location # <--- where does self.location come from? self.object.location perhaps?
context['FormType']='Edit'
return context

Django CreateView didn't return an HttpResponse object

Can't figure out why CreateView doesn't return HttpResponse. For now, I use this view just for posting (no GET). I thought that set self.success_url should be enough (as you can see in def post).
class TripCreationView(CreateView):
form_class = TripCreationForm
template_name = 'frontend/homepage.html'
def post(self, request, *args, **kwargs):
self.success_url = request.POST.get('success_url') or reverse('frontend:homepage')
super(TripCreationView, self).post(self, request, *args, **kwargs)
#
# def form_valid(self, form):
# trip = form.save(self.request)
# return HttpResponseRedirect(self.success_url)
def get_form_kwargs(self):
kwargs = super(TripCreationView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
Do you know what to do?
You forgot a return statement.
def post(self, request, *args, **kwargs):
self.success_url = request.POST.get('success_url') or reverse('frontend:homepage')
return super(TripCreationView, self).post(self, request, *args, **kwargs)

Django external api calls

I have a trouble calling external api. This is my view:
class TestView(APIView):
def call_api(self, request, *args, **kwargs):
headers = {}
url = 'http://jsonplaceholder.typicode.com/users/'
method = request.method.lower()
method_map = {
'get': requests.get,
'post': requests.post,
'put': requests.put,
'patch': requests.patch,
'delete': requests.delete
}
return Response(method_map[method](url, headers=headers, data=json.dumps(request.data)).json())
def get(self, request, *args, **kwargs):
return self.call_api(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.call_api(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.call_api(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.call_api(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.call_api(request, *args, **kwargs)
This is my urls.py:
url(r'^test/(?P<pk>[0-9]+)/$', TestView.as_view()),
How can i update my urls and call_api() to get one of users: test/1/ - go to http://jsonplaceholder.typicode.com/users/1, test/2 - go to http://jsonplaceholder.typicode.com/users//2. Now i have all users in that urls.py. Also i need this for all REST requests. Thanks.
Update your call_api to
def call_api(self, request, *args, **kwargs):
headers = {}
url = 'http://jsonplaceholder.typicode.com/users/'+args[0]
# args[0] = pk

How to get keyword arguments passed in Generic views in Django

This is my URL
url(r'^users/(?P<pk>\d+)/testy/$', views.MyModelUpdate.as_view(model=models.User,
form_class=forms.UserForm, myVariable='testing'), name='my_update'),
This is my class view
class MyModelUpdate(UpdateView):
def get(self, request, *args, **kwargs):
How can i get myVariable here
This should do the trick:
def get(self, request, *args, **kwargs):
pk=kwargs['pk']
myVariable=self.kwargs['myVariable']
Edit
How about:
def get(self, request, myVariable, *args, **kwargs):

Categories