== Dev Week -- Django -- DaveWalker -- Mon, Jan 25 == UTC {{{#!IRC [18:01] thank you sir [18:01] next up is Dave Walker who will talk about a fantastic piece of software to write (web) software: Django! [18:01] Hello everyone, and thanks for being here! [18:01] I'm happy to have questions as they come, so if you have a question - please feel free to interupt me [18:02] via QUESTION: Some question ---> in #ubuntu-classroom-chat [18:02] (hope that makes sense) [18:02] I'll start. [18:02] Django is a "Web Framework".. I'm sure many of you have an idea what this is [18:02] * dholbach hugs Daviey [18:03] However, some people (including myself) were totally baffled when we first tried to start with it [18:03] In this session, i hope to give a good introduction and allow everyone to make thier first app [18:03] Django has a slogan of "The Webframework for perfectionists with deadlines" [18:04] This is kinda catchy :) [18:04] If we try to start a project, hopefully people can follow. [18:04] I'll explain what each part is inside the "project" [18:04] many people install django from the repositories via "sudo apt-get install python-django" [18:05] However, many people feel that it doesn't provide a new enough version - so they install it other ways [18:05] I'll get more onto that later. [18:05] Once it is installed, we have an extra app that we can run. [18:05] If you installed via the repositories it is called django-admin [18:05] If you installed via source it is called django-admin.py [18:06] If we try and create a very simple blog i wold od [18:06] dave@boogie:~/django$ django-admin startproject blogsite [18:07] this then creates the following: [18:07] dave@boogie:~/django/blogsite$ ls [18:07] __init__.py manage.py settings.py urls.py [18:07] This is the basis of our application [18:07] manage.py is our application we use to RUN certain commands [18:07] so it's the only executable we will use from now on [18:08] The default settings file called, settings.py is here http://pastebin.com/f4533df0f [18:08] As you can see it's very well documented. [18:09] The database backends provide abstraction, so we really don't care what the actual database type is [18:09] it could be sqlite3, mysql, postgres (my fav.) or oracle [18:09] We won't need to care about SQL commands, that are normally associated with web applications [18:09] urls.py is here, http://pastebin.com/f64110653 [18:10] It allos us to describe via a regex where to direct http requests [18:10] Everyone following ok? [18:10] < Navaneeth> QUESTION: If we don't care about SQL statements, how specific optimizations on SQL can be applied? [18:11] Navaneeth: Good question.. Because everything is an object, if we code our applications in a sane way it should be quite optimal in the SQL queries [18:11] you can manually run sql commands, but you often don't need to [18:11] < n3rd> Question:manage.py is our application we use to RUN certain commands..like? [18:12] django can also cache the requests to mean that it doesn't need to do the same query multiple times [18:12] n3rd: that comes next :) [18:12] dave@boogie:~/django/blogsite$ ./manage.py startapp blog [18:13] So what we have done now is create a app inside our project called blog [18:13] dave@boogie:~/django/blogsite/blog$ ls [18:13] __init__.py models.py tests.py views.py [18:13] That stuff was all included automatically [18:13] but before i explain what each of those does, i would like to show you all the admin interface [18:14] dave@boogie:~/django/blogsite$ ./manage.py syncdb [18:14] This will create all our database tables that we require [18:14] As i've only enabled the admin, via adding "django.contrib.admin" to the INSTALLED_APPS section of settings.py [18:14] it will only create that [18:15] FLOOD: [18:15] dave@boogie:~/django/blogsite$ ./manage.py syncdb [18:15] Creating table django_admin_log [18:15] Creating table auth_permission [18:15] Creating table auth_group [18:15] Creating table auth_user [18:15] Creating table auth_message [18:15] Question : got this error : django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet. [18:15] Creating table django_content_type [18:15] Creating table django_session [18:15] Creating table django_site [18:15] You just installed Django's auth system, which means you don't have any superusers defined. [18:15] Would you like to create one now? (yes/no): yes [18:15] Username (Leave blank to use 'dave'): [18:15] E-mail address: davewalker@ubuntu.com [18:15] Password: [18:15] Password (again): [18:15] Superuser created successfully. [18:15] Installing index for admin.LogEntry model [18:15] Installing index for auth.Permission model [18:15] Installing index for auth.Message model [18:16] As you can see, manage.py created the database tables [18:16] Okay, i did add one more change [18:16] in settings.py add: [18:16] DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. [18:16] DATABASE_NAME = 'database.sql3' # Or path to database file if using sqlite3. [18:16] That will mean it will use sqlite3, and a file called database.sql3 [18:16] okay? [18:17] so whoever that was, now run syncdb [18:17] now if i do: [18:17] ./manage.py runserver [18:17] I get an development webserver built in for free! [18:18] http://ubuntu.dev.week.daviey.com:8000/ [18:18] However, there isn't a whole lot going on there (yet) [18:18] feel free to have a look [18:18] Okay, now back to the blog app we started [18:19] dave@boogie:~/django/blogsite/blog$ ls [18:19] __init__.py models.py tests.py views.py [18:19] __init__.py - runs when the app is loaded [18:19] it's empty by default, and tends to stay that way [18:19] models.py - This is where we describe our database structure. You can do much more [18:20] But for this, we will limit it to simply describing your database as objects [18:20] http://pastebin.com/f295abd14 <-- pretty much empty [18:20] We have our views.py - http://pastebin.com/f50aad6cb [18:21] This is how we describe how our data is shown to the user. [18:21] lets start putting some content in there [18:22] http://pastebin.com/f5d4145da <-- that is the contents of my models.py [18:22] Now, we need to tell the project we are actually using it. [18:22] If everyone doing ok so far? [18:22] ok [18:23] dave@boogie:~/django/blogsite$ nano settings.py [18:23] I'm adding " 'blogsite.blog', [18:23] " [18:23] inside the INSTALLED_APPS section. [18:23] < tiemonster> QUESTION: the admin.site.register doesn't necessarily have to go in an admin.py? [18:24] tiemonster: hmm, you could put it in __init__.py [18:24] but it makes sense to put it in the admin.py [18:24] admin.py is a fairly new feature that was introduced in 1.0 iirc.. [18:24] anyway, we don't need to cover that atm [18:25] KISS :) [18:25] < tiemonster> QUESTION: So it doesn't matter where you declare the admin.site.register? [18:25] I'll wait for the netsplit to finish before continuing [18:26] Okay, i think i will continue [18:27] tiemonster: technically it doesn't matter where you register it [18:27] All it's doing is telling the admin interface to be aware of these objects [18:27] i tend to do it in models.py i think [18:27] but you could put it anywhere that is aware of the model [18:27] dave@boogie:~/django/blogsite$ ./manage.py syncdb [18:27] Creating table blog_blogpost [18:27] Installing index for blog.Blogpost model [18:28] As you can see, the blog object has been created in the database [18:28] you can't see this at the moment, so i'll open up the web interface [18:28] in order to enable the worst kept secret of django.. we need to open urls.py [18:28] we need to uncomment: [18:29] # from django.contrib import admin [18:29] # admin.autodiscover() [18:29] (remove the #) [18:29] and also from the # (r'^admin/', include(admin.site.urls)), [18:29] http://pastebin.com/f24559b5f <-- like so [18:29] now, if people want to log into the admin interface and have a look - log in here [18:29] http://ubuntu.dev.week.daviey.com:8000/admin/ [18:30] I might add, that i hope i can trust you all not to abuse this :) [18:30] the username is: dave [18:30] and the password is: password [18:30] (secure huh?) [18:30] < SmartSsa> Question: I'm seeing some differences in what my django is creating vs. what you're showing. What version are you using and how can I find out what version I'm using? [18:30] i set that when i first sync'db [18:31] SmartSsa: What differences are you seeing? [18:31] dave@boogie:~/django/blogsite$ apt-cache show python-django | grep Version [18:31] Version: 1.1.1-1ubuntu1 [18:31] That is the one i have installed on this box [18:32] SmartSsa: if you can list your differences, we'll come back to it if that is ok? [18:32] < SmartSsa> Daviey, I don't have tests.py or the line with 'admin.site.urls' .. i have 'admin.site.root in my urls.py file. [18:32] cjohnston: Ah, that was a change with 1.1 i believe [18:33] < Navaneeth> QUESTION: Django has support for running on a server farm? Like sharing session etc.. [18:33] Are people seeing the webinterface? [18:35] Okay, i'm running that under the dev server which isn't designed for large use.. so perhaps it may be falling over slightly [18:36] However, you will see the "Blog" application listed there [18:36] feel free to "Add blogpost" [18:36] that luiX_ [18:36] thanks* [18:37] okay.. hopefully everyone can see the potential of the admin interface [18:38] Now we need a way to show that to a user, who isn't authenticated [18:38] we do this via a "view" [18:38] so in "blogsite/blog/views.py" [18:38] I'm going to put in a function [18:39] < luiX_> QUESTION: Can you filter what apps are showed up to each user or profile in the admin page? [18:39] http://pastebin.com/f19ccde62 [18:39] luiX_: yes, you can [18:40] so as you can see, if the function posts() gets called [18:40] posts = Blogpost.objects.all() [18:40] Which under the wrappers does a SELECT * FROM blogpost, for example [18:40] it then pushes the data to a html file that is created called posts.html [18:41] However, first we need to let the app know how to send a HTTP request there [18:41] nano blogsite/blog/urls.py [18:42] http://pastebin.com/f34534c6d <--- [18:42] Everyone following ok? [18:42] ok [18:43] so, posts.html is a "template" [18:43] so lets create a template folder [18:43] dave@boogie:~/django/blogsite$ mkdir templates [18:43] and we need to tell the settings where this is [18:44] TEMPLATE_DIRS needs to have something added [18:44] to keep things simple: [18:44] TEMPLATE_DIRS = ( [18:44] # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". [18:44] # Always use forward slashes, even on Windows. [18:44] # Don't forget to use absolute paths, not relative paths. [18:44] '/home/dave/django/blogsite/templates/', [18:44] ) [18:44] (you can do clever things such as working from current postion + 'templates' [18:44] but for this we'll keep it simple [18:44] adjust the following to what suites your dir layout [18:46] dave@boogie:~/django/blogsite$ nano templates/posts.html [18:46] http://pastebin.com/f6a0156a4 [18:46] okay, everyone following ok? [18:47] Now, lets add an entry to "blogsite/urls.py [18:47] (r'^', include('blogsite.blog.urls')), <-- [18:47] add it above or below the admin one: [18:47] (r'^admin/', include(admin.site.urls)), [18:48] http://pastebin.com/f6f95c0b [18:48] ^^ urls.py [18:48] Now, if we do our runserver again [18:48] ./manage.py runserver [18:48] we should see: http://ubuntu.dev.week.daviey.com:8000/ [18:49] We have made a simple blog \o/ [18:49] we can add and remove these via http://ubuntu.dev.week.daviey.com:8000/admin/blog/blogpost/ [18:49] I hope everyone is keeping up okay [18:50] I won't touch on theming today, or any other stuff like that.. [18:50] if we look at our templates/posts.html [18:50] we can see there is a mini language in itself [18:50] {% for post in posts %} [18:50] for loop [18:51] post.title , post.content <-- this is the models.py we described earlier [18:51] Everything is an object! [18:51] Some of the things that makes django particualry cool for us ubuntu folk, is some of the awesome apps we already have [18:52] It is VERY easy to add openid auth support that uses launchpad for signing in. [18:52] You can see examples of this in UbuntuOne web interface, UDS planner, and the community created project of http://loco.ubuntu.com [18:53] Other things that are exciting to us, is that it's python - so we can tie into bzr and things like the launchpad api [18:53] menaing oru web applicationc an do real time cool stuff. [18:53] our* [18:53] I was going to cover django-auth-openid, but i somehow feel that adding things like that right now, might cause people to explode :) [18:54] Other cool things are deployment methods, such as using fabric, virtualenv and pip [18:54] Other ways of installing django include pip, this is like easy_install or for the perl types cpan [18:55] It installs python things, but not necessarily in a clean place [18:55] < strycore66> QUESTION : Any good book on Django to suggest ? [18:55] strycore66: THE best book i would recommend is http://docs.djangoproject.com/en/1.1/ [18:56] the documentation is updated so regulary and there are many tutorials [18:56] http://www.djangobook.com/ <-- is another good source [18:56] < SmartSsa> Question: What's the "Sites" section for in the admin? [18:56] ^^ that is a real print book, avaliable for no cost online [18:57] SmartSsa: We can tie projects to a specific site and use that to make a difference. [18:57] Ie, we could have had 15 different blog sites going there [18:57] in this instance, we didn't use it [18:57] Shall i just fall back to questions now, and perhaps have a more advance session another time? [18:57] Questions?! [18:58] < n3rd> QUESTION :How can we use web2.0 like features with this framework [18:58] n3rd: Django won't make an app look web2.0-y on it's own, you do still need css and javascript.. django can help, but it does help if you have an arty influcence [18:58] (i odn't) [18:58] don't* [18:58] sadly [18:59] things like plugging into twitter are also easy! [18:59] next? [18:59] That's all that I have for now Daviey [18:59] okay, i'll end it there [18:59] but! [18:59] loco directory (loco.ubuntu.com) is open source [19:00] If you want to come and help, the water is warm [19:00] jump in a #ubuntu-locoteam [19:00] at* [19:00] Thanks all for listening [19:00] Next we have kees_lernid for an exciting, and cool subject! [19:00] Daviey: #ubuntu-locoteams ? [19:00] cjohnston: my mistake, yes [19:01] :-) }}}