Boston Python Workshop/Saturday/Web app project: Difference between revisions

Revert to pre-spam
imported>Paulproteus
(Revert to pre-spam)
 
(2 intermediate revisions by 2 users not shown)
Line 11:
This page should say what you should actually expect to know. It is okay that you don't understand everything you are typing in. After a lot more learning, you will be able to. The first time, though, it's okay if you don't. Will and Katie have feedback for this page.
 
== Writing your first Django app, part 1 ==
Free knolwgdee like this doesn't just help, it promote democracy. Thank you.
 
Let’s learn by example.
 
This tutorial walks you through the creation of a basic poll application.
 
It’ll consist of two parts:
 
* A public site that lets people view polls and vote in them.
* An admin site that lets you add, change and delete polls.
 
=== Switch to the right directory ===
 
* In a terminal (or GitBash), get into the django_projects directory we created in the Friday setup portion of the tutorial. You can do that by typing this into your terminal:
 
cd Desktop
cd django_projects
 
In the Friday setup portion of the workshop, you already saw how to use the ''django-admin.py'' command to start a project. The workshop coordinators already created a project, and you already ''fork''ed it on Github. So now, you'll clone that to your computer.
 
* Go to http://github.com/
* Find your clone of workshop_mysite. Find the '''SSH''' URL for it, and copy that to the clipboard.
* In the terminal, type: <tt>git clone</tt> followed by the URL for your personal fork of the workshop_mysite repository.
* Make sure you can "cd" into it:
 
cd workshop_mysite
 
=== Look at the files ===
 
Let’s look at files in the project:
 
workshop_mysite/
public/
README.mediawiki
__init__.py
manage.py
settings.py
urls.py
 
These files are:
 
* README.mediawiki: Many projects come with ''README'' files that, well, you should read. This one does, too.
* public/: This directory contains files the instructors put together so you can easily deploy your web app to Alwaysdata.com.
* __init__.py: An empty file that tells Python that this directory should be considered a Python module. Because of the __init__.py file, you can use ''import'' to ''import workshop_mysite''.
* manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin.py and manage.py.
* settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
* urls.py: The URL declarations for this Django project; a "table of contents" of your Django-powered site. You can read more about URLs in URL dispatcher.
 
=== The development server ===
 
Let's verify this worked. Run the command:
 
<pre>
python manage.py runserver
</pre>
 
You'll see the following output on the command line:
 
<pre>
Validating models...
0 errors found.
 
Django version 1.2, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
</pre>
 
You've started the Django development server, a lightweight Web server written purely in Python. The Django maintainers include this web server, but on a "deployment" like alwaysdata.com, you typically tie Django into an existing server like Apache.
 
Now that the server's running, visit http://127.0.0.1:8000/ with your Web browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel. It worked!
 
Exit the server by pressing CONTROL-C on your keyboard.
 
=== Fixing security settings ===
 
Right now, everyone in the workshop has the same '''SECRET_KEY'''. According to the Django documentation, that is bad. So open up settings.py in your editor (for example, Komodo Edit).
 
'''settings.py''' is a Python script that only contains variable definitions. (Django looks at the values of these variables when it runs your web app.)
 
Find the variable named <tt>SECRET_KEY</tt> and set it to whatever string you want. Go on, we'll wait.
 
=== Database setup ===
 
Keep looking at settings.py: The DATABASES variable is a dictionary with one key: '''default'''.
 
The value is itself another dictionary with information about the site's default database. You can see from the ''NAME'' that the Django project uses a file called ''database.db'' to store information.
 
'''Pop quiz''': Does database.db exist right now?
 
While you're editing settings.py, take note of the INSTALLED_APPS setting towards the bottom of the file. That variable holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.
 
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
 
* django.contrib.auth -- An authentication system.
* django.contrib.contenttypes -- A framework for content types.
* django.contrib.sessions -- A session framework.
* django.contrib.sites -- A framework for managing multiple sites with one Django installation.
* django.contrib.messages -- A messaging framework.
 
These applications are included by default as a convenience.
 
Each of these applications makes use of at least one database table, so we need to create the tables in the database before we can use them. To do that, run the following command:
 
python manage.py syncdb
 
The syncdb command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your settings.py file. You'll see a message for each database table it creates, and you'll get a prompt asking you if you'd like to create a superuser account for the authentication system. Go ahead and do that.
 
== Part 1.5: Creating polls ==
Line 545 ⟶ 650:
Load the new detail page in your browser: http://127.0.0.1:8000/polls/1/ The poll choices now appear.
 
=== Adding some style ===
Articles like this are an example of quick, helpful asnewrs.
 
The web page looks okay, but it is somewhat drab.
 
FIXME: CSS
 
== Part 3: Let people vote ==
Line 819 ⟶ 928:
It works like this: There are three slots for related Choices -- as specified by extra -- and each time you come back to the "Change" page for an already-created object, you get another three extra slots.
 
=== Customize the admin change list ===
Yeah that's what I'm tlaikng about baby--nice work!
 
Now that the Poll admin page is looking good, let's make some tweaks to the admin "change list" page -- the one that displays all the polls in the system.
 
By default, Django displays the str() of each object. But sometimes it'd be more helpful if we could display individual fields. To do that, use the list_display admin option, which is a tuple of field names to display, as columns, on the change list page for the object:
 
<pre>
class PollAdmin(admin.ModelAdmin):
# ...
list_display = ('question', 'pub_date')
</pre>
 
Just for good measure, let's also include the was_published_today custom method from Tutorial 1:
 
<pre>
class PollAdmin(admin.ModelAdmin):
# ...
list_display = ('question', 'pub_date', 'was_published_today')
</pre>
 
Now, check out the polls list.
 
You can click on the column headers to sort by those values -- except in the case of the was_published_today header, because sorting by the output of an arbitrary method is not supported. Also note that the column header for was_published_today is, by default, the name of the method (with underscores replaced with spaces).
 
This is shaping up well. Let's add some search capability. Add this to '''class PollAdmin''':
class PollAdmin(admin.ModelAdmin):
# ...
search_fields = ['question']
 
That adds a search box at the top of the change list. When somebody enters search terms, Django will search the question field. You can use as many fields as you'd like -- although because it uses a LIKE query behind the scenes, keep it reasonable, to keep your database happy.
 
Finally, because Poll objects have dates, it'd be convenient to be able to drill down by date. Add this line:
 
class PollAdmin(admin.ModelAdmin):
# ...
date_hierarchy = 'pub_date'
 
That adds hierarchical navigation, by date, to the top of the change list page. At top level, it displays all available years. Then it drills down to months and, ultimately, days.
 
That's the basics of the Django admin interface!
 
Create a poll! Create some choices. Find your views, and show them to the world.
 
== Part 4.5: Deploy again, again! ==
Anonymous user