<?xml version="1.0" encoding="UTF-8" standalone="no"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2967116530949647538</atom:id><lastBuildDate>Sat, 09 Aug 2025 07:27:25 +0000</lastBuildDate><title>The Django Blog</title><description></description><link>http://thedjangoblog.blogspot.com/</link><managingEditor>noreply@blogger.com (Tejas Sathe)</managingEditor><generator>Blogger</generator><openSearch:totalResults>33</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle/><itunes:category text="Technology"><itunes:category text="Software How-To"/></itunes:category><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-5385858892737152218</guid><pubDate>Mon, 31 Aug 2015 03:22:00 +0000</pubDate><atom:updated>2015-08-31T10:58:13.414-07:00</atom:updated><title>How to make one view do the work of two</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey folks! Today's is an interesting topic: making one view do two functions. In fact, you can make it do more than two; you an make them do 8 at a time. Think of it as view overloading, if you're into that kind of thing.&lt;br&gt;
So let's dive into it.&lt;br&gt;
&lt;br&gt;
Take logging into your account for example. Initially what I used to do is split this process into three views: one for displaying the login page, one for verification of credentials, and a third for displaying the profile. With the new method I will outline in this post you can do that using only two views rather than three. We'll combine displaying the login page and credential verification into one view. The way well do that will use HTTP request methods. Let's see what those are (going to take a little help from TutorialsPoint for the exact definitions):&lt;br&gt;
&lt;br&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;b&gt;GET&lt;/b&gt;: This method is used to extract data that is embedded in the URL. The data is identified by a '?'. For example, in the URL "http://some.url/?data1=value1&amp;amp;data2=value2" the data after the '?' can be translated as {'data1':'value1', 'data2':'value2'}&lt;/li&gt;
&lt;b&gt;&lt;/b&gt;
&lt;li&gt;&lt;b&gt;HEAD&lt;/b&gt;: This one is the same as GET, but this one only transfers status line and header section&lt;/li&gt;
&lt;li&gt;&lt;b&gt;POST&lt;/b&gt;: This method sends data directly to the server rather than embedding it in the URL. It is used for handling sensitive data like passwords and credit card numbers. That way, when a user enters it in the browser, it cannot be seen by someone standing behind him&lt;/li&gt;
&lt;li&gt;&lt;b&gt;PUT&lt;/b&gt;: This is an upload based method. The representations of target resource are replaced with uploaded content&lt;/li&gt;
&lt;li&gt;&lt;b&gt;DELETE&lt;/b&gt;: As the name indicates, this method deletes all representations of the target resource&lt;/li&gt;
&lt;li&gt;&lt;b&gt;CONNECT&lt;/b&gt;: Establishes a connection with the server&lt;/li&gt;
&lt;li&gt;&lt;b&gt;OPTIONS&lt;/b&gt;: Describes communication options for target resource&lt;/li&gt;
&lt;li&gt;&lt;b&gt;TRACE&lt;/b&gt;: Performs a loopback test on connection tunnel&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;Now each webpage can be accessed using one of those methods. you can overload a view by using an &lt;b&gt;if &lt;/b&gt;&amp;nbsp;block to see what access method is used and do a different thing based on that.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;def login(request):&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;&amp;nbsp; &amp;nbsp; if request.method == "GET":&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return render(request, "login.html")&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;&amp;nbsp; &amp;nbsp; elif request.method == "POST":&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; # code to authenticate user&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;&lt;br&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;As you can see, in this case, if the request method is &lt;b&gt;GET&lt;/b&gt;, which it is by default, it'll just render the login page. Normally, in the login form the action will be &lt;b&gt;POST&lt;/b&gt;&amp;nbsp;since we'll be asking for passwords. So the first page will be rendered due to the &lt;b&gt;GET &lt;/b&gt;method and the verification will be done by the &lt;b&gt;POST &lt;/b&gt;method. So this way you can overload views.&lt;/font&gt;&lt;/div&gt;&lt;div&gt;&lt;font face="Helvetica Neue Light, HelveticaNeue-Light, helvetica, arial, sans-serif"&gt;That's all for today folks!&lt;/font&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/08/how-to-make-one-view-do-work-of-two.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-8243693116526867676</guid><pubDate>Thu, 16 Jul 2015 15:58:00 +0000</pubDate><atom:updated>2015-07-16T08:58:59.373-07:00</atom:updated><title>How to access your Django site from a different computer</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! Today we'll see how to access your Django site from another computer. Now this won't be a problem if you've hosted on a remote server like PythonAnywhere or Heroku. But what if you have hosted it on your local machine and want to provide people with access to it?&lt;br /&gt;
&lt;br /&gt;
I'm developing an online mentorship platform for my college that is hosted on the college server. So for that I had to use this concept. Before you do this, however, here's a disclaimer: &lt;i&gt;only computers connected in the same network as your local machine can access your Django site.&lt;/i&gt;&amp;nbsp;You can, of course, provide global access, but there's a lot of complicated networking stuff involved in that. Let's not go into that right now.&lt;br /&gt;
&lt;br /&gt;
So let's get on with it. You know the command for hosting and accessing the site on your local machine is &lt;b&gt;python manage.py runserver&lt;/b&gt;. This essentially gives your local machine server capabilities and lets you access it from the same machine. However, to let other machines access it, here's the command:&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;python manage.py runserver &amp;lt;ip_address&amp;gt;:&amp;lt;port_number&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Replace &lt;b&gt;&amp;lt;ip_address&amp;gt; &lt;/b&gt;with your machine's IP address. If you don't know the IP address, do one of the following:&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Windows: Open command prompt and type &lt;b&gt;ipconfig&lt;/b&gt;. There'll be a field that reads &lt;b&gt;IPv4 Address&lt;/b&gt;. That's the address you should put in your runserver command.&lt;/li&gt;
&lt;li&gt;Linux/Mac: Open terminal and type &lt;b&gt;ifconfig&lt;/b&gt;. There'll be a field that reads &lt;b&gt;inet address&lt;/b&gt;. Now there might be two fields that read that. If you're connected by Ethernet, use the one listed under Ethernet. If you're connected by Wi-Fi, use the one listed under WLAN0&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
As for the &lt;b&gt;&amp;lt;port_number&amp;gt;&lt;/b&gt;, the default port for any Django project is &lt;b&gt;8000&lt;/b&gt;. However, you can use any port you want with the exceptions of the ones used for say SMTP and Telnet.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Let's look at an example. If my IP address is &lt;b&gt;172.20.55.21&lt;/b&gt;&amp;nbsp;and I want to use the standard port, i.e. &lt;b&gt;8000&lt;/b&gt;, my command will look like this:&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;python manage.py runserver 172.20.55.21:8000&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Then, any user wanting to access it, (again, from the same network, can't stress that enough) should simply type&amp;nbsp;&lt;b style="text-align: center;"&gt;172.20.55.21:8000 &lt;/b&gt;&lt;span style="text-align: center;"&gt;in the URL bar and he will be able to view and use the site.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/07/how-to-access-your-django-site-from.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-5290094895652921506</guid><pubDate>Tue, 09 Jun 2015 13:24:00 +0000</pubDate><atom:updated>2015-06-09T06:27:49.805-07:00</atom:updated><title>Deploying a Django application - Part I</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Well, first and foremost, this blog just crossed 1000 page views. Sincere thanks to all readers. Despite what the title indicates, this is not the last post. The world of Django is ever changing and there's always new stuff to learn. But this post (or series of posts, haven't decided yet) will show you how to deploy the project you've created to a server and make it battle ready.&lt;br /&gt;
&lt;br /&gt;
I was recently drafted by my college to create and deploy an online mentorship platform. We have a mentorship process that involves scheduling meetings with mentors and discussing attendance, marks and also stuff like grievances. The college wants to make it paper free. So here I am. It's a point of pride, because I'm creating and deploying all by myself.&lt;br /&gt;
&lt;br /&gt;
Anyway, first off, let's see what all we need to pull this off. Here's the list:&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Working Django code (duh.)&lt;/li&gt;
&lt;li&gt;A &lt;u&gt;dedicated&lt;/u&gt; computer that will act as a server. Specifications:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;At least 4 GB RAM (to handle multiple concurrent requests)&lt;/li&gt;
&lt;li&gt;At least 500 GB Hard Disk space (to store the database and static files)&lt;/li&gt;
&lt;li&gt;Django installation: same as or one compatible with the one you developed your project in&lt;/li&gt;
&lt;li&gt;A static IP address that is unique within the network you want to host the service in&lt;/li&gt;
&lt;li&gt;Side note: You might want to consider multiple servers if you anticipate thousands of requests per hour. Otherwise just one is OK&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;div&gt;
Have you got all this? Great! Now let's see what changes you need to make in the code to make it deployment ready. Here are a few things off the top of my head. They're pretty basic. We'll go into more details in subsequent posts. I guess I've made up my mind now :P Anyway, here they are:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Change &lt;b&gt;DEBUG = True &lt;/b&gt;to &lt;b&gt;DEBUG&lt;/b&gt;&amp;nbsp;= &lt;b&gt;False &lt;/b&gt;in the settings file. The reason for this is that if there's an error Django gives you the comprehensive report, along with some part of the code. You don't want the users to be able to see that&lt;/li&gt;
&lt;li&gt;If you need to have that, you can make two settings files: one for development and one for production. Here's how:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;You can have two copies of the same file, with &lt;b&gt;DEBUG True&lt;/b&gt;&amp;nbsp;in one&amp;nbsp;(development)&amp;nbsp;and &lt;b&gt;False&lt;/b&gt;&amp;nbsp;in the other (production)&lt;/li&gt;
&lt;li&gt;Alternatively, you can have a settings_production file that will import the main settings file. That way, all the user will see is an import statement in case of an error&lt;/li&gt;
&lt;li&gt;Note that all this configuration and switching between two settings files have to be registered in the &lt;b&gt;manage.py &lt;/b&gt;file before running the application&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;div&gt;
This is the initial preparation you need to make. I'm discontinuing here so that the post will be of appropriate size and not tedious to read. Keep checking!&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/06/deploying-django-application.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-8269756469311490877</guid><pubDate>Tue, 06 Jan 2015 05:32:00 +0000</pubDate><atom:updated>2015-01-05T21:32:43.911-08:00</atom:updated><title>Django Forms</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
This is another example of Django's reusability. We saw how to create your own form in HTML and render it and get data from it using GET or POST methods. Again, Django does this for you. While it certainly is useful knowing how to do this on a basic level, using Django's Forms API can give you more powerful forms with richer features.&lt;br /&gt;
&lt;br /&gt;
Like &lt;b&gt;models.py&lt;/b&gt;, create a file called &lt;b&gt;forms.py &lt;/b&gt;in the app where the form is required. Let's create a simple contact form. In &lt;b&gt;forms.py&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;forms.py&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;b&gt;from django import forms&lt;br /&gt;&lt;br /&gt;class SimpleUploadForm (forms.Form):&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;name = forms.CharField (label = "Enter name")&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;email = forms.EmailField (&lt;/b&gt;&lt;b&gt;&lt;b&gt;label = "Enter email ID", &lt;/b&gt;required = False)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;dob = forms.DateTimeField (&lt;/b&gt;&lt;b&gt;&lt;b&gt;label = "Enter date of birth"&lt;/b&gt;)&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, just like a model, we created a simple form. Now let's render it from a view.&lt;br /&gt;
&lt;b&gt; &lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;views.py&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;from django.shortcuts import render&lt;br /&gt;from forms import SimpleContactForm&lt;br /&gt;&lt;br /&gt;def simple_contact_form (request):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; form = SimpleContactForm ()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return render (request, "simple_contact_form.html", {"form":form})&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Here, we import the &lt;b&gt;SimpleContactForm &lt;/b&gt;class from &lt;b&gt;forms.py &lt;/b&gt;and initialize its instance in the view. Then we pass that instance as a context to the webpage we're rendering. The view will become clearer when you see the template.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;simple_contact_form.html&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Simple Contact Form&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;body&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;form action = "" method = "POST"&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;table&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {{ form.as_table }}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/table&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {% csrf_token %}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;input type = "submit" value = "Submit"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/form&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Ok now let's break this down. First, &lt;b&gt;form action = "" &lt;/b&gt;means the page doesn't change when the button is clicked. Now, in the form, we've passed the &lt;b&gt;SimpleContactForm () &lt;/b&gt;instance as &lt;b&gt;form &lt;/b&gt;to the template. That will be rendered in the &amp;lt;form&amp;gt; tag. The &lt;b&gt;as_table&lt;/b&gt; method displays the form as a table. However, that only contains &lt;b&gt;&amp;lt;tr&amp;gt;&lt;/b&gt;, &lt;b&gt;&amp;lt;th&amp;gt;&lt;/b&gt; and &lt;b&gt;&amp;lt;td&amp;gt; &lt;/b&gt;tags. So we have to add the &lt;b&gt;&amp;lt;table&amp;gt; &lt;/b&gt;and &lt;b&gt;&amp;lt;/table&amp;gt; &lt;/b&gt;tags ourselves. Another form display method is &lt;b&gt;as_p&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
The &lt;b&gt;{% csrf_token %} &lt;/b&gt;protects &lt;b&gt;POST&lt;/b&gt; from, well, CSRF. If you remember I told you to comment that line which included that middleware class. You can uncomment it now and proceed normally.&lt;br /&gt;
See you around!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/01/django-forms.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-5849681318873747788</guid><pubDate>Sun, 04 Jan 2015 06:43:00 +0000</pubDate><atom:updated>2015-01-03T22:43:09.438-08:00</atom:updated><title>HTTP headers</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Many times you many need the IP address of the computer that's accessing your webapp. You may want it for a survey, or cataloging that user to offer better services and what not. Today we'll see not only how to get the IP address, but a host of other client side information that can be used to fine tune your app.&lt;br /&gt;
&lt;br /&gt;
Let's take the IP address for example. I once needed it to see from where the app was being accessed from the most. I wrote a tracker that would piggyback on the client's signal and work backwards all the way to the source and get me the IP address. But little did I know that Django already does that for me. The IP address and host name (ISP) is encoded in the &lt;b&gt;request&lt;/b&gt;&amp;nbsp;parameter. Let's see how to view them.&lt;br /&gt;
&lt;br /&gt;
To do this we'll create a simple view. In HTML, we'll create a list that contains header name and value, and then populate it through Django.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;headers.html&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Django HTTP headers&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;body&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;ul&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {% for header in headers %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;li&amp;gt;&amp;lt;b&amp;gt;{{ header.name }}&amp;lt;/b&amp;gt;: {{ header.value }}&amp;lt;/li&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {% endfor %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/ul&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/body&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Pretty straightforward. Now let's populate the list.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;views.py&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;from django.shortcuts import render&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;class Header (object):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; def __init__ (self, name, value):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.name, self.value = name, value&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;def headers (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; headers = []&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; headers_list = request.META.items ()&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for name, value in headers_list:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; headers.append (Header (name, value))&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return render (request, "headers.html", {"headers":headers})&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Let's see what's going on here. First, we import the &lt;b&gt;render &lt;/b&gt;function. Then, we create a class called &lt;b&gt;Header&lt;/b&gt;&amp;nbsp;for use in the templating language. We already saw this with Student in &lt;b&gt;Django Templating Language - Part IV&lt;/b&gt;. Now,&amp;nbsp;&lt;b&gt;request.META &lt;/b&gt;contains a list of headers and its method &lt;b&gt;items() &lt;/b&gt;returns a list of tuples with name and value. We use that to populate the list in HTML.&lt;br /&gt;
&lt;br /&gt;
One more thing. Notice how I provided the context directly as a parameter in the &lt;b&gt;render &lt;/b&gt;function call? You can do that too. There's no need to do the Template/Context schedule each time.&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/01/http-headers.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-2351006442956863157</guid><pubDate>Sat, 03 Jan 2015 18:10:00 +0000</pubDate><atom:updated>2015-01-03T10:10:18.562-08:00</atom:updated><title>App specific URLs</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
As you may know, Django boasts &lt;b&gt;reusability&lt;/b&gt; of apps. This means you can just copy one app from one project, plug it into another and it would still work the same way. Neat, huh? Well, admittedly, till now we've only focused on getting out projects to run. But now that we know the basics of creating a working Django project, it's time to delve deeper and take advantage of Django's rich features.&lt;br /&gt;
&lt;br /&gt;
Let's start with URLs. Till now, we just went on adding URLs to the main &lt;b&gt;urls.py&lt;/b&gt;&amp;nbsp;as we developed the project. But when the number of URLs starts going into the triple digits, the &lt;b&gt;urls.py&lt;/b&gt;&amp;nbsp;file can become massive and difficult to keep track of. So let's see how to create a &lt;b&gt;urls.py &lt;/b&gt;file in each app and direct the main file to this new one.&lt;br /&gt;
&lt;br /&gt;
In the main &lt;b&gt;urls.py&lt;/b&gt;&amp;nbsp;file, add a pattern that will point to the app:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;url (r'^myapp/', include('myproject.myapp.urls')),&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
This tells Django that whenever someone access the &lt;b&gt;/myapp/ &lt;/b&gt;URL they should be directed to the &lt;b&gt;urls.py &lt;/b&gt;file in that app. From here on in, all the views rendered in this app will have URLs like &lt;b&gt;/myapp/foo/bar&lt;/b&gt;. This makes them easier to manage. There's also another advantage: you no longer need unique URLs; you can have &lt;b&gt;/home/&lt;/b&gt;&amp;nbsp;in multiple apps. That is, &lt;b&gt;/myapp1/home &lt;/b&gt;and &lt;b&gt;/myapp2/home &lt;/b&gt;have the same sub URL, but are still unique as a whole.&lt;br /&gt;
&lt;br /&gt;
Now, create a &lt;b&gt;urls.py &lt;/b&gt;in the concerned app. In it:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;from django.conf.urls.defaults import *&lt;/b&gt;&lt;br /&gt;
(This imports all patterns from the original &lt;b&gt;urls.py&lt;/b&gt;&amp;nbsp;file)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;urlpatterns = patterns ('myproject.myapp.views',&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; url(r'^foo/', 'bar'),&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Another advantage: you don't have to type the entire path to the view; just the name suffices. Hence, from the above example, when someone accesses the &lt;b&gt;/myapp/foo/ &lt;/b&gt;URL, the &lt;b&gt;bar &lt;/b&gt;view from&amp;nbsp;&lt;b&gt;myproject.myapp.views &lt;/b&gt;will be rendered.&lt;br /&gt;
&lt;br /&gt;
To summarize, this makes handling URLs very easy. We'll see more examples of reusability in the following tutorials.&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2015/01/app-specific-urls.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-3544924303601214557</guid><pubDate>Thu, 06 Nov 2014 17:43:00 +0000</pubDate><atom:updated>2014-11-08T20:59:00.357-08:00</atom:updated><title>Django Templating Language - Part IV</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
OK, for this exercise, assume I have a database table (in SQLite3) that contains student names and roll numbers. Let's look at this in steps.&lt;br&gt;
&lt;br&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Import everything you need in &lt;b&gt;views.py&lt;/b&gt;:&lt;br&gt;&lt;b&gt;import sqlite3&lt;br&gt;from django.template import Template, Context&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Create a data structure that will hold your records:&lt;br&gt;&lt;b&gt;class Student (object):&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; def __init__ (self, name, rno):&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; self.name = name&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; self.rno = rno&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Define your view and in it extract all records of student:&lt;br&gt;&lt;b&gt;def display (request):&lt;/b&gt;&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; conn = sqlite3.connect ("/path/to/db/dbname.db")&lt;/b&gt;&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; cur = conn.cursor ()&lt;/b&gt;&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; cur.execute ("select * from student")&lt;/b&gt;&lt;br&gt;&lt;b style="font-weight: bold;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; students = [] &amp;nbsp;&lt;/b&gt;# Will store student records&lt;br&gt;&lt;b style="font-weight: bold;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for row in cur:&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; students.append (Student (row[0], row[1]))&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;# Append to list 'students' an object of the class 'Student'&lt;br&gt;&lt;br&gt;&lt;/li&gt;
&lt;li&gt;Now it's time for the ol' Template/Context magic:&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; t = Template ("/path/to/html/file/stud.html")&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; c = Context ({'students':students})&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; html = t.render (c)&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return HttpReponse (html)&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;But we're not done yet! We don't have the HTML file in which we define how the data is to be structured:&lt;br&gt;&lt;b&gt;&amp;lt;html&amp;gt;&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {% for student in students %}&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Roll no.: {{ student.rno }}&lt;/b&gt;&lt;b&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;Name: {{ student.name }}&lt;/b&gt;&lt;/b&gt;&lt;b&gt;&lt;br&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/b&gt;{% endfor %}&lt;/b&gt;&lt;br&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;&lt;br&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
You're set! Notice how elements can be accessed in HTML using the names you defined in Python in the class &lt;b&gt;Student&lt;/b&gt;. Neat huh? I threw a lot of information your way right now. Please comment with doubts. Until next time!&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/11/django-templating-language-part-iv.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-7476643918388110470</guid><pubDate>Thu, 06 Nov 2014 17:23:00 +0000</pubDate><atom:updated>2014-11-06T09:24:17.603-08:00</atom:updated><title>CSRF Token</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
I don't believe I didn't cover this before. Many of you (in fact &lt;i&gt;all&lt;/i&gt;&amp;nbsp;of you) must be getting a 'CSRF Token missing' error when you submit data using the &lt;b&gt;POST&lt;/b&gt; method. First of all let me tell you what CSRF is.&lt;br /&gt;
&lt;br /&gt;
CSRF stands for &lt;b&gt;C&lt;/b&gt;ross &lt;b&gt;S&lt;/b&gt;ite &lt;b&gt;R&lt;/b&gt;equest &lt;b&gt;F&lt;/b&gt;orgery. When data is submitted using the &lt;b&gt;GET &lt;/b&gt;method, it just gets encoded in the URL. But when it is submitted using the &lt;b&gt;POST &lt;/b&gt;method it is sent directly to the servers. During this transfer if there is some bot snooping on that site, it can intercept the data and send it's own, infected data to the site.&lt;br /&gt;
&lt;br /&gt;
Anyway, coming back to Django, the solution to that error lies in Django's Templating Language. I myself am still trying to understand it. In the meantime, I found a temporary fix.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;NOTE:&lt;/u&gt;&amp;nbsp;This is a temporary fix for development websites, and should not be deployed on production websites.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Sorry for sneaking that on you like that. But it had to be done. You'll understand once I tell you how to (temporarily) fix it.&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Open &lt;b&gt;settings.py&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Look for a tuple named &lt;b&gt;MIDDLEWARE_CLASSES&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;You'll see&amp;nbsp;&lt;b&gt;django.middleware.csrf.CsrfViewMiddleware &lt;/b&gt;inclulded in it.&lt;/li&gt;
&lt;li&gt;Comment that line out.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now do you see why this is so dangerous, even though it fixes the error? You're essentially disabling Django's in built protection against CSRF (which is pretty good). NEVER deploy it on production websites. This fix is only for a temporary situation, when you want to evaluate the authenticity of some other code module.&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/11/csrf-token.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-2613763504293768575</guid><pubDate>Tue, 04 Nov 2014 17:39:00 +0000</pubDate><atom:updated>2014-11-04T09:47:58.456-08:00</atom:updated><title>Django Templating Language - Part III</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
We're finally here! We've already seen how to extract stuff from databases. Now let's put that to good use. Before we begin, let me explain two concepts here: &lt;b&gt;Template&lt;/b&gt;&amp;nbsp;and &lt;b&gt;Context&lt;/b&gt;.&lt;br /&gt;
&lt;b&gt;Template&lt;/b&gt;&amp;nbsp;is the front end, the looks of the page. This will mean the HTML (+CSS+jQuery+whatever else) code you write.&lt;br /&gt;
&lt;b&gt;Context &lt;/b&gt;means what you want to put in that page. In the earlier post, we had used the variable &lt;b&gt;{{ name }}&lt;/b&gt;. &lt;b&gt;Context&lt;/b&gt;&amp;nbsp;will tell Python what value to pass to that variable.&lt;br /&gt;
&lt;br /&gt;
First things first. Let's import the necessary stuff.&lt;br /&gt;
&lt;b&gt;from django.template import Template, Context&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;from django.shortcuts import render&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;from django.http import HttpResponse&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Now what I do is pass HTML code as string to &lt;b&gt;Template &lt;/b&gt;make it render it. While this is a neat method, I'm sure there are others out there that I don't know. So please comment so that I'll be able to learn other (maybe more efficient) ways to render templates.&lt;br /&gt;
The way to do this would be to use file handling. So, in &lt;b&gt;views.py&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;def showName (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; f1 = open ("/path/to/file/something.html")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; code = ""&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; for i in f1.read ():&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; code += i&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Hope you understand what I did there. I opened a file called &lt;b&gt;something.html&lt;/b&gt;, and returned everything in that file in the form of the &lt;b&gt;code&lt;/b&gt;&amp;nbsp;variable.&lt;br /&gt;
Now, let's create a template out of that file.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;def showName (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; f1 = open ("/path/to/file/something.html")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; code = ""&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; for i in f1.read ():&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; code += i&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; t = Template (code)&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;

As I said earlier, I passed file contents in the form of a string to the &lt;b&gt;Template &lt;/b&gt;method. We'll consider the same code we used for for and if in the last post (I'm hoping you will add all the other necessities to make it valid HTML code). Now let's pass data to &lt;b&gt;{{ name }} &lt;/b&gt;using &lt;b&gt;Context&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;def showName (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; f1 = open ("/path/to/file/something.html")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; code = ""&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; for i in f1.read ():&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; code += i&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; t = Template (code)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; c = Context ({'name':'tejas'})&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
As you can see, you have to pass a dictionary as a parameter to the &lt;b&gt;Context&lt;/b&gt;&amp;nbsp;method. The keys are the variables used in the templating language, and the values are the values we want them to have. The values can be variables too. But the keys have to be strings.&lt;br /&gt;
Finally, let's render the page.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;def showName (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; f1 = open ("/path/to/file/something.html")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; code = ""&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; for i in f1.read ():&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; code += i&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; t = Template (code)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; c = Context ({'name':'tejas'})&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp;&lt;i&gt; html = t.render (c)&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&amp;nbsp; &amp;nbsp; return HttpReponse (html)&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
The second to last statement tells the system to render the context &lt;b&gt;c &lt;/b&gt;to the template &lt;b&gt;t&lt;/b&gt;. Assign this to a variable and return HttpResponse of this variable.&lt;br /&gt;
In the next part, we'll see how to render context with content from databases. Au revoir!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/11/django-templating-language-part-iii.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-1188993365476938087</guid><pubDate>Tue, 04 Nov 2014 17:08:00 +0000</pubDate><atom:updated>2014-11-04T09:28:57.482-08:00</atom:updated><title>Django Templating Language - Part II</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Now let's look at some in-built tags. These will help you arrange data efficiently on your page. These are a lot like XML. However, the syntax is different and it is easier to manage.&lt;br /&gt;
If you've noticed, it says &lt;b&gt;Django Templating &lt;/b&gt;&lt;i style="font-weight: bold;"&gt;Language&lt;/i&gt;. Thus, as with every other language, this one also has control structures. Let's look at a few.&lt;br /&gt;
(&lt;b&gt;Note&lt;/b&gt;: We're still learning the language, and even at the end of this post the code will print raw data. I swear, we're really close to rendering data from Python. Just hang tight for one more post.)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;FOR:&lt;/b&gt;&lt;br /&gt;
This simulates a for loop. It is useful when extracting data from a database and rendering on the page, especially when you don't know the number of rows that will be returned. This will simply iterate over the list and render everything.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;lt;html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;{% for row in rows %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;{{ row }}&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;{% endfor %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Let's look at this code step by step. First of all, let's map the for here with that in Python.&lt;br /&gt;
In Python:&lt;br /&gt;
&lt;b&gt;for i in lst&lt;/b&gt;&lt;br /&gt;
Thus, &lt;b&gt;i &lt;/b&gt;is &lt;b&gt;row&lt;/b&gt;, the variable that we will use to iterate over the list of rows that the database returns. &lt;b&gt;lst &lt;/b&gt;is &lt;b&gt;rows&lt;/b&gt;, the list over which we need to iterate. You can name them whatever you want. Iterating over the list is useless if you don't do anything with the data. As you can see, I've simply printed the value. (As &lt;b&gt;row &lt;/b&gt;is a variable, it is enclosed within &lt;b&gt;{{ }}&lt;/b&gt;).&lt;br /&gt;
Again, this is assuming that each row has only one field. This is seldom the case. We'll see how to render rows with multiple fields when we see Python rendering in general.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;IF:&lt;/b&gt;&lt;br /&gt;
This will simulate an if block. Now, &lt;b&gt;elif &lt;/b&gt;won't work in Django 1.3, but it does in 1.6 onwards. If you want an &lt;b&gt;elif &lt;/b&gt;statement, just put it under &lt;b&gt;else &lt;/b&gt;and then &lt;b&gt;if&lt;/b&gt;.&lt;br /&gt;
We normally compare two values in &lt;b&gt;if&lt;/b&gt;. So Django ships two tags, &lt;b&gt;ifequal &lt;/b&gt;and &lt;b&gt;ifnotequal&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;lt;html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;{% ifequal name "tejas" %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;{{ name }}&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;{% endifequal %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&amp;lt;html&amp;gt;&lt;/b&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;{% ifnotequal name "tejas" %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;{{ name }}&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;{% endifnotequal %}&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;/html&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;ifequal &lt;/b&gt;compares &lt;b&gt;name &lt;/b&gt;and &lt;b&gt;"tejas" &lt;/b&gt;to see if they're equal. It's Python equivalent would be &lt;b&gt;if name == "tejas"&lt;/b&gt;. These are the two parameters. They may be variables, strings, or any hard coded values of any data type.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ifnotequal&amp;nbsp;&lt;/b&gt;compares&amp;nbsp;&lt;b&gt;name&amp;nbsp;&lt;/b&gt;and&amp;nbsp;&lt;b&gt;"tejas"&amp;nbsp;&lt;/b&gt;to see if they're &lt;b&gt;not&amp;nbsp;&lt;/b&gt;equal. It's Python equivalent would be&amp;nbsp;&lt;b&gt;if name != "tejas"&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Great! We've learned the basics of Django's Templating Language. As usual, you can comment or inbox me with your queries. In the next post we see how to actually put data there through Python (Yay!).&lt;br /&gt;
You didn't Yay did you? Come on guys, a little more enthusiasm!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/11/django-templating-language-part-ii.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-8845976270722117653</guid><pubDate>Tue, 04 Nov 2014 16:44:00 +0000</pubDate><atom:updated>2014-11-04T08:44:49.643-08:00</atom:updated><title>Django 1.6</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hi all! Today let's explore Django's newest version. This won't be in incredible detail, however. The post would be too long. We'll just see an overview. Even as we speak the developers are working on versions 1.7 and 1.8&lt;br /&gt;
&lt;br /&gt;
Let's start at the top.
&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" cellpadding="2"&gt;
    &lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Django 1.3&lt;/th&gt;
&lt;th&gt;Django 1.6&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Project directory structure&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;The structure is the same as we explored earlier. Once you open the project directory, you can see the list of apps, &lt;i&gt;__init__.py, manage.py, settings.py, and urls.py&lt;/i&gt;.&lt;/td&gt;
&lt;td&gt;The structure here is slightly different. Once you open the project directory, there is another folder with the same name as your project. The manage.py is here too. Inside the other directory are all the apps and settings etc. While this provides a certain level of independence and atomicity, I personally am disappointed with this. I liked the older one better, and the new one takes some getting used to.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Settings&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;The settings file contains all settings imaginable, with the ability to add your own.&lt;/td&gt;
&lt;td&gt;The default settings file shipped with 1.6 is spartan and contains only those settings that an app absolutely needs to run. Rest all need to be added, e.g. TEMPLATE_DIRS, STATIC_DIRS etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;WSGI (Web Server Gateway Interface)&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;No additional wsgi file&lt;/td&gt;
&lt;td&gt;Shipped with a default wsgi file (&lt;i&gt;wsgi.py&lt;/i&gt;) to configure the interface.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Database configuration&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;The ENGINE field is supposed to hold the DBMS you use, like &lt;i&gt;sqlite3&lt;/i&gt;, &lt;i&gt;mysql&lt;/i&gt; etc.&lt;/td&gt;
&lt;td&gt;The ENGINE field now has to include the full specification, i.e. &lt;i&gt;django.db.backends.sqlite3&lt;/i&gt;, &lt;i&gt;django.db.backends.mysql&lt;/i&gt; etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
These are the changes I noticed and those that directly affect my coding style in Django. Feel free to comment ones that you've noticed. Until next time.&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/11/django-16.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-9102242385301310515</guid><pubDate>Mon, 25 Aug 2014 18:06:00 +0000</pubDate><atom:updated>2014-08-25T11:06:51.584-07:00</atom:updated><title>Django Templating Language - Part I</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! I know I haven't posted anything in a very long time. Exigent circumstances. Anyway, now I'm back with what may well be the most important skill in your entire Django toolkit. The templating language that Django ships makes writing pages that display multiple results mundanely easy.&lt;br /&gt;
In this part I'll only introduce you to templates. We'll see how to render them in the next one.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Variables:&lt;/b&gt;&lt;br /&gt;
We all know what variables are and how important they are for programming. This is how variables are represented in Django:&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;{{ variable_name }}&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
It can also be written as &lt;b&gt;{{variable_name}} &lt;/b&gt;but the former is more readable. Let's see how to 'declare' a variable in an HTML script:&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;b&gt;&amp;lt;h1&amp;gt;{{ heading }}&amp;lt;/h1&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;b&gt;&amp;lt;form action = "/foo/" method = "post"&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;lt;input type = "text" name = "bar" placeholder = "{{ placeholder }}"&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;b&gt;&amp;lt;/form&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Notice the difference? &lt;b&gt;{{ heading }} &lt;/b&gt;is without quotes and &lt;b&gt;{{ placeholder }} &lt;/b&gt;is with. The basic concept is you write the name of the variable exactly as you would write normal text. While writing normal text, you would not put quotes between the &lt;b&gt;&amp;lt;h1&amp;gt;...&amp;lt;/h1&amp;gt; &lt;/b&gt;tags but would in the &lt;b&gt;&amp;lt;input&amp;gt; &lt;/b&gt;tag.&lt;br /&gt;
&lt;br /&gt;
Right now if you just write this much code it'll print all raw data. There is no use of this code yet. However, we'll see how to render context to these templates soon enough.&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/08/django-templating-language-part-i.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-8026119573779291547</guid><pubDate>Tue, 17 Jun 2014 09:39:00 +0000</pubDate><atom:updated>2014-06-17T02:39:45.488-07:00</atom:updated><title>Enforcing case sensitivity in databases</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! When I was developing today I realized that the username primary keys were not case sensitive. This is a big drawback. It reduces the number of unique and viable usernames. Even though this number is still gargantuan, case sensitive usernames are a must everywhere.&lt;br /&gt;
The demonstrate my problem, here's what was happening:&lt;br /&gt;
&lt;b&gt;nitin&lt;/b&gt; was a username registered on my site. It was allowing entry and showing the same account when logged in with &lt;b&gt;Nitin&lt;/b&gt;, &lt;b&gt;nItin&lt;/b&gt;, and so on. There are 32 possibilities (2^5) of &lt;b&gt;nitin &lt;/b&gt;being spelled with either upper or lower case characters.&lt;br /&gt;
Thus, 32 possible user accounts were being mapped on to only one. To do this, the username has to be checked in the query itself.&lt;br /&gt;
&lt;br /&gt;
Now, &lt;b&gt;select something from some_table where username = "nitin" &lt;/b&gt;would give you the same result for all 32 possibilities of &lt;b&gt;nitin&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
To avoid this, the query has to be altered:&lt;br /&gt;
&lt;b&gt;select something from some_table where binary username = "nitin"&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;b&gt;binary &lt;/b&gt;checks for case also.&lt;br /&gt;
Cheers!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/enforcing-case-sensitivity-in-databases.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-3459011125894815389</guid><pubDate>Thu, 12 Jun 2014 08:19:00 +0000</pubDate><atom:updated>2014-06-12T01:20:52.642-07:00</atom:updated><title>File download script</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Greetings! If you recall, a while ago we saw how to write a file upload script. File download is also equally important. Let's see how to write a download script now.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;def download (request):&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; filename = "/foo/bar/%s.%s" % (name, extension)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; f1 = open (filename, "r")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; response = HttpResponse (f1, mimetype = "type/extension")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; response['Content-Disposition'] = "attachment; filename = foo_bar.extension" &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return response&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Ok, now let's examine this code:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Specify the file name&lt;/li&gt;
&lt;li&gt;Open this file in &lt;b&gt;read &lt;/b&gt;mode&lt;/li&gt;
&lt;li&gt;Create &lt;b&gt;HttpResponse &lt;/b&gt;object with the following parameters: &lt;b&gt;filename&lt;/b&gt;, and &lt;b&gt;mimetype&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;MIME &lt;/b&gt;stands for &lt;b&gt;M&lt;/b&gt;ultipurpose &lt;b&gt;I&lt;/b&gt;nternet &lt;b&gt;M&lt;/b&gt;ail &lt;b&gt;E&lt;/b&gt;xtension.&lt;/li&gt;
&lt;li&gt;Here's a list of all MIME types. Find the appropriate one for the extension you want: &lt;b&gt;http://www.sitepoint.com/web-foundations/mime-types-complete-list/&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;The second filename in the last but one line is the name and extension which the downloaded file should have. For example, your file may be .py, but you might want to make it download as .txt&lt;/li&gt;
&lt;li&gt;Finally, return the &lt;b&gt;HttpResponse &lt;/b&gt;object, and et voila! You have your file ready for download.&lt;/li&gt;
&lt;/ul&gt;
&lt;u&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;/u&gt; As usual, this download will be triggered when the URL that points to the function &lt;b&gt;download&lt;/b&gt; in the particular view is accessed.&lt;br /&gt;
Cheers! &lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/file-download-script.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-9094354655858883499</guid><pubDate>Mon, 09 Jun 2014 14:08:00 +0000</pubDate><atom:updated>2014-06-09T07:12:32.276-07:00</atom:updated><title>The SaaS methodology</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! This one's not a tutorial. So just sit back with a cup of coffee and relax. I assure you this is going to be fun.&lt;br /&gt;
Up until now we've seen two names associated with programming in Django. The first one is &lt;b&gt;Web Design&lt;/b&gt;, which is very mundane. Any Tom, Dick and Harry can visit a CMS like Wordpress nowadays and make a website for themselves.&lt;br /&gt;
Then we saw the slightly more colorful and yet highly misunderstood name &lt;b&gt;Web Application Design&lt;/b&gt;, or simply &lt;b&gt;Web App Design&lt;/b&gt;.&lt;br /&gt;
I'm finally going to tell you a name that is not only prestigious, but one that will blow your mind whole. It's &lt;b&gt;SaaS&lt;/b&gt;, or &lt;b&gt;Software as a Service&lt;/b&gt;.&lt;br /&gt;
I'll give you a minute to let the awesomeness sink in.&lt;br /&gt;
Done?&lt;br /&gt;
Good.&lt;br /&gt;
Yes, Django programming falls under software. People think that software only means the ones we open on our computers.&lt;br /&gt;
The &lt;b&gt;Service&lt;/b&gt; in &lt;b&gt;SaaS &lt;/b&gt;is not service as in social service. It means the service you provide to computers.&lt;br /&gt;
SaaS pieces are hosted on servers and can be accessed from anywhere with an Internet connection. They may be free or charged. The charged ones are called &lt;b&gt;Proprietary Software&lt;/b&gt;. Also note that the free ones &lt;b&gt;are not &lt;/b&gt;called &lt;b&gt;Open Source&lt;/b&gt;. That's completely different. Open Source is when the underlying code is visible to the general public. Proprietary software may also be Open Source.&lt;br /&gt;
Furthermore, SaaS may be for only a company or organization in general or may be publicly usable. In the first case, it is hosted on indigenous servers, whose access is restricted to only within that organization. In the second, they would normally be hosted on a global server.&lt;br /&gt;
So how does SaaS make money? There are many models currently employed:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Developers can charge on a time basis (i.e. weekly, monthly, fortnightly etc) that will enable the users unlimited utility out of the software.&lt;/li&gt;
&lt;li&gt;They may charge a small amount every time the software is used, which then limits the usage. This one sometimes is a bigger money maker.&lt;/li&gt;
&lt;li&gt;They may also charge a very high one time price, which would transfer the ownership of the software to the buyer. The developers may further charge on maintenance and repairs.&lt;/li&gt;
&lt;/ul&gt;
So why did I write this article? When I first found this out several months ago while talking to my Dad (very wise man), it made me develop in Django in more earnest, all the while thinking, "Hey! I'm developing software!"&lt;br /&gt;
SaaS is also used while talking about RoR (Ruby on Rails). Currently the number of Rails developers greatly outnumber that of Django.&lt;br /&gt;
So I guess I'm hoping that by reading this article more people will be encouraged to become Django coders. Forget Sparta, we'll stand in front of Rails developers and say "THIS IS DJANGO!!".&lt;br /&gt;
(I have absolutely nothing against Rails or its developers by the way)&lt;br /&gt;
Cheers!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/the-saas-methodology.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-5151230284230625265</guid><pubDate>Mon, 09 Jun 2014 05:48:00 +0000</pubDate><atom:updated>2014-06-08T22:48:48.859-07:00</atom:updated><title>An important thing</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! This is an important one. This had me stymied for a day and a half. Do not forget to commit the changes before closing the connection in the particular view if that view contains any of &lt;b&gt;insert&lt;/b&gt;, &lt;b&gt;update&lt;/b&gt; or &lt;b&gt;delete&lt;/b&gt; queries. It doesn't matter with &lt;b&gt;select&lt;/b&gt; queries, but for the aforementioned it is a must.&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/an-important-thing.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-2418165603659020739</guid><pubDate>Fri, 06 Jun 2014 12:08:00 +0000</pubDate><atom:updated>2014-06-09T07:23:43.901-07:00</atom:updated><title>Displaying images</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
As I mentioned in a previous post, &lt;b&gt;&amp;lt;img src = ""&amp;gt; &lt;/b&gt;does not work in Django. I mean, it does work, and that's how we are going to display images, but it doesn't work if you give local machine addresses like &lt;b&gt;/home/foo/bar &lt;/b&gt;or &lt;b&gt;C:/images/foo/bar&lt;/b&gt;. This is actually a good thing. Django has a very sophisticated method of handling images and other files, often called as &lt;b&gt;static &lt;/b&gt;files.&lt;br /&gt;
If you open your &lt;b&gt;settings.py &lt;/b&gt;file, you'll see two URLs: &lt;b&gt;STATIC_URL &lt;/b&gt;and &lt;b&gt;MEDIA_URL&lt;/b&gt;. These can be used to render static files and media in general.&lt;br /&gt;
Now there are two ways of doing this:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Using the in-built &lt;b&gt;{{ STATIC_URL }} &lt;/b&gt;or &lt;b&gt;{{ MEDIA_URL }}&lt;/b&gt; variables. But as I have not posted about Django's templating language, we'll put this one on the back burner. But rest assured, once you understand the templating language, how to use these will be self-evident.&lt;/li&gt;
&lt;li&gt;Using full URLs, including the values specified in &lt;b&gt;{{ STATIC_URL }} &lt;/b&gt;and &lt;b&gt;{{ MEDIA_URL }}&lt;/b&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
We'll be using the second method. The only disadvantage of this method is that if the directory containing static files changes then you have to make sure that change reflects in all templates. In the first case, this would happen automatically.&lt;br /&gt;
&lt;br /&gt;
Let's assume you're running your website on localhost, i.e. &lt;b&gt;http://127.0.0.1/ &lt;/b&gt;and you want to render an image on the URL &lt;b&gt;http://127.0.0.1/home&lt;/b&gt;. Let's also assume that the &lt;b&gt;.html&lt;/b&gt; file behind this webpage is called &lt;b&gt;home.html&lt;/b&gt; (wow, that's a lot of assuming).&lt;br /&gt;
&lt;br /&gt;
Anyway, change into your project directory, and create a folder called &lt;b&gt;static&lt;/b&gt;. Store all your images here. Let's assume (not again!) that you have an image called &lt;b&gt;garden.jpg &lt;/b&gt;in the &lt;b&gt;static &lt;/b&gt;folder, which you want to display on the homepage.&lt;br /&gt;
&lt;br /&gt;
So instead of giving local machine paths, you give the path in the form of a URL.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;lt;img src&amp;nbsp;&lt;/b&gt; = &lt;b&gt;"http://127.0.0.1/static/garden.jpg"&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Now, to validate this, open your &lt;b&gt;settings.py &lt;/b&gt;file, and make the following changes.&lt;br /&gt;
&lt;b&gt; &lt;/b&gt;&lt;br /&gt;
&lt;b&gt;STATIC_ROOT = "/foo/bar/mysite/static"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;STATIC_URL = "/static/"&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
This will tell Django that when &lt;b&gt;http://127.0.0.1/static/ &lt;/b&gt;(or &lt;b&gt;STATIC_URL&lt;/b&gt;) is accessed, the path in &lt;b&gt;STATIC_ROOT &lt;/b&gt;is to be checked.&lt;br /&gt;
&lt;br /&gt;
That's all for now! Two things before we part:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;I'm still working on the templating tutorial, to make it concise as it is a very vast topic. Once I come up with it, we'll investigate the &lt;b&gt;{{ STATIC_URL }} &lt;/b&gt;and &lt;b&gt;{{ MEDIA_URL }} &lt;/b&gt;business.&lt;/li&gt;
&lt;li&gt;What we did here with &lt;b&gt;static&lt;/b&gt; can also be done with &lt;b&gt;media&lt;/b&gt;. They are two objects of the same file displaying facility, giving us more customizability. To use &lt;b&gt;media&lt;/b&gt;, just replace &lt;b&gt;static &lt;/b&gt;with &lt;b&gt;media &lt;/b&gt;in all of the code above.&lt;/li&gt;
&lt;/ul&gt;
Cheers! &lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/displaying-images.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-6123309776053815313</guid><pubDate>Tue, 03 Jun 2014 09:01:00 +0000</pubDate><atom:updated>2014-06-03T02:01:12.525-07:00</atom:updated><title>Backing up</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Backing up your files is extremely important. If the server you've hosted your site on crashes and you don't have a copy of the files then your goose is cooked. You'll have to start from scratch.&lt;br /&gt;
I've written the code for backing up files and your MySQL database. Here's the link:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;https://github.com/AgentK1729/File-Backup&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
In case you have an SQLite database, there's no need to back up the database as shown in the code, there'll be a readymade .db file. Just copy it somewhere reliable.&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/backing-up.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-6124460419332081994</guid><pubDate>Tue, 03 Jun 2014 08:52:00 +0000</pubDate><atom:updated>2014-06-03T01:56:23.456-07:00</atom:updated><title>Troubleshooting</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
More often than not, your site will generate an error that will cripple its functioning. This is not a bad thing, however. Not only does it let you grow as a programmer, but also it is said that if your code executes the first time without any errors, you're not a good programmer!&lt;br /&gt;
I'm currently developing a commercial website and this happened to me today. I did some resizing and suddenly all my webpages showed the same message: 'Unhandled Exception'.&lt;br /&gt;
After hours of hunting down the source, I discovered that I deleted two apps but forgot to remove them from the &lt;b&gt;INSTALLED_APPS &lt;/b&gt;in the &lt;b&gt;settings.py&lt;/b&gt;.&lt;br /&gt;
The point of this story is that such things happen to everyone without exception. The important thing is to not panic. Here's what you do:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Close your eyes and take a deep breath.&lt;/li&gt;
&lt;li&gt;Recall the last point where it was working.&lt;/li&gt;
&lt;li&gt;Retrace the steps you took from that point till now and evaluate what might have caused the error.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
The error can also be caused by some very insignificant things, which make you want to kick yourself.&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;The one I described.&lt;/li&gt;
&lt;li&gt;Forgetting to 'Reload' the site in case you have hosted it on a server.&lt;/li&gt;
&lt;li&gt;Not saving a file you made changes to.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
So guys, remember two very important things:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;The fact that you got an unidentifiable error means you're a better programmer than you think, because you managed to screw up a system like never before.&lt;/li&gt;
&lt;li&gt;DO NOT PANIC.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Follow these two and you'll be an awesome programmer.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Cheers!&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/06/troubleshooting.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-5265796135367452503</guid><pubDate>Tue, 13 May 2014 13:43:00 +0000</pubDate><atom:updated>2014-05-13T06:43:01.472-07:00</atom:updated><title>A break from the tutorial: Intuitive stuff</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! Let's take a break from the bombarding of information, and let's do something interesting. Let's talk about some of the features that are taken for granted in all the giant websites. You may already have noticed them. If not, double back and check them out. We'll also see how to implement them in your website.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The "Keep me logged in":&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Many websites like Facebook and Gmail offer this option at the time of login. What it does is even if you close the browser/tab/window, the next time you open it it opens on your homepage. Even more fascinating, if you try to log in from some other device it you're not logged in, and you even get an alert that someone tried to access your account from an unknown location. Let's see how this is done in steps:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;When you log in, cookies with your username are stored in your browser. If you close it and reopen it, and access that site again, it checks for that-site-specific cookies. If they are there, they take you to your homepage. Else, you're redirected to the login page.&lt;/li&gt;
&lt;li&gt;To prevent multiple people accessing the account at the same type, the session is logged in to the database. The username/email is unique, so only one session of that name can be logged in to the database. If another one tries, it captures the IP address, denies it access and warns the user. (We'll see how to track IP addresses in the next article)&lt;/li&gt;
&lt;li&gt;When you log in, it notes down your IP address. It is added to the list of addresses from which you frequently access that site.&lt;/li&gt;
&lt;li&gt;On log out, it deletes the cookies and logs you out of the database.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;The "Display &lt;i&gt;n&lt;/i&gt; search results":&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
This is very easy. Just maintain a count of how many results are wanted. Assume the form method is GET:&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;count = int (request.GET['count'])&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;temp = 0&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;for row in cur/cur.fetchall ():&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; if temp == count:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; else:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print row[0]&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp += 1&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Opening a new tab on hyperlink or button press:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
For hyperlink:&amp;nbsp;&lt;b&gt;&amp;lt;a href = "foo/bar.html" target = "_blank"&amp;gt;&lt;/b&gt;&lt;br /&gt;
For button press: &lt;b&gt;&amp;lt;form action = "/foo/" method = "GET" target = "_blank"&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
The &lt;b&gt;target = "_blank" &lt;/b&gt;does the trick. Note that it have to be inserted into form attributes and not the button's.&lt;br /&gt;
&lt;br /&gt;
That's it for today! Tell me if I missed anything.&lt;br /&gt;
Cheers!&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/a-break-from-tutorial-intuitive-stuff.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-1318954048075645196</guid><pubDate>Fri, 09 May 2014 16:58:00 +0000</pubDate><atom:updated>2014-05-09T10:00:10.496-07:00</atom:updated><title>Custom queries III: MySQL</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Ok, we saw how to connect to an SQLite database in Python. Now let's see MySQL. There's only a small difference in syntax, but it's significant. The steps, however, are the same.&lt;br /&gt;
&lt;br /&gt;
Import the library&lt;br /&gt;
&lt;b&gt;import MySQLdb as ms&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;u&gt;Note: The &lt;b&gt;as &lt;/b&gt;specifies an alternate name for the library. In this case, I won't have to type &lt;b&gt;MySQLdb &lt;/b&gt;again and again. I'll just type &lt;b&gt;ms&lt;/b&gt;.&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;
Establish a connection&lt;br /&gt;
&lt;b&gt;conn = ms.connect (host, username, password, database_name)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
If you're running this on a machine, normally host is &lt;b&gt;localhost&lt;/b&gt;. You'll have to create a root user, which you'll have to see how to do in your particular OS on the Internet. Provide that username and password. Then execute the command &lt;b&gt;create database db_name &lt;/b&gt;and use this database as the fourth field.&lt;br /&gt;
&lt;br /&gt;
Exhausted? So am I. I know it's a lot of effort, but trust me, the results are totally worth it.&lt;br /&gt;
&lt;br /&gt;
Create a cursor as usual&lt;br /&gt;
&lt;b&gt;cur = conn.cursor ()&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Remember I said &lt;b&gt;%s &lt;/b&gt;doesn't work in SQLite? Well, that's the only thing that works here. Let's write a query.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;query = "select name from employee where empid = %s" % empid&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;READ THIS VERY CAREFULLY:&lt;/b&gt;&lt;br /&gt;
Now, &lt;b&gt;empid &lt;/b&gt;is an integer, but you still have to pass is as a string. Assume &lt;b&gt;empid &lt;/b&gt;is &lt;b&gt;1234&lt;/b&gt;. The query then becomes:&lt;br /&gt;
&lt;b&gt;select name from employee where empid = 1234&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Let's look at a query where we need to pass a string.&lt;br /&gt;
&lt;b&gt;query = "select empid from employee where name = '%s'" % name&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Notice the difference? &lt;b&gt;%s &lt;/b&gt;from&lt;b&gt;&amp;nbsp;&lt;/b&gt;the first query is replace by&lt;b&gt; '%s' &lt;/b&gt;in the second. Assume &lt;b&gt;name &lt;/b&gt;is &lt;b&gt;Douglas&lt;/b&gt;.&amp;nbsp;The query now becomes:&lt;br /&gt;
&lt;b&gt;select empid from employee where name = 'Douglas'&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Don't worry if you're confused, go through the paragraph once again. Let it sink in.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OK, NOW RELAX AND ENJOY THE REMAINING PART.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Ok, the rest is pretty much the same.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;cur.execute (query)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;for row in cur.fetchall ():&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; print row[0]&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;for row in cur: &lt;/b&gt;in SQLite is replaced by &lt;b&gt;cur.fetchall (): &lt;/b&gt;in MySQL.&lt;br /&gt;
&lt;br /&gt;
Alternatively, you can also use &lt;b&gt;cur.fetchone () &lt;/b&gt;if you're sure only one row is going to be returned (for primary keys) or just want it that way.&lt;br /&gt;
&lt;br /&gt;
Hope I didn't miss anything. If I did, please comment. The remaining things are the same for both SQLite and MySQL. Don't forget to commit and close the connection once the job is done.&lt;br /&gt;
Cheers!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/custom-queries-iii-mysql.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-1680526963051781262</guid><pubDate>Wed, 07 May 2014 12:43:00 +0000</pubDate><atom:updated>2014-05-07T05:45:09.246-07:00</atom:updated><title>Custom queries II: SQLite</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Let's see how to connect to an SQLite database from Python. The current version is SQLite3.&lt;br /&gt;
Open a Python file, and import the library:&lt;br /&gt;
&lt;b&gt;import sqlite3&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Now, open a connection to the database. If the &lt;b&gt;.db&lt;/b&gt;&amp;nbsp;file already exists, connection to that file is opened, else a file of that name is created and connection is opened.&lt;br /&gt;
&lt;b&gt;conn = sqlite3.connect ("/foo/bar/filename.db")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
For executing queries, we need a cursor. So let's initialize that:&lt;br /&gt;
&lt;b&gt;cur = conn.cursor ()&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Now let's write a query.&lt;br /&gt;
&lt;b&gt;cur.execute ("select something from some_table")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
You can directly pass the string as a parameter, but for more customisations, you should initialize it separately and pass it as follows:&lt;br /&gt;
&lt;b&gt;query = "select foo from bar"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;cur.execute (query)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
If you want to insert data dynamically into your query, the following is the syntax:&lt;br /&gt;
THIS DOES NOT WORK:&lt;br /&gt;
&lt;b&gt;query = "select empid from employee where name = '%s'" % name&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;cur.execute (query)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
THIS SHOULD BE DONE:&lt;br /&gt;
&lt;b&gt;query = "select empid from employee where name = ?"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;data = (name,)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;cur.execute (query)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Note that &lt;b&gt;data&lt;/b&gt;&amp;nbsp;has to be a list or tuple. The syntax shown forces a single element to be a tuple. However, if there are multiple elements, just list them out normally like this:&lt;br /&gt;
&lt;b&gt;query = "select empid, dept from employee where name = ? and salary = ?"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;data = (name, sal)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;cur.execute (query)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
No data needs to be passed for insert and update queries, so just write those normally.&lt;br /&gt;
For delete, pass data in the same way as shown.&lt;br /&gt;
Now we need to fetch the data we get from select statement. Here's how you do it (assume I executed the previous query):&lt;br /&gt;
&lt;b&gt;for row in cur:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; print row[0], row[1]&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
We selected two values here, namely &lt;b&gt;empid &lt;/b&gt;and&lt;b&gt; dept&lt;/b&gt;. So &lt;b&gt;row[0] &lt;/b&gt;and&lt;b&gt; row[1] &lt;/b&gt;will hold &lt;b&gt;empid &lt;/b&gt;and&lt;b&gt; dept &lt;/b&gt;respectively. This can be extended for several values.&lt;br /&gt;
&lt;br /&gt;
Once you're done, you should commit the changes and close the connection.&lt;br /&gt;
&lt;b&gt;conn.commit ()&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;conn.close ()&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
In the next article, we'll see how to handle a MySQL database in Python.&lt;br /&gt;
Cheers!&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/custom-queries-ii-sqlite.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-658727817274795795</guid><pubDate>Wed, 07 May 2014 09:22:00 +0000</pubDate><atom:updated>2014-05-07T02:22:29.812-07:00</atom:updated><title>Custom queries I: Comparison between SQLite and MySQL</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
I've already said in earlier posts that you can use SQLite or MySQL as a database for your website. Recently when I was working on one I had to move to MySQL (I'll tell you why later in this article). So I thought I'll tell you guys how to transition.&lt;br /&gt;
But first, let's compare.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;SQLite:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;It is a lightweight DBMS&lt;/li&gt;
&lt;li&gt;It is mainly used for testing and for small websites that do not generate too much data.&lt;/li&gt;
&lt;li&gt;The biggest advantage is that it handles all the data through a &lt;b&gt;.db &lt;/b&gt;file which can be transported elsewhere and used in the same way&lt;/li&gt;
&lt;li&gt;It supports up to 2^64 rows (wow!)&lt;/li&gt;
&lt;li&gt;On the downside, for the kind of data that giants like Facebook and Google generate, SQLite falls laughably short&lt;/li&gt;
&lt;li&gt;It also has low concurrency control, which means that it crashes if too many people are trying to access the same data&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
The last point, the concurrency control, was the main reason I decided to switch to MySQL. The database of the website was crashing again and again. Let's look at MySQL now.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;MySQL:&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;It is tough, inflexible and an extremely powerful DBMS&lt;/li&gt;
&lt;li&gt;It is used by Facebook, Google, Twitter, pretty much all the web giants that exist today&lt;/li&gt;
&lt;li&gt;It has very high concurrency control&lt;/li&gt;
&lt;li&gt;It has the potential to store and handle an unlimited amount of data&lt;/li&gt;
&lt;li&gt;It has quick crash recovery&lt;/li&gt;
&lt;li&gt;There is very low chance of deadlocks&lt;/li&gt;
&lt;li&gt;All in all, if you plan to build a large site this is the DBMS for you&lt;/li&gt;
&lt;li&gt;The only downside is that it does not generate any file as earlier, so there has to be one native machine running the DBMS&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
This article is just the first of three of the series &lt;b&gt;Custom queries&lt;/b&gt;. The reason I'm writing these three is that the query API that Django provides is good, but it does not allow custom queries (as far as my knowledge goes). For example, you want to customize search parameters and find all people that satisfy multiple conditions. To do this, Python's database API is very useful and can be used very well in Django.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
In the next article, we'll see how to write custom queries for SQLite.&lt;/div&gt;
&lt;div&gt;
Cheers!&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/custom-queries-i-comparison-between.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-8487043778122878988</guid><pubDate>Wed, 07 May 2014 07:45:00 +0000</pubDate><atom:updated>2014-05-07T00:49:07.943-07:00</atom:updated><title>Sending emails</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
If you've noticed, on many sites, once you sign up, it sends you an automated confirmation email. This is another function your site should be able to perform. So today let's take a look at that.&lt;br /&gt;
Let's assume you have a Gmail account (if you don't, create one today, it's great!). We'll be using Gmail's email host (wow that rhymed).&lt;br /&gt;
Go to the &lt;b&gt;settings.py&lt;/b&gt;&amp;nbsp;file and add the following code anywhere in between.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;EMAIL_USE_TLS = True&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;EMAIL_HOST = "smtp.gmail.com"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;EMAIL_HOST_USER = "your_name@example.com"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;EMAIL_HOST_PASSWORD = "your_password"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;EMAIL_PORT = 587&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
The port number 587 is pretty standard. Also, it is very important to set TLS to &lt;b&gt;True&lt;/b&gt;. We'll go into why later.&lt;br /&gt;
You have to enter your Gmail password there, but I assure you it is secure. Only the programmer(s) can see it. You may also create another common account and use those credentials instead.&lt;br /&gt;
Ok, now we'll need to write the Python code to send emails.&lt;br /&gt;
Open the &lt;b&gt;views.py&lt;/b&gt;&amp;nbsp;file in the appropriate app and write the following code:&lt;br /&gt;
&lt;br /&gt;
At the top, import the Django's &lt;b&gt;send_mail&lt;/b&gt;:&lt;br /&gt;
&lt;b&gt;from django.core.mail import send_mail&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Also import &lt;b&gt;settings.py:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;from django.conf import settings&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Later, create this function:&lt;br /&gt;
&lt;b&gt;def foo (request):&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; title = "some title"&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;b&gt;message = "some message"&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; host = settings.EMAIL_HOST_USER&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; receiver = ["someone@example.com"]&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; send_mail (title, message, host, receiver, fail_silently = False)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; return HttpResponse ("Mail sent successfully")&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Some important things to note here:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;You can directly write send_mail () and pass all the above values as arguments. I just wrote them separately so it's easier to understand.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;receiver &lt;/b&gt;variable has to be a list or tuple, as Django supports sending emails to multiple people (a.k.a. multicasting).&lt;/li&gt;
&lt;li&gt;&lt;b&gt;fail_silently = False &lt;/b&gt;enables you to see what error was generated in case the email wasn't successfully sent. You can then use error handling to do stuff for each error. Or if the mail you're sending is not important (or an advertisement, 'cause people hate that), you can set it to &lt;b&gt;True&lt;/b&gt;&amp;nbsp;and set your mind at ease.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
That's all for now! I just covered the basics here, there's a lot you can do with exceptions, responses etc. If you have questions, or are getting some error you can't identify the solution for, please feel free to ask in the comments.&lt;/div&gt;
&lt;div&gt;
Cheers!&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/sending-email.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2967116530949647538.post-4804415428039587234</guid><pubDate>Tue, 06 May 2014 06:32:00 +0000</pubDate><atom:updated>2014-05-07T00:23:40.988-07:00</atom:updated><title>Github</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Hey guys! For those of you who don't know, Github is a code sharing site on which you can review and edit other people's code and post your own. I have shown you a lot of code on this blog. Some of the code like file upload, login sessions etc is the same and can be reused. So I'm going to post such pieces of code on Github for you guys to review and edit. Please do so, I would be very happy if someone improved upon the code I've written.&lt;br /&gt;
Also, my favorite language is Python and I do a lot of other things like software development, hacking in it. I've posted repos for these also. Please feel free to review and edit those too.&lt;br /&gt;
&lt;br /&gt;
Here's the link:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;https://github.com/AgentK1729&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
Cheers!&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thedjangoblog.blogspot.com/2014/05/github.html</link><author>noreply@blogger.com (Tejas Sathe)</author><thr:total>0</thr:total></item></channel></rss>