title: django redirect update to detail view after ajax update
body: <p>I want to redirect to detail view after an update that uses ajax form submission.</p>
  <p><strong>Urls</strong></p>
  <pre><code>path('&lt;uuid:pk&gt;', concept_detail, name='concept_detail'),
  path('&lt;uuid:pk&gt;/update/', update_concept, name='update-concept'),
  </code></pre>
  <p><strong>Detail View</strong></p>
  <pre><code>def concept_detail(request, pk):
    context = {}
  
    context['concept'] = Concept.objects.get(id=pk)
    return render(request, 'concepts/concept_detail.html', context)
  </code></pre>
  <p><strong>Update View</strong></p>
  <pre><code>@login_required
  def update_concept(request, pk):
    if request.method == 'POST':
      try:
        ## I GET THE FORM DATA - I haven't included the querysets because I haven't figured it out yet, just want to get the redirect to work
        print(request.POST)
  
     
        ## THIS DOESN'T WORK, BUT IT WORKS BELOW
        ##concept = Concept.objects.filter(id=pk).first()
        ##return redirect(concept)
  
        
  
      except Exception as e:
        print(e)
       
    else:
      ## this populates the update form
      context = {}
      obj = get_object_or_404(Concept, pk=pk)
      context['concept'] = obj
      if (request.user == obj.user):
        return render(request, 'concepts/concept_update.html', context)
        
      else:
        ## If user didn't create the post but tried to go to update url, redirect to detail page - WORKS
        concept = Concept.objects.filter(id=pk).first()
        
        return redirect(concept)
  </code></pre>
  <p><strong>Response Codes</strong>
  Don't know why I'm getting 302 for update view -- don't get that for my create view, and the forms and ajax are basically the same.</p>
  <pre><code>&quot;POST /concepts/1241955d-1392-4f85-b8ed-e3e0b89b4e50/update/ HTTP/1.1&quot; 302 0
  &quot;GET /concepts/1241955d-1392-4f85-b8ed-e3e0b89b4e50 HTTP/1.1&quot; 200 7577
  </code></pre>
  <p><strong>AJAX FORM SUBMIT</strong>
  The form is a lot more dynamic than this which is why I'm using Ajax.  The data gets sent to server, but the update page stays on the screen and doesn't go to the detail page.</p>
  <pre><code>conceptForm.addEventListener('submit', function(e){
    e.preventDefault();
    var formdata = new FormData();
  
    formdata.append('concept', conceptInput.value)
    formdata.append('definition', definitionInput.value)
  
    $.ajax({
      url: &quot;&quot;,
      method: &quot;POST&quot;,
      data: formdata,
      enctype: 'multipart/form-data',
      processData: false,
      contentType: false,
      success: function(json){
        console.log('SUCCESS')
       
        conceptInput.value = ''
        definitionInput.value = ''      
      },
      error: function(xhr, errmsg, err){
        console.log('ERROR')
        console.log(xhr.status + &quot;: &quot; + xhr.responseText);
      }
    })
    
  })
  </code></pre>
  <p>I use this <a href="https://github.com/realpython/django-form-fun/blob/master/part1/main.js" rel="nofollow noreferrer">code</a> from the <a href="https://realpython.com/django-and-ajax-form-submissions/" rel="nofollow noreferrer">Real Python blog</a> to handle the csrf token creation.</p>
  <p><strong>WORKING NOW</strong></p>
  <pre><code>def update_concept(request, pk):
     is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
  
     if request.method == 'POST' and is_ajax:
        redirect_url = 'http://127.0.0.1:8000/concepts/'+str(pk)
        return JsonResponse({'redirect_url':redirect_url})
  
  
  ## IN Javascript
  
  
  $.ajax({
    success: function(json){
      window.location.replace(json['redirect_url'])
    }
  
  })
   
  
  </code></pre>