Django- why after redirection, display of the form "No"

I have a form after entering information based on the information, it filters the database and performs some calculations, and finally displays the result in the redirected URL.

I really can redirect to another url and successfully display the result. But the problem is that it cannot display any data submitted by the user, just does not show anything for each field, and the result is not based on a set of queries. Say the sum, it just sums all the columns in the database without using the filtered result.

I suspect that the request does not pass the filter result for def get_context_data, so the set of requests in get_context_data does not work.

Thank you very much if you resolve my doubt.

(I made an EDIT version based on the proposal to combine 2 classes, hope someone can fix this EDIT version, thanks)

enter image description here

urls.py

url(r'^result_list/$',ResultView.as_view(),name='result'), url(r'^input/$',InputFormView.as_view(),name='input'), 

views.py

 class InputFormView(request): #class InputFormView(FormView): template_name = 'inputform.html' form_class = InputForm response = HttpResponse( 'result' ) request_form_data = request.POST #you need to sanitize/clear this data response.set_cookie('form_data', request_form_data) #redirect to result page with submitted form information def get_success_url(self): return ''.join( [ reverse('result'), '?company=',self.request.POST.get('company'), <--do I need to change "POST" into "USER"๏ผŸ '&region=',self.request.POST.get('region') ] ) #class ResultView(ListView): class ResultView(request): context_object_name = 'result_list' template_name = 'result_list.html' model = Result def get_context_data(self, **kwargs): context = super(ResultView, self).get_context_data(**kwargs) context["sales"] = self.get_queryset().aggregate(Sum('sales')) context["company"] = self.request.POST.get("company") context["region"] = self.request.POST.get("region") return context def get_queryset(self): if self.request.method == 'POST': form = InputForm(self.request.POST) if form.is_valid(): company = form.cleaned_data['company'] region = form.cleaned_data['region'] queryset=Result.objects.filter(region=region) return queryset return Result.objects.all() if request.COOKIES.has_key('form_data'): value = request.COOKIES['form_data'] #this data also should be sanitized 

HTML

 <div class="basicinfo"> <!--Entry Form information submitted by user--> <table border="1" cellpadding="1"> <tr> <td align="left">Company</td> <td>{{ company }}</td> </tr> <tr> <td align="left">Region</td> <td>{{ region }}</td> </tr> </table> </div> <!--Showing the filtered result in database--> <td><table border="0" cellspacing="10" cellpadding="10"> <tr><b>Sales</b></tr> <td bgcolor="#F0F0F0"> {{ sales.sales__sum }}</td> </tr> <tr><b>Employee</b></tr> <tr> <td bgcolor="#F0F0F0"> {{ employee.employee__sum }}</td> </table> 

EDIT - Combining Class 2 Views

 import urllib #@csrf_exempt class ResultView(ListView): context_object_name = 'result_list' template_name = 'result_list.html' model = Result def get_queryset(self): form = InputForm(self.request.GET) if form.is_valid(): company = form.cleaned_data['company'] region = form.cleaned_data['region'] queryset=Result.objects.filter(region=region) return queryset return Result.objects.all() def get_success_url(self): params = { 'company': self.request.POST.get('company'), 'region': self.request.POST.get('region') } return ''.join([reverse('result'), '?', urllib.urlencode(params.items())]) def get_context_data(self,**kwargs): context = super(ResultView, self).get_context_data(**kwargs) context["sales"] = self.get_queryset().aggregate(Sum('sales')) context["company"] = self.request.GET.get("company") context["region"] = self.request.GET.get("region") return context 

** EDIT- urls.py **

 url(r'^result_list/$',ResultView.as_view(),name='result'),----for the result page url(r'^input/$',result.views.get_success_url,name='input') -----for the form, I am not sure if this line correct or not? 
+5
python django forms
source share
2 answers

Your code should work if you change the get_queryset method to:

 def get_queryset(self): # You are sending GET params here, not POST form = InputForm(self.request.GET) if form.is_valid(): company = form.cleaned_data['company'] region = form.cleaned_data['region'] queryset=Result.objects.filter(region=region) return queryset return Result.objects.all() 

and your get_context_data method:

 def get_context_data(self, **kwargs): context = super(ResultView, self).get_context_data(**kwargs) context["sales"] = self.get_queryset().aggregate(Sum('sales')) # Your variables are in GET, not POST context["company"] = self.request.GET.get("company") context["region"] = self.request.GET.get("region") return context 

However, your code may work with some refactoring. Do you really need a FormView that accepts a POST request? Instead, you can have a form that is sent directly via GET to your result.

With your current approach, you actually process the form twice - once in each of your submissions.

Edit: Also, how you create your redirect URL is unsafe. Instead, you should do something like this:

 import urllib def get_success_url(self): params = { 'company': self.request.POST.get('company'), 'region': self.request.POST.get('region') } return ''.join([reverse('result'), '?', urllib.urlencode(params.items())]) 
+3
source share

If you execute a POST request, and after that you redirect the user, the next request will have an empty POST (since now it is a different request). So this is not an amazing behavior. If you want to save this data between sessions, you can save it in a user session, for example.

You can change some of your views (which I suppose the redirect does) by adding this code:

Cookie Settings:

 def your_view_which_makes_redirect(request): #.. here is your code response = HttpResponse( 'blah' ) request_form_data = request.POST #you need to sanitize/clear this data response.set_cookie('form_data', request_form_data) 

Receiving a cookie:

 def your_view_which_renders_page_after_rediret(request): if request.COOKIES.has_key('form_data'): value = request.COOKIES['form_data'] #this data also should be sanitized 

1) You can also move the name of this cookie to the settings, because now they are hard-coded, and now it is very good practice. Something like the settings .SAVED_FORM_NAME_COOIKE_TOKEN 2) You must also sanitize the data from request.POST and request.COOKIES, since the user can put some malicious data there (SQL injection, etc.).

+1
source share

All Articles