Reputation: 314
I'm getting value from user in form like this
#views.py
class DbTotsugoForm(LoginRequiredMixin, FormView):
form_class = DbTotsugoForm
template_name = 'totsugo_app/db_totsugo_chose.html'
# success_url = reverse_lazy('<app-name>:contact-us')
def form_valid(self, form):
file_type = form.cleaned_data['file_type'] # here I have a value from user (book, folder)
return super(DbTotsugoForm, self).form_valid(form)
and I would like to redirect user to list view
- a data from DB based on the value above like this
class MYLook(ListView):
template_name = 'totsugo_app/db_totsugo_list.html'
# So based on `file_type` variable above I want to change `Book` to `Folder` here
queryset = Book.objects.all()
How could I pass there that value, without creating MYLook
two times for both values?
Upvotes: 0
Views: 37
Reputation: 52028
There are two ways you can do that. First, use URL Querystring to send the value from one view to another:
# in DbTotsugoForm view
def form_valid(self, form):
file_type = form.cleaned_data['file_type'] # here I have a value from user (book, folder)
return redirect("{}?fileType={}".format(reverse('<app-name>:contact-us'), file_type))
# in MYLook view
class MYLook(ListView):
# ...
def get_queryset(self, *args, **kwargs):
file_type = self.request.GET.get('fileType', None)
if file_type == 'Book':
return Book.objects.all()
return Folder.objects.all()
(Alternative Solution) you can use request.session
to store the data. For example:
# in DbTotsugoForm view
def form_valid(self, form):
file_type = form.cleaned_data['file_type'] # here I have a value from user (book, folder)
self.request.session['file_type'] = file_type
return super(DbTotsugoForm, self).form_valid(form)
# in MYLook view
class MYLook(ListView):
# ...
def get_queryset(self, *args, **kwargs):
file_type = self.request.session.pop('file_type', None)
if file_type == 'Book':
return Book.objects.all()
return Folder.objects.all()
FYI, you are using same name DbTotsugoForm
for the form and view, you should change the name of the view to something else, ie DbTotsugoView
.
Upvotes: 1