1👍
✅
as_view()
of a class based view isn’t supposed to return an http response, but a callable function…
If you want to send out a response for a GET
request, add a get
method to you view class:
class MyView(View):
def get(request):
# return your http response here
If you want to browse Django’s class based views a bit, here’s a nice documentation!
0👍
why don’t you use Django FormView? This is the documentation.
from django.views.generic.edit import FormView
class MyFormView(FormView):
form_class = myForm
template_name = 'my_template.html'
success_url = '/thanks/'
def get_context_data(self, **kwargs):
#This is you GET
return super(MyFormView. self).get_context_data(**kwargs)
def form_valid(self, form):
#This is after the post, when the form is valid
return super(MyFormView, self).form_valid(form)
def form_invalid(self, form):
#This is after the post, when the form is invalid
return super(MyFormView, self).form_invalid(form)
You can play with get_succes_url()
method to redirect so somewhere.
I Hope that helps.
- [Answer]-Formatting date and time: in view (template) or in controller?
- [Answer]-Memory consumption grows
- [Answer]-Django manytomany field in admin – how to add a default list?
- [Answer]-Can i do this in django? admin with pages as models vs cms?
- [Answer]-Django : NoReverseMatch error when reverse a url
Source:stackexchange.com