Django view not rendering after Ajax redirect












1















The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure why.



urls.py



url(r'ajax_filter/', views.ajax_filter, name='ajax_filter'),
url(r'filter=(w+)/$', views.filtered_index, name='filtered_index'),


views.py



def filtered_index(request, filter):
clothes = Clothes_Item.objects.filter(gender=filter)
if request.user.is_authenticated():
favorite_clothes_ids = get_favorite_clothes_ids(request)
return render(request, 'test.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
else:
return render(request, 'test.html', {'clothes': clothes, })

def ajax_filter(request):
if request.is_ajax():
gender_filter = request.GET.get('gender_filter') #filter type
if gender_filter is not None:
return HttpResponseRedirect(reverse('filtered_index', args=[gender_filter]))
return HttpResponse('')









share|improve this question



























    1















    The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure why.



    urls.py



    url(r'ajax_filter/', views.ajax_filter, name='ajax_filter'),
    url(r'filter=(w+)/$', views.filtered_index, name='filtered_index'),


    views.py



    def filtered_index(request, filter):
    clothes = Clothes_Item.objects.filter(gender=filter)
    if request.user.is_authenticated():
    favorite_clothes_ids = get_favorite_clothes_ids(request)
    return render(request, 'test.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
    else:
    return render(request, 'test.html', {'clothes': clothes, })

    def ajax_filter(request):
    if request.is_ajax():
    gender_filter = request.GET.get('gender_filter') #filter type
    if gender_filter is not None:
    return HttpResponseRedirect(reverse('filtered_index', args=[gender_filter]))
    return HttpResponse('')









    share|improve this question

























      1












      1








      1








      The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure why.



      urls.py



      url(r'ajax_filter/', views.ajax_filter, name='ajax_filter'),
      url(r'filter=(w+)/$', views.filtered_index, name='filtered_index'),


      views.py



      def filtered_index(request, filter):
      clothes = Clothes_Item.objects.filter(gender=filter)
      if request.user.is_authenticated():
      favorite_clothes_ids = get_favorite_clothes_ids(request)
      return render(request, 'test.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
      else:
      return render(request, 'test.html', {'clothes': clothes, })

      def ajax_filter(request):
      if request.is_ajax():
      gender_filter = request.GET.get('gender_filter') #filter type
      if gender_filter is not None:
      return HttpResponseRedirect(reverse('filtered_index', args=[gender_filter]))
      return HttpResponse('')









      share|improve this question














      The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure why.



      urls.py



      url(r'ajax_filter/', views.ajax_filter, name='ajax_filter'),
      url(r'filter=(w+)/$', views.filtered_index, name='filtered_index'),


      views.py



      def filtered_index(request, filter):
      clothes = Clothes_Item.objects.filter(gender=filter)
      if request.user.is_authenticated():
      favorite_clothes_ids = get_favorite_clothes_ids(request)
      return render(request, 'test.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
      else:
      return render(request, 'test.html', {'clothes': clothes, })

      def ajax_filter(request):
      if request.is_ajax():
      gender_filter = request.GET.get('gender_filter') #filter type
      if gender_filter is not None:
      return HttpResponseRedirect(reverse('filtered_index', args=[gender_filter]))
      return HttpResponse('')






      python ajax django redirect django-views






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 21 '16 at 22:54









      Bryce MorrowBryce Morrow

      417




      417
























          1 Answer
          1






          active

          oldest

          votes


















          10














          You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.



          $.ajax({
          // You send your request here
          }).done(function(data) {
          // You can handle your redirection here
          });




          Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:



          from django.http import JsonResponse

          def ajax_filter(request):
          if request.is_ajax():
          gender_filter = request.GET.get('gender_filter') #filter type
          if gender_filter is not None:
          return JsonResponse({
          'success': True,
          'url': reverse('filtered_index', args=[gender_filter]),
          })
          return JsonResponse({ 'success': False })


          In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:



          $.ajax({
          // You send your request here
          }).done(function (data) {
          if (data.success) {
          window.location.href = data.url;
          }
          });





          share|improve this answer


























          • How should I handle the redirection in the second part of the code you wrote above?

            – Bryce Morrow
            Dec 21 '16 at 23:37











          • Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

            – 2ps
            Dec 21 '16 at 23:55











          • As @2ps mentioned, you can use window.location.href = your_url.

            – ettanany
            Dec 21 '16 at 23:59











          • @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

            – HamzDiou
            Nov 21 '18 at 9:31











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41273295%2fdjango-view-not-rendering-after-ajax-redirect%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          10














          You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.



          $.ajax({
          // You send your request here
          }).done(function(data) {
          // You can handle your redirection here
          });




          Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:



          from django.http import JsonResponse

          def ajax_filter(request):
          if request.is_ajax():
          gender_filter = request.GET.get('gender_filter') #filter type
          if gender_filter is not None:
          return JsonResponse({
          'success': True,
          'url': reverse('filtered_index', args=[gender_filter]),
          })
          return JsonResponse({ 'success': False })


          In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:



          $.ajax({
          // You send your request here
          }).done(function (data) {
          if (data.success) {
          window.location.href = data.url;
          }
          });





          share|improve this answer


























          • How should I handle the redirection in the second part of the code you wrote above?

            – Bryce Morrow
            Dec 21 '16 at 23:37











          • Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

            – 2ps
            Dec 21 '16 at 23:55











          • As @2ps mentioned, you can use window.location.href = your_url.

            – ettanany
            Dec 21 '16 at 23:59











          • @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

            – HamzDiou
            Nov 21 '18 at 9:31
















          10














          You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.



          $.ajax({
          // You send your request here
          }).done(function(data) {
          // You can handle your redirection here
          });




          Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:



          from django.http import JsonResponse

          def ajax_filter(request):
          if request.is_ajax():
          gender_filter = request.GET.get('gender_filter') #filter type
          if gender_filter is not None:
          return JsonResponse({
          'success': True,
          'url': reverse('filtered_index', args=[gender_filter]),
          })
          return JsonResponse({ 'success': False })


          In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:



          $.ajax({
          // You send your request here
          }).done(function (data) {
          if (data.success) {
          window.location.href = data.url;
          }
          });





          share|improve this answer


























          • How should I handle the redirection in the second part of the code you wrote above?

            – Bryce Morrow
            Dec 21 '16 at 23:37











          • Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

            – 2ps
            Dec 21 '16 at 23:55











          • As @2ps mentioned, you can use window.location.href = your_url.

            – ettanany
            Dec 21 '16 at 23:59











          • @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

            – HamzDiou
            Nov 21 '18 at 9:31














          10












          10








          10







          You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.



          $.ajax({
          // You send your request here
          }).done(function(data) {
          // You can handle your redirection here
          });




          Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:



          from django.http import JsonResponse

          def ajax_filter(request):
          if request.is_ajax():
          gender_filter = request.GET.get('gender_filter') #filter type
          if gender_filter is not None:
          return JsonResponse({
          'success': True,
          'url': reverse('filtered_index', args=[gender_filter]),
          })
          return JsonResponse({ 'success': False })


          In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:



          $.ajax({
          // You send your request here
          }).done(function (data) {
          if (data.success) {
          window.location.href = data.url;
          }
          });





          share|improve this answer















          You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.



          $.ajax({
          // You send your request here
          }).done(function(data) {
          // You can handle your redirection here
          });




          Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:



          from django.http import JsonResponse

          def ajax_filter(request):
          if request.is_ajax():
          gender_filter = request.GET.get('gender_filter') #filter type
          if gender_filter is not None:
          return JsonResponse({
          'success': True,
          'url': reverse('filtered_index', args=[gender_filter]),
          })
          return JsonResponse({ 'success': False })


          In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:



          $.ajax({
          // You send your request here
          }).done(function (data) {
          if (data.success) {
          window.location.href = data.url;
          }
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 22 '16 at 0:11

























          answered Dec 21 '16 at 22:57









          ettananyettanany

          9,87932345




          9,87932345













          • How should I handle the redirection in the second part of the code you wrote above?

            – Bryce Morrow
            Dec 21 '16 at 23:37











          • Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

            – 2ps
            Dec 21 '16 at 23:55











          • As @2ps mentioned, you can use window.location.href = your_url.

            – ettanany
            Dec 21 '16 at 23:59











          • @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

            – HamzDiou
            Nov 21 '18 at 9:31



















          • How should I handle the redirection in the second part of the code you wrote above?

            – Bryce Morrow
            Dec 21 '16 at 23:37











          • Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

            – 2ps
            Dec 21 '16 at 23:55











          • As @2ps mentioned, you can use window.location.href = your_url.

            – ettanany
            Dec 21 '16 at 23:59











          • @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

            – HamzDiou
            Nov 21 '18 at 9:31

















          How should I handle the redirection in the second part of the code you wrote above?

          – Bryce Morrow
          Dec 21 '16 at 23:37





          How should I handle the redirection in the second part of the code you wrote above?

          – Bryce Morrow
          Dec 21 '16 at 23:37













          Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

          – 2ps
          Dec 21 '16 at 23:55





          Return a JsonResponse in your view and return the next URL that you want to redirect to. In your JavaScript, use window.location.href to do the redirection on the client.

          – 2ps
          Dec 21 '16 at 23:55













          As @2ps mentioned, you can use window.location.href = your_url.

          – ettanany
          Dec 21 '16 at 23:59





          As @2ps mentioned, you can use window.location.href = your_url.

          – ettanany
          Dec 21 '16 at 23:59













          @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

          – HamzDiou
          Nov 21 '18 at 9:31





          @aettanany : thanks for your code, well working. What about adding a context ? If I want to show something new in the template (refreshing the initial page). I tried passing a context and request through HttpRedirect but it's not Json Serializable. Any Idea ?

          – HamzDiou
          Nov 21 '18 at 9:31




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41273295%2fdjango-view-not-rendering-after-ajax-redirect%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?