Django and jQuery AJAX
This is probably my first post for Django. I have only been using it for a few weeks and I am already falling in love with it.
Here is how to do an AJAX “save” using jQuery and Django.
My Javascript would look something like this:
$(document).ready(function() { $("#save_button").click(function(){ if(save_data()) { alert('Saved!'); } else { alert('Failed!'); } }); function save_data() { $.post("/business/add/", { csrfmiddlewaretoken: $("input[name='id_csrfmiddlewaretoken").val(), name: $("#id_name").val(), website: $("#id_website").val() }, function(data) { data = json_parse(data); //You must get this file: http://www.JSON.org/json_parse.js if(data.status=="success") { return true; } else { console.log("status: "+data.status) console.log("error: "+data.error) console.log("POST DATA: "+data.data) $.each(data.data, function(i, n){ console.log(">"+i+": "+n); }); return false; } }); } });
And my views.py is like this:
@login_required @transaction.commit_manually def business_add(request): success = False error = None if request.method == 'POST': business_form = BusinessForm(request.POST) if (business_form.is_valid()): business_name = business_form.cleaned_data['name'] business_website = business_form.cleaned_data['website'] try: business = Business(name=business_name,website=business_website) business.save() except IntegrityError, e: transaction.rollback() success = False error = e else: transaction.commit() success = True else: business_form = BusinessForm() data = { "business_form": business_form, "success": success, "error": error } if(success): if request.is_ajax(): results = {"status":"success", "message":"Business saved."} data = json.dumps(results) return HttpResponse(data) else: return render_to_response("business/business_add.html", data, context_instance=RequestContext(request)) else: if request.is_ajax(): data = json.dumps({"status":"failed", "error":error, "data":request.POST}) return HttpResponse(data) else: return render_to_response("business/business_add.html", data, context_instance=RequestContext(request))
And last but not the least, my template file:
More HTML here...
<div id="add_business_container" title="Add a business">
<form id="add_business_form">
{% csrf_token %}
<h2>Business info</h2>
{{ business_form.as_p }}
<input id="save_button" class="submit" type="submit" value="SAVE"/>
</form>
</div>
More HTML here...It is basically just like \doing it in any web framework (Zend Framework). But with Django, everything is so easy to understand — you have built-in methods like request.is_ajax()
WordPress: How to enable widgets in your custom theme
This is probably in the WordPress documentation but to enable widgets in your own customized theme, do this:
... rest of PHP and HTML code in your here ...
<div id="sidebar">
<ul>
<?php if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>
<li> id="gray_box">
<?php get_search_form(); ?>
</li>
<?php endif; ?>
</ul>
</div>After doing that, go to your WP-Admin page and drag-and-drop widgets. The widgets page is found in Appearance > Widgets .
My notes in Symfony
Right now, I am working on a pet-project using Symfony.
Symfony is a full-stack framework, a library of cohesive classes written in PHP.
It provides an architecture, components and tools for developers to build complex web applications faster. Choosing symfony allows you to release your applications earlier, host and scale them without problem, and maintain them over time with no surprise.
Symfony is based on experience. It does not reinvent the wheel: it uses most of the best practices of web development and integrates some great third-party libraries.
I have spent a few hours today reading their tutorials and books. The learning curve isn’t that hard if you know Zend Framework or Code Igniter. That is why I made notes in my Wiki — containing the important stuff that will guide me while I am building my application.
It has a robust CLI commands to build and reload your database. Using doctrine needs a little getting used to but the documentation for it is really good. If all goes well, I plan to use Symfony on my succeeding applications.
My notes can be found here: http://wiki.ekini.net/main/Symfony
How to create a sub-domain in Linode
Open the DNS Manager in Linode
- Login to Linode Members Area
- Go to the DNS Manager tab.
- Click the domain name you want to put the sub-domain to.
- Scroll to the middle of the page and look for the A/AAAA Records.
- Click on Add a new A/AAAA Record
- In the Hostname input box, input the sub-domain you want. For example, if you want blog.mydomain.com, enter “blog” in the input box.
- For the IP Address, input the IP address of your server.
- Click Save.
SSH to your server
- SSH to your server.
- Once you are logged in as root, create a user called “shop” (same as your sub-domain name).
- Go to: /etc/apache2/sites-available OR enter this command:
cd /etc/apache2/sites-available
- If you do an
ls -la
you should see something like this:
-rw-r--r-- 1 root root 385 Apr 24 02:21 default -rw-r--r-- 1 root root 7364 Mar 9 21:19 default-ssl
- Copy the default file into your new sub-domain file. You can issue this command:
cp default shop.mydomain.com
- Open up shop.mydomain.com in VI.
- Make the new file look like this:
ServerAdmin admin@mydomain.com ServerName shop.mydomain.com ServerAlias www.mydomain.com DocumentRoot /home/shop/public_html/ ErrorLog /home/shop/logs/error.log CustomLog /home/shop/logs/access.log combined - The next obvious thing to do is to create the directory structure found above. So just do something like this:
mkdir /home/shop/public_html/
- Enable the site by issuing this command:
a2ensite shop.mydomain.com
- Then refresh Apache by using this command:
/etc/init.d/apache2 reload
That’s about it.
FYI: My Linode is using Ubuntu 9.10.
OpenX Ad Server: Beginner’s Guide from Packt Publising
I have not yet considered seriously running ads on my site so I have never heard of OpenX Ad Server before Packt Publishing sent me a free PDF copy of OpenX Ad Server (link here).
What is OpenX Ad Server? OpenX is a platform where you can manage your ads across different websites. It is written in PHP and uses MySQL. The book from Packt Publishing was written by Murat Yilmaz. He is from Russia and obviously has a lot of experience. He is a software developer and runs his own blog and online advertising network.
The book he has written is very detailed. What I mean by details is that a lot of screenshots are provided to guide the reader. Anyone who knows how to create a website will understand the book by scanning the pages. It is a very good guide if you want to get your hands dirty right away with OpenX Ad Server. The author even included screenshots for the installation. I have been doing some tutorials over the past few years, and I tell you, that is a lot of work. The author even goes through an extra step by giving an explaination at the end of each topic.
The book is for beginners, so you won’t find advanced topics. But it is good enough to introduce the new comers to the Ad serving community. It also includes chapters on how to use OpenX with WordPress.
In the end, the book convinced me to look into the “Ad Serving” part of the internet. I have dealt with other systems and web applications but none of this kind. So if you own a lot of websites and want to run advertisements, I suggest that you get a copy of this book. It will probably make your life easier.
You can find out more about this book by clicking HERE. And yes, that is an affiliate link. So if you are going to buy the book, use the link. ;)

