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

Revert to pre-spam
imported>Paulproteus
(Revert to pre-spam)
 
(3 intermediate revisions by 3 users not shown)
Line 38:
cd workshop_mysite
 
=== Look at the files ===
Umm, are you rlelay just giving this info out for nothing?
 
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 ===
Line 631 ⟶ 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 905 ⟶ 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