Django

Dev Week -- Django -- DaveWalker -- Mon, Jan 25

UTC

   1 [18:01] <Daviey> thank you sir
   2 [18:01] <dholbach> next up is Dave Walker who will talk about a fantastic piece of software to write (web) software: Django!
   3 [18:01] <Daviey> Hello everyone, and thanks for being here!
   4 [18:01] <Daviey> I'm happy to have questions as they come, so if you have a question - please feel free to interupt me
   5 [18:02] <Daviey> via QUESTION: Some question ---> in #ubuntu-classroom-chat
   6 [18:02] <Daviey> (hope that makes sense)
   7 [18:02] <Daviey> I'll start.
   8 [18:02] <Daviey> Django is a "Web Framework".. I'm sure many of you have an idea what this is
   9 [18:02]  * dholbach hugs Daviey
  10 [18:03] <Daviey> However, some people (including myself) were totally baffled when we first tried to start with it
  11 [18:03] <Daviey> In this session, i hope to give a good introduction and allow everyone to make thier first app
  12 [18:03] <Daviey> Django has a slogan of "The Webframework for perfectionists with deadlines"
  13 [18:04] <Daviey> This is kinda catchy :)
  14 [18:04] <Daviey> If we try to start a project, hopefully people can follow.
  15 [18:04] <Daviey> I'll explain what each part is inside the "project"
  16 [18:04] <Daviey> many people install django from the repositories via "sudo apt-get install python-django"
  17 [18:05] <Daviey> However, many people feel that it doesn't provide a new enough version - so they install it other ways
  18 [18:05] <Daviey> I'll get more onto that later.
  19 [18:05] <Daviey> Once it is installed, we have an extra app that we can run.
  20 [18:05] <Daviey> If you installed via the repositories it is called django-admin
  21 [18:05] <Daviey> If you installed via source it is called django-admin.py
  22 [18:06] <Daviey> If we try and create a very simple blog i wold od
  23 [18:06] <Daviey> dave@boogie:~/django$ django-admin startproject blogsite
  24 [18:07] <Daviey> this then creates the following:
  25 [18:07] <Daviey> dave@boogie:~/django/blogsite$ ls
  26 [18:07] <Daviey> __init__.py  manage.py  settings.py  urls.py
  27 [18:07] <Daviey> This is the basis of our application
  28 [18:07] <Daviey> manage.py is our application we use to RUN certain commands
  29 [18:07] <Daviey> so it's the only executable we will use from now on
  30 [18:08] <Daviey> The default settings file called, settings.py is here http://pastebin.com/f4533df0f
  31 [18:08] <Daviey> As you can see it's very well documented.
  32 [18:09] <Daviey> The database backends provide abstraction, so we really don't care what the actual database type is
  33 [18:09] <Daviey> it could be sqlite3, mysql, postgres (my fav.) or oracle
  34 [18:09] <Daviey> We won't need to care about SQL commands, that are normally associated with web applications
  35 [18:09] <Daviey> urls.py is here, http://pastebin.com/f64110653
  36 [18:10] <Daviey> It allos us to describe via a regex where to direct http requests
  37 [18:10] <Daviey> Everyone following ok?
  38 [18:10] <Pendulum>  < Navaneeth> QUESTION: If we don't care about SQL statements, how specific optimizations on SQL can be applied?
  39 [18:11] <Daviey> 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
  40 [18:11] <Daviey> you can manually run sql commands, but you often don't need to
  41 [18:11] <Pendulum> < n3rd> Question:manage.py is our application we use to RUN certain commands..like?
  42 [18:12] <Daviey> django can also cache the requests to mean that it doesn't need to do the same query multiple times
  43 [18:12] <Daviey> n3rd: that comes next :)
  44 [18:12] <Daviey> dave@boogie:~/django/blogsite$ ./manage.py startapp blog
  45 [18:13] <Daviey> So what we have done now is create a app inside our project called blog
  46 [18:13] <Daviey> dave@boogie:~/django/blogsite/blog$ ls
  47 [18:13] <Daviey> __init__.py  models.py  tests.py  views.py
  48 [18:13] <Daviey> That stuff was all included automatically
  49 [18:13] <Daviey> but before i explain what each of those does, i would like to show you all the admin interface
  50 [18:14] <Daviey> dave@boogie:~/django/blogsite$ ./manage.py syncdb
  51 [18:14] <Daviey> This will create all our database tables that we require
  52 [18:14] <Daviey> As i've only enabled the admin, via adding "django.contrib.admin" to the INSTALLED_APPS section of settings.py
  53 [18:14] <Daviey> it will only create that
  54 [18:15] <Daviey> FLOOD:
  55 [18:15] <Daviey> dave@boogie:~/django/blogsite$ ./manage.py syncdb
  56 [18:15] <Daviey> Creating table django_admin_log
  57 [18:15] <Daviey> Creating table auth_permission
  58 [18:15] <Daviey> Creating table auth_group
  59 [18:15] <Daviey> Creating table auth_user
  60 [18:15] <Daviey> Creating table auth_message
  61 [18:15] <Pendulum> Question : got this error : django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet.
  62 [18:15] <Daviey> Creating table django_content_type
  63 [18:15] <Daviey> Creating table django_session
  64 [18:15] <Daviey> Creating table django_site
  65 [18:15] <Daviey> You just installed Django's auth system, which means you don't have any superusers defined.
  66 [18:15] <Daviey> Would you like to create one now? (yes/no): yes
  67 [18:15] <Daviey> Username (Leave blank to use 'dave'):
  68 [18:15] <Daviey> E-mail address: davewalker@ubuntu.com
  69 [18:15] <Daviey> Password:
  70 [18:15] <Daviey> Password (again):
  71 [18:15] <Daviey> Superuser created successfully.
  72 [18:15] <Daviey> Installing index for admin.LogEntry model
  73 [18:15] <Daviey> Installing index for auth.Permission model
  74 [18:15] <Daviey> Installing index for auth.Message model
  75 [18:16] <Daviey> As you can see, manage.py created the database tables
  76 [18:16] <Daviey> Okay, i did add one more change
  77 [18:16] <Daviey> in settings.py add:
  78 [18:16] <Daviey> DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  79 [18:16] <Daviey> DATABASE_NAME = 'database.sql3'             # Or path to database file if using sqlite3.
  80 [18:16] <Daviey> That will mean it will use sqlite3, and a file called database.sql3
  81 [18:16] <Daviey> okay?
  82 [18:17] <Daviey> so whoever that was, now run syncdb
  83 [18:17] <Daviey> now if i do:
  84 [18:17] <Daviey> ./manage.py runserver
  85 [18:17] <Daviey> I get an development webserver built in for free!
  86 [18:18] <Daviey> http://ubuntu.dev.week.daviey.com:8000/
  87 [18:18] <Daviey> However, there isn't a whole lot going on there (yet)
  88 [18:18] <Daviey> feel free to have a look
  89 [18:18] <Daviey> Okay, now back to the blog app we started
  90 [18:19] <Daviey> dave@boogie:~/django/blogsite/blog$ ls
  91 [18:19] <Daviey> __init__.py  models.py  tests.py  views.py
  92 [18:19] <Daviey> __init__.py - runs when the app is loaded
  93 [18:19] <Daviey> it's empty by default, and tends to stay that way
  94 [18:19] <Daviey> models.py - This is where we describe our database structure.  You can do much more
  95 [18:20] <Daviey> But for this, we will limit it to simply describing your database as objects
  96 [18:20] <Daviey> http://pastebin.com/f295abd14 <-- pretty much empty
  97 [18:20] <Daviey> We have our views.py - http://pastebin.com/f50aad6cb
  98 [18:21] <Daviey> This is how we describe how our data is shown to the user.
  99 [18:21] <Daviey> lets start putting some content in there
 100 [18:22] <Daviey> http://pastebin.com/f5d4145da <-- that is the contents of my models.py
 101 [18:22] <Daviey> Now, we need to tell the project we are actually using it.
 102 [18:22] <Daviey> If everyone doing ok so far?
 103 [18:22] <Daviey> ok
 104 [18:23] <Daviey> dave@boogie:~/django/blogsite$ nano settings.py
 105 [18:23] <Daviey> I'm adding "    'blogsite.blog',
 106 [18:23] <Daviey> "
 107 [18:23] <Daviey> inside the INSTALLED_APPS section.
 108 [18:23] <cjohnston> < tiemonster> QUESTION: the admin.site.register doesn't necessarily have to go in an admin.py?
 109 [18:24] <Daviey> tiemonster: hmm, you could put it in __init__.py
 110 [18:24] <Daviey> but it makes sense to put it in the admin.py
 111 [18:24] <Daviey> admin.py is a fairly new feature that was introduced in 1.0 iirc..
 112 [18:24] <Daviey> anyway, we don't need to cover that atm
 113 [18:25] <Daviey> KISS :)
 114 [18:25] <cjohnston>  < tiemonster> QUESTION: So it doesn't matter where you declare the admin.site.register?
 115 [18:25] <Daviey> I'll wait for the netsplit to finish before continuing
 116 [18:26] <Daviey> Okay, i think i will continue
 117 [18:27] <Daviey> tiemonster: technically it doesn't matter where you register it
 118 [18:27] <Daviey> All it's doing is telling the admin interface to be aware of these objects
 119 [18:27] <Daviey> i tend to do it in models.py i think
 120 [18:27] <Daviey> but you could put it anywhere that is aware of the model
 121 [18:27] <Daviey> dave@boogie:~/django/blogsite$ ./manage.py syncdb
 122 [18:27] <Daviey> Creating table blog_blogpost
 123 [18:27] <Daviey> Installing index for blog.Blogpost model
 124 [18:28] <Daviey> As you can see, the blog object has been created in the database
 125 [18:28] <Daviey> you can't see this at the moment, so i'll open up the web interface
 126 [18:28] <Daviey> in order to enable the worst kept secret of django.. we need to open urls.py
 127 [18:28] <Daviey> we need to uncomment:
 128 [18:29] <Daviey> # from django.contrib import admin
 129 [18:29] <Daviey> # admin.autodiscover()
 130 [18:29] <Daviey> (remove the #)
 131 [18:29] <Daviey> and also from the     # (r'^admin/', include(admin.site.urls)),
 132 [18:29] <Daviey> http://pastebin.com/f24559b5f <-- like so
 133 [18:29] <Daviey> now, if people want to log into the admin interface and have a look - log in here
 134 [18:29] <Daviey> http://ubuntu.dev.week.daviey.com:8000/admin/
 135 [18:30] <Daviey> I might add, that i hope i can trust you all not to abuse this :)
 136 [18:30] <Daviey> the username is: dave
 137 [18:30] <Daviey> and the password is: password
 138 [18:30] <Daviey> (secure huh?)
 139 [18:30] <cjohnston> < 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?
 140 [18:30] <Daviey> i set that when i first sync'db
 141 [18:31] <Daviey> SmartSsa: What differences are you seeing?
 142 [18:31] <Daviey> dave@boogie:~/django/blogsite$ apt-cache show python-django | grep Version
 143 [18:31] <Daviey> Version: 1.1.1-1ubuntu1
 144 [18:31] <Daviey> That is the one i have installed on this box
 145 [18:32] <Daviey> SmartSsa: if you can list your differences, we'll come back to it if that is ok?
 146 [18:32] <cjohnston>  < 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.
 147 [18:32] <Daviey> cjohnston: Ah, that was a change with 1.1 i believe
 148 [18:33] <cjohnston> < Navaneeth> QUESTION: Django has support for running on a server farm? Like sharing session etc..
 149 [18:33] <Daviey> Are people seeing the webinterface?
 150 [18:35] <Daviey> Okay, i'm running that under the dev server which isn't designed for large use.. so perhaps it may be falling over slightly
 151 [18:36] <Daviey> However, you will see the "Blog" application listed there
 152 [18:36] <Daviey> feel free to "Add blogpost"
 153 [18:36] <Daviey> that luiX_
 154 [18:36] <Daviey> thanks*
 155 [18:37] <Daviey> okay.. hopefully everyone can see the potential of the admin interface
 156 [18:38] <Daviey> Now we need a way to show that to a user, who isn't authenticated
 157 [18:38] <Daviey> we do this via a "view"
 158 [18:38] <Daviey> so in "blogsite/blog/views.py"
 159 [18:38] <Daviey> I'm going to put in a function
 160 [18:39] <cjohnston> < luiX_> QUESTION: Can you filter what apps are showed up to each user or profile in the admin page?
 161 [18:39] <Daviey> http://pastebin.com/f19ccde62
 162 [18:39] <Daviey> luiX_: yes, you can
 163 [18:40] <Daviey> so as you can see, if the function posts() gets called
 164 [18:40] <Daviey> posts = Blogpost.objects.all()
 165 [18:40] <Daviey> Which under the wrappers does a SELECT * FROM blogpost, for example
 166 [18:40] <Daviey> it then pushes the data to a html file that is created called posts.html
 167 [18:41] <Daviey> However, first we need to let the app know how to send a HTTP request there
 168 [18:41] <Daviey> nano blogsite/blog/urls.py
 169 [18:42] <Daviey> http://pastebin.com/f34534c6d <---
 170 [18:42] <Daviey> Everyone following ok?
 171 [18:42] <Daviey> ok
 172 [18:43] <Daviey> so, posts.html is a "template"
 173 [18:43] <Daviey> so lets create a template folder
 174 [18:43] <Daviey> dave@boogie:~/django/blogsite$ mkdir templates
 175 [18:43] <Daviey> and we need to tell the settings where this is
 176 [18:44] <Daviey> TEMPLATE_DIRS needs to have something added
 177 [18:44] <Daviey> to keep things simple:
 178 [18:44] <Daviey> TEMPLATE_DIRS = (
 179 [18:44] <Daviey>     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
 180 [18:44] <Daviey>     # Always use forward slashes, even on Windows.
 181 [18:44] <Daviey>     # Don't forget to use absolute paths, not relative paths.
 182 [18:44] <Daviey>    '/home/dave/django/blogsite/templates/',
 183 [18:44] <Daviey> )
 184 [18:44] <Daviey> (you can do clever things such as working from current postion + 'templates'
 185 [18:44] <Daviey> but for this we'll keep it simple
 186 [18:44] <Daviey> adjust the following to what suites your dir layout
 187 [18:46] <Daviey> dave@boogie:~/django/blogsite$ nano templates/posts.html
 188 [18:46] <Daviey> http://pastebin.com/f6a0156a4
 189 [18:46] <Daviey> okay, everyone following ok?
 190 [18:47] <Daviey> Now, lets add an entry to "blogsite/urls.py
 191 [18:47] <Daviey> (r'^', include('blogsite.blog.urls')), <--
 192 [18:47] <Daviey> add it above or below the admin one:
 193 [18:47] <Daviey> (r'^admin/', include(admin.site.urls)),
 194 [18:48] <Daviey> http://pastebin.com/f6f95c0b
 195 [18:48] <Daviey> ^^ urls.py
 196 [18:48] <Daviey> Now, if we do our runserver again
 197 [18:48] <Daviey> ./manage.py runserver
 198 [18:48] <Daviey> we should see: http://ubuntu.dev.week.daviey.com:8000/
 199 [18:49] <Daviey> We have made a simple blog \o/
 200 [18:49] <Daviey> we can add and remove these via http://ubuntu.dev.week.daviey.com:8000/admin/blog/blogpost/
 201 [18:49] <Daviey> I hope everyone is keeping up okay
 202 [18:50] <Daviey> I won't touch on theming today, or any other stuff like that..
 203 [18:50] <Daviey> if we look at our templates/posts.html
 204 [18:50] <Daviey> we can see there is a mini language in itself
 205 [18:50] <Daviey> {% for post in posts %}
 206 [18:50] <Daviey> for loop
 207 [18:51] <Daviey> post.title , post.content <-- this is the models.py we described earlier
 208 [18:51] <Daviey> Everything is an object!
 209 [18:51] <Daviey> Some of the things that makes django particualry cool for us ubuntu folk, is some of the awesome apps we already have
 210 [18:52] <Daviey> It is VERY easy to add openid auth support that uses launchpad for signing in.
 211 [18:52] <Daviey> You can see examples of this in UbuntuOne web interface, UDS planner, and the community created project of http://loco.ubuntu.com
 212 [18:53] <Daviey> Other things that are exciting to us, is that it's python - so we can tie into bzr and things like the launchpad api
 213 [18:53] <Daviey> menaing oru web applicationc an do real time cool stuff.
 214 [18:53] <Daviey> our*
 215 [18:53] <Daviey> I was going to cover django-auth-openid, but i somehow feel that adding things like that right now, might cause people to explode :)
 216 [18:54] <Daviey> Other cool things are deployment methods, such as using fabric, virtualenv and pip
 217 [18:54] <Daviey> Other ways of installing django include pip, this is like easy_install or for the perl types cpan
 218 [18:55] <Daviey> It installs python things, but not necessarily in a clean place
 219 [18:55] <cjohnston> < strycore66> QUESTION : Any good book on Django to suggest ?
 220 [18:55] <Daviey> strycore66: THE best book i would recommend is http://docs.djangoproject.com/en/1.1/
 221 [18:56] <Daviey> the documentation is updated so regulary and there are many tutorials
 222 [18:56] <Daviey> http://www.djangobook.com/ <-- is another good source
 223 [18:56] <cjohnston> < SmartSsa> Question: What's the "Sites" section for in the admin?
 224 [18:56] <Daviey> ^^ that is a real print book, avaliable for no cost online
 225 [18:57] <Daviey> SmartSsa: We can tie projects to a specific site and use that to make a difference.
 226 [18:57] <Daviey> Ie, we could have had 15 different blog sites going there
 227 [18:57] <Daviey> in this instance, we didn't use it
 228 [18:57] <Daviey> Shall i just fall back to questions now, and perhaps have a more advance session another time?
 229 [18:57] <Daviey> Questions?!
 230 [18:58] <cjohnston> < n3rd> QUESTION :How can we use web2.0 like features with this framework
 231 [18:58] <Daviey> 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
 232 [18:58] <Daviey> (i odn't)
 233 [18:58] <Daviey> don't*
 234 [18:58] <Daviey> sadly
 235 [18:59] <Daviey> things like plugging into twitter are also easy!
 236 [18:59] <Daviey> next?
 237 [18:59] <cjohnston> That's all that I have for now Daviey
 238 [18:59] <Daviey> okay, i'll end it there
 239 [18:59] <Daviey> but!
 240 [18:59] <Daviey> loco directory (loco.ubuntu.com) is open source
 241 [19:00] <Daviey> If you want to come and help, the water is warm
 242 [19:00] <Daviey> jump in a #ubuntu-locoteam
 243 [19:00] <Daviey> at*
 244 [19:00] <Daviey> Thanks all for listening
 245 [19:00] <Daviey> Next we have kees_lernid for an exciting, and cool subject!
 246 [19:00] <cjohnston> Daviey: #ubuntu-locoteams ?
 247 [19:00] <Daviey> cjohnston: my mistake, yes
 248 [19:01] <cjohnston> :-)
 249 

MeetingLogs/devweek1001/Django (last edited 2010-01-26 16:50:10 by dholbach)