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 8184189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:25:06+00:00 2026-06-07T01:25:06+00:00

I am writing Python Pyramid project and I use jQuery and jquery.form.js. I want

  • 0

I am writing Python Pyramid project and I use jQuery and jquery.form.js. I want my views to handle both HTTP and AJAX posts (I am not sure if it is best idea, but this is probably the stuff for another question here).

Now I use self.request.session.flash() for error notification when HTTP POST was send and error happened.
But when Ajax is posted, I return HTTPOk() or HTTPServerError() response so then I can easy show error message to the user:

$('.ajax_form').ajaxForm({
    error: function(xhr, textStatus, err){
        $('.ajax_messages>.alert').hide().siblings('.alert-error').show().children('p').text(xhr.responseText);
    },
    success: function(responseText, statusText, xhr){
        $('.ajax_messages>.alert').hide().siblings('.alert-success').show().children('p').text(xhr.responseText);
    }
});

But as Loïc Faure-Lacroix suggested I could send dict for both Ajax and HTTP requests and that way simplify the code.

What is better idea? What should I return from view when an exception was thrown?

  • 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-07T01:25:08+00:00Added an answer on June 7, 2026 at 1:25 am

    The idea is to return a dict that contains the values you want in the json response. This dict may be rendered via json or via your template. In an error condition you can set the response status to 500. This works if everything you’re doing is identical (same data needed for json and for html, same response codes). Otherwise your life will be difficult and it’s best to just separate it.

    @action(name='ftp', renderer='templates/ftp.pt')
    @action(xhr=True, renderer='json', name='ftp')
    def ftp_ajax(self):
        log.debug('View: %s', 'ftp (ajax)')
    
        if 'form.submitted' in self.params:
            try:
                self.config.save(self.params)
            except:
                request.response.status = 500
                return {'status': 'error', 'error_msg': 'some error'}
            else:
                return {'status': 'success'}
    
        request.response.status = 400
        return {'status': 'error', 'error_msg': 'invalid request'}
    

    All you have to do is make sure that your template can handle ‘status’ and ‘error_msg’ fields and you’re good to go. Again, this only works well if the template’s requirements are a subset of what you’re returning for json.

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

Sidebar

Related Questions

I'm using Eclipse for writing Python, and I want to be able to easily
I am a PHP programmer, and want to get into writing Python GUI apps
I'm writing python code to parse data from http://www.istockphoto.com/ and it seems like the
I'm writing Python scripts for Blender for a project, but I'm pretty new to
I'm writing Python code. I want to check if numpy and wxpython are installed
I'm writing a python application that will make heavy use of a graph data
I am writing python scripts and execute them in a Makefile. The python script
I have been writing Python code for only a couple of weeks, so I'm
I am writing Python code in Visual Studio 2010 using the excellent Python Tools
I am writing python script which gets links from website. But when I tried

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.