• Aucun résultat trouvé

ADMONITION: HOW DJANGO URL CONFIGURATION WORKS

Dans le document Projects Django (Page 33-36)

Once you’ve made that change and saved your settings file, run syncdbagain:

python manage.py syncdb

You’ll see the output scroll by as Django creates database tables for the data models defined in these applications. Now, open up the urls.pyfile in your project, which—as you saw in the previous chapter—contains the root URL configuration for your project. There’s a line that says, “Uncomment this for admin:” followed by this line (the hash mark at the begin-ning indicates a Python comment and means the line will not be executed as code):

# (r'^admin/', include('django.contrib.admin.urls')),

Uncomment that line and save the file. This will add a set of URLs, included in django.contrib.admin, to your project’s URL configuration.

ADMONITION: HOW DJANGO URL CONFIGURATION WORKS

A Django URL configuration file, or URLConf, defines a list of URL patterns and indicates how they map to parts of your code. Each URL pattern has at least two parts: a regular expression that describes what the URL looks like and either a view (a Python function that can respond to HTTP requests) to map that URL to or an include, which points to a different URLConfmodule. The ability to include other URLConfmodules makes it easy to define reusable and “pluggable” sets of URLs, which can be dropped into any point in your project’s URL hierarchy.

Aregular expression, in case you’ve never encountered that term before, is a common way to represent a particular pattern of text, and most programming languages have support for checking whether a given piece of text matches the pattern specified in a regular expression. Most introductory programming books cover regular expressions.Dive Into Pythonby Mark Pilgrim (Apress, 2004) has a good chapter that covers the basics.

Also, note that regular expressions are quite strict about matching. Ordinarily, a web server will be somewhat lax and treat, for example,/adminand /admin/as the same URL, returning the same result either way. But if you specify a regular expression that ends in a slash—as I’m doing here—you must include the slash on the end when you visit that address in your browser, or the pattern will not match and you’ll get a “Page not found” error.

Now you’ll be able to launch the built-in web server again and see the administrative interface:

python manage.py runserver

The URL pattern for the admin application is ^admin/, which means that if you visit http://127.0.0.1:8000/admin/in your web browser, you’ll see the login page. Enter the user-name and password you used when syncdbprompted you to create a user account, and you’ll

see the main admin index page, as shown in Figure 2-1. But note that URLs beginning with admin/are the only ones that will work right now; you haven’t set up any other URLs yet.

Figure 2-1.Home page of the Django administrative interface

Each item listed on the index page corresponds to a data model in one of the installed applications. They’re grouped according to which application they belong to. The auth appli-cation, django.contrib.auth, provides models for users and groups; the sitesapplication, django.contrib.sites, provides a model to represent a web site; and the flatpages applica-tion you just installed provides a “flat page” model. To the right of this list is a sidebar, which will report actions you’ve taken recently in the admin interface. Since you haven’t done any-thing yet, it’s empty, but as soon as you start making changes to site content it will show a summary of your actions. As a first step, click on the Sites link. You’ll see a listing like the one shown in Figure 2-2.

As part of its initialization, django.contrib.sitescreated an example site “object” for you, which you can click to edit. Since the built-in web server is running on your computer’s local loopback interface at port 8000, change the Domain Name field to 127.0.0.1:8000, and change the Display Name field to localhost. Then click the Save button at the bottom right, and your changes will be saved to the database. If you go back to the main index of the admin interface, you’ll see the sidebar now has an entry for that site, showing that you’ve changed it recently.

Figure 2-2.The default site object created by Django

You’ll notice that the main admin page, next to each type of item, displays an Add link and a Change link; let’s add a new flat page by clicking the Add link next to the Flat Page link.

This will bring up a blank form, automatically generated from the appropriate data model.

Enter the following values:

• In the URL field, enter /first-page/.

• In the Title field, enter My first page.

• In the Content field, enter This is my first Django flat page.

Then scroll down and click the Save and Continue Editing button. The new flat page will be saved into your database, and then the form will be redisplayed so you can edit the page.

You’ll also notice that two buttons have appeared above the form: History and View on Site.

The History button will show a simplified history of this flat page (right now, nothing but the initial entry for it has been created). The View on Site button will let you see the flat page at its public URL. Click it, and you’ll be redirected to http://127.0.0.1:8000/first-page/, which will, for the moment, display an error message like the one shown in Figure 2-3.

Figure 2-3.A Django “Page not found” error

This is a 404 “Page not found” error, but with a twist—every new Django project starts out in a debugging mode, which displays more helpful error messages to help you get up and run-ning. In this case, Django shows you the URL patterns it found in your project’s URLConf, and explains that the URL you tried to visit didn’t match any of them, which makes sense because you haven’t yet added anything that looks like the URL /first-page/. So let’s fix that. Open up the urls.pyfile again and add the following line right below the one for the admin interface:

(r'', include('django.contrib.flatpages.urls')),

The pattern part of this is simply an empty string (''), which means it will actually match any URL. You could, if you wanted, go into urls.pyand add a new line each time you add a flat page. In applications you’ll develop later on, you’ll mostly be defining individual URLs, but because django.contrib.flatpageslets you specify anything for the URL of a page, it’s easiest in this case to simply place a “catch-all” URL pattern to handle it.

Dans le document Projects Django (Page 33-36)