TurboGears Noteshttps://tglogs.wordpress.com
A blog on TurboGears and Python web developmentFri, 16 Mar 2018 13:35:38 +0000enhourly1http://wordpress.com/https://s2.wp.com/i/buttonw-com.pngTurboGears Noteshttps://tglogs.wordpress.com
TurboGears and AJAXhttps://tglogs.wordpress.com/2012/04/13/turbogears-and-ajax/
https://tglogs.wordpress.com/2012/04/13/turbogears-and-ajax/#respondFri, 13 Apr 2012 20:14:41 +0000http://tglogs.wordpress.com/?p=98Continue reading →]]>Many beginner web developers ask questions like “how do i do something without refreshing page?” or “how can i load another page in my page?” etc. The answer is AJAX. AJAX stands for “Asynchronous JavaScript and XML”. With AJAX, you can retrieve or send data to the server in the background using an “XMLHttpRequest.”

Before going further, i would like to take your time and advise you to learn JavaScript. If you are a web developer, no matter if you are back-end or front-end one, at some point in your career you will have to use JavaScript. In order to be ready for those times, go and learn JavasSript. There are awesome resources.

Do not be one of those people who think learning jQuery is enough and how you don’t need to know JavaScript. jQuery is a JS framework. It’s a great one and that’s all. Learn JavaScript.

There are many techniques to use AJAX in a web application. In these examples we are going to use jQuery framework and it’s powerful “$.get” and “$.post” methods.

Open up your terminal, cd into your TurboGears environment, activate it, upgrade it to the most recent version which is 2.1.5 with:

We create a “div” element with the class “tasks” to use in our JavaScript.

We include jQuery framework via Google CDN.

So whenever we click to a project’s link, its tasks will be listed under the “tasks” div.

Let me walk you through the simple JavaScript codes we wrote.

In the first line, we tell jQuery “when the DOM is ready”, use all these child scripts of yours. So it attaches the event listener, changes css classes, anything we want to do.

In the second line, we tell, whenever an “a” element with a “project_link” class is clicked, create an AJAX request and append the result to the closest div with the class “tasks” to the “a” element’s parent.

In JavaScript, whenever a new scope is opened like the second line, the keyword “this” changes meaning. In line 3 of our script, it references to the clicked “a” element however in the line 5 it references the response object. This is why I have created the “context” variable and mapped it to “this” which is the clicked “a” element. This way, I am able to use the clicked a element.

We are missing two things here. The corresponding TurboGears action and it’s template. So let’s create them. In your RootController, add the following action:

And the following goes by “show_tasks.mak” under the “templates” directory.

% if tasks:
% for task in tasks:
<div>${task.task_name}</div>
% endfor
% else:
<div>This project does not have any tasks, yet.</div>
% endif

That’s all. Run your server and open it up in your browser. Let’s see how it goes.

In the next post, I will show you how to use the $.post method and deal with JSON responses from the server when using AJAX. I hope you guys enjoyed all these. Please do not hesitate to ask any question. You are more than welcome.

]]>https://tglogs.wordpress.com/2012/04/13/turbogears-and-ajax/feed/0mengukaganTroll CatQuerying The Modelshttps://tglogs.wordpress.com/2012/03/28/querying-the-models/
https://tglogs.wordpress.com/2012/03/28/querying-the-models/#respondWed, 28 Mar 2012 22:02:33 +0000http://tglogs.wordpress.com/?p=81Continue reading →]]>If you remember the post that we created the models in, you will also remember that TurboGears uses SQLAlchemy for the model part of MVC.

Now, we are going to do some practice on querying the models. In the meanwhile, you will discover the TurboGears console. Let’s get into our project’s home dir and start the console.

]]>https://tglogs.wordpress.com/2012/03/28/querying-the-models/feed/0mengukaganA Simple Project Management Tool With TurboGears – Templateshttps://tglogs.wordpress.com/2012/03/28/a-simple-project-management-tool-with-turbogears-templates/
https://tglogs.wordpress.com/2012/03/28/a-simple-project-management-tool-with-turbogears-templates/#commentsWed, 28 Mar 2012 18:07:03 +0000http://tglogs.wordpress.com/?p=70Continue reading →]]>We have created our models, created our controller actions and forms. Now we are going to set up our templates. Let’s take a look at the controller action “new_project”:

In this action, we tell TurboGears that when new_project action is called, we are going to render it with “new_project.mak” file in the “templates” folder. We also pass “project_form” variable to the template.

It is very easy to create forms with TurboGears. In this example we are going to use ToscaWidgets 1 however keep in mind that in the next major TurboGears release, TurboGears will switch to ToscaWidgets 2.

HTML forms consist of a form element and its child elements called input. In these examples, we are going to create forms in Pytho thon with the help of ToscaWidgets library and render these forms in our templates.

1) We have created two classes called ProjectForm and TaskForm inheriting ToscaWidgets’ TableForm class. When we call the project_form and task_form in our templates, it will render the form elements in a table layout.

2) In the ProjectForm, we have a text field (<input type="text">) and a textarea (<textarea>) field.

3) In the TaskForm, we have a dropdown box with the options of all our Projects, a text input for the task name and a textarea field for the task description.

]]>https://tglogs.wordpress.com/2012/03/27/a-simple-project-management-tool-with-turbogears-setting-up-the-forms/feed/0mengukaganA Simple Project Management Tool With TurboGears – Setting Up the Modelshttps://tglogs.wordpress.com/2012/03/25/a-simple-project-management-tool-with-turbogears-setting-up-the-models/
https://tglogs.wordpress.com/2012/03/25/a-simple-project-management-tool-with-turbogears-setting-up-the-models/#commentsSun, 25 Mar 2012 03:25:16 +0000http://tglogs.wordpress.com/?p=27Continue reading →]]>I want you to love TurboGears, understand how to use TurboGears and how to become fully productive with it. For example, I can develop a fully fledged application that would take 3 weeks with something else in 1-1.5 weeks with TurboGears. For that, I think a project management tool fits best here. It requires working with a database, creating forms, authentication and authorization and so on.

Now, go ahead and create a project called “ideas”. What we are going to do is a simple, a very simple project management tool. We are going to create a Project model, a Task model.

What have we done?
We have created two models, 1) Project and 2) Task. We have imported the necessary modules from both our project and SQLAlchemy in order to be able to setup our models correctly. The very next step is to create the tables we have declared in the models. Go and open ideas/model/__init__.py file. Go to the end of file. (Here is a “vim” trick for you. When you press ESC and then type “:$”, you will go to the end of file.) Add the following line there:

“paster setup-app development.ini” will create all our tables, including the authentication tables. It will use the database backend defined in the development.ini file, line: 52. By default, it is SQLite.

In the next post, we will create the forms. Until then, take care and check out ToscaWidgets.

]]>https://tglogs.wordpress.com/2012/03/25/a-simple-project-management-tool-with-turbogears-setting-up-the-models/feed/2mengukaganFirst Steps With TurboGearshttps://tglogs.wordpress.com/2012/03/20/first-steps-with-turbogears/
https://tglogs.wordpress.com/2012/03/20/first-steps-with-turbogears/#commentsTue, 20 Mar 2012 23:24:42 +0000http://tglogs.wordpress.com/?p=20Continue reading →]]>Now that you have learnt what is TurboGears and how to install it, you want to get your hands dirty with it. This is really nice. I’m glad you’ve decided to do that. You won’t regret this decision. TurboGears is like that guitar you have always wanted to buy. Now that you have it, you can start to rock.

You may get errors depending on your system and your system’s requirements. If you have not seen any errors, congratulations, you have installed TurboGears 2.1.4 (the current stable release as of this writing) and its dependencies successfully.

The pip way

You have tried to install TurboGears with the standard way however you are curious. How to install with pip as well? Fear not, young TGer. I will feed your hunger.

The purpose of this blog is to explore TurboGears from a beginner’s point of view. We will be posting on TurboGears until we feel it’s enough.

TurboGears is a powerful, full stack MVC web development framework. It comes with its own decisions, yet it is flexible enough to choose your own tools. What TurboGears has for you?

SQLAlchemy (PostgreSQL, MySQL, SQLite, etc.)

Ming ORM for MongoDB

Authentication and authorization

Jinja2, Mako or Genshi templating

Administration interface

Easy form generation

Object dispatch

Easy session management

Easy cache management

TurboGears is fast enough, if you wonder about the performance. We had deployed a TurboGears application that had ~2K/sec connections. The setup also had nginx and uWSGI but this is another subject in this blog’s roadmap.

You can get very productive with TurboGears quickly. Using TurboGears is very easy. TurboGears has a decent documentation that will be better in the near future. When you get stuck you can ask questions in the TurboGears mailing list or the IRC channel on Freenode network.