Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:09:26+00:00 2026-06-13T03:09:26+00:00

I have an Edit Form page which edits existing posts. i wanted to put

  • 0

I have an Edit Form page which edits existing posts. i wanted to put a Preview link into this page. what i want is ; when i click preview link it must ;

  1. take form element’s current values and write them into my
    Post_Preview Model.
  2. then , ajax function must redirect to my *show_preview* page which i defined in views and urls.
  3. and in show_preview page (preview.html) it must be rendered. (form’s
    current values)

this is how it must works. And here are my codes :

and my edit-form page:

<form method="post" action="">
    {% csrf_token %}
    <input type="hidden" id="post_owner" value="{{ post.owner }}"/></td>
    <input type="hidden" id="post_id" value="{{ post.id }}"> </td>

            {{ form.title }}
            {{ form.body }}

    <a href="#" id="preview">Preview</a>
    <input type="submit" value="Save" style="cursor: pointer;"/>
</form>

ajax Function:

$(function(){

    $('#preview').click(function() {

        var title = $('#id_title').val();
        var body = $('#id_body').val();
        var owner = $('#post_owner').val(); //hidden value at form page
        var id = $('#post_id').val();  //hidden value at form page

     var ajaxOptions = {
         type:'post',
         url : '/admin/post/save_preview/', //save_preview's url
         data : {
             'title' : title,
             'body'  : body,
             'owner' : owner,
             'id'    : id
         },
         success: function(){
             window.open("/blog/"+owner+"/preview/"+id); //show_preview's url
         },
         error: function(){
            alert('There is an Error'); //this is what i see when click preview link.
         }
     };
       $.ajax(ajaxOptions);
});
});

my save_preview view:

def save_preview(request):
    title = request.POST['title']
    body = request.POST['body']
    owner = request.POST['owner']
    post_id = request.POST['id']
    try:
        preview = Post_Preview(id=post_id, title=title, body=body, owner=owner)
        preview.save()
    except:
        pass
    return HttpResponse(200)

my show_preview view:

def show_preview(request,post_id,username):
    preview = Post_Preview.objects.get(id=post_id)

    return render_to_response('preview.html',{'post': preview}, context_instance=RequestContext(request))

my related url lines:

url(r'^admin/post/save_preview/', view='save_preview' ,name='save_preview'),
url(r'^blog/(?P<username>[-\w]+)/preview/(?P<post_id>\d+)', view='show_preview', name='show_preview'),

When i click Preview link at edit-form page : it shows ‘There is an Error’ error; which is defined in ajax function.

thank you!

edit : there is more code before my ajax function in js file (which are related to django-ajax relation ):

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function sameOrigin(url) {
    // test that a given url is a same-origin URL
    // url could be relative or scheme relative or absolute
    var host = document.location.host; // host + port
    var protocol = document.location.protocol;
    var sr_origin = '//' + host;
    var origin = protocol + sr_origin;
    // Allow absolute or scheme relative URLs to same origin
    return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
        (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
        // or any other URL that isn't scheme relative or absolute i.e relative.
        !(/^(\/\/|http:|https:).*/.test(url));
}
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
            // Send the token to same-origin, relative URLs only.
            // Send the token only if the method warrants CSRF protection
            // Using the CSRFToken value acquired earlier
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

edit 2: when i add try/except blocks for save_preview ; i didnt throw popup error page. it redirected to save_preview page and threw and error :

Post_Preview matching query does not exist.

156. preview = Post_Preview.objects.get(id=post_id)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:09:28+00:00Added an answer on June 13, 2026 at 3:09 am

    You’re not sending the csrf token in $.ajax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the scenario: I have multiple users using a PHP page/form to edit
I have this function to edit all fields that come from the form and
I have a page which displays a form that a logged-in user can use
I have a modal which pops out a form, allowing an admin to edit/update
I have a form on my page which gives the user the possibility to
I have a page which has a small form with one input field and
Issue: Hello, I have an edit page which enables the client to update the
I have page in which I edit some entity. That page has two command
I have one Asp.net MVC3 page which renders details of a Mobile. This page
I'm trying to single source a form page which can be in edit mode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.