• Aucun résultat trouvé

Making It Generic

Dans le document Pro Django (Page 145-148)

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

From there, the typical workflow steps still apply, such as checking the validity of the input data and taking the appropriate steps that are specific to the application’s functionality.

Once this is all rolled up together in a view, it looks something like this:

bnki`f]jckeilknpdppl

bnki`f]jck*odknp_qpoeilknpnaj`an[pk[naolkjoa bnki`f]jck*pailh]pa*_kjpatpeilknpNamqaop?kjpatp bnkilnklanpeaoeilknpbknio

`abi]ga[kbban$namqaop(e`(pailh]pa[j]ia9##(bkni[d]od9Jkja%6 ebnamqaop*iapdk`99#LKOP#6

bkni9bknio*Kbban$namqaop*LKOP%

eb#laj`#ejnamqaop*LKOP6 bkni[d]od9bkni*laj`$%

napqnjdppl*DpplNa`ena_p$bkni[d]od%

ahoa6

ebbkni*eo[r]he`$%6

Pdeoeosdana]_pq]hlnk_aooejcskqh`p]galh]_a ahoa6

ebbkni[d]od6

bkni9bknio*Kbban*naoqia$bkni[d]od%

ahoa6

bkni9bknio*Kbban$%

napqnjnaj`an[pk[naolkjoa$pailh]pa[j]ia(w#bkni#6bkniy(

_kjpatp[ejop]j_a9Namqaop?kjpatp$namqaop%%

There’s a lot going on here, but very little of it has anything to do with making an offer on a house. The vast majority of that code exists solely to manage all the different states the form could be in at any given time, and would have to be repeated every time a view uses a Laj`Bkni subclass, and that’s not efficient.

Making It Generic

While it’s easy to see which aspects of the view are repetitive, and should thus be factored out into something reusable, it’s a bit trickier to decide how to do so. The main issue is that the portion of the code that’s specific to this particular view isn’t just a string or a number, like has been shown in most of the previous examples, but rather a block of code.

This is something of a problem, because previous examples had shown how generic views can be used to factor out commonalities, while allowing specific differences to be specified in a URL pattern. That works well for basic data types, such as strings, numbers, sequences and dictionaries, but code is handled differently. Instead of being able to just specify the value inline in the URL pat-tern, this code must be defined in a separate function, which is then passed in to the pattern.

While that’s certainly possible, it makes the URL configuration module a bit more cumber-some, given that there might be a number of top- level functions declared above each block of URL patterns. Lambda- style functions could be a way around this, but since they’re restricted to executing simple expressions, with no loops or conditions, they’d severely limit the type of code that could be used.

One alternative is a decorator, which could be applied to a standard function, providing all of the necessary functionality in a wrapper. This way, any function can be used to contain the code that will actually process the form, with the full capabilities of Python at its disposal.

That code also wouldn’t have to deal with any of the boilerplate necessary to pend or resume the form, because the decorator could do all that before the view code itself even executes, simply passing in a form as an argument. Here’s how the previous view could look, if a decora-tor was used to remove the boilerplate.

bnkilaj`[bknio*`a_kn]pknoeilknplaj`[bkni

<laj`[bkni

`abi]ga[kbban$namqaop(e`(bkni%6

Pdeoeosdana]_pq]hlnk_aooejcskqh`p]galh]_a

Now all that’s left is to write the decorator itself, encapsulating the functionality that was removed from the previous example, wrapping it around a view that would be passed in. This would be placed in a new `a_kn]pkno*lu module.

bnki`f]jckeilknpdppl

bnki`f]jck*odknp_qpoeilknpnaj`an[pk[naolkjoa bnki`f]jck*pailh]pa*_kjpatpeilknpNamqaop?kjpatp bnki`f]jck*qpeho*bqj_pekj]heilknpsn]lo

`ablaj`[bkni$reas%6

`absn]llan$namqaop(bkni[_h]oo(pailh]pa[j]ia(

bkni[d]od9Jkja(&]nco(&&gs]nco%6 ebnamqaop*iapdk`99#LKOP#6

bkni9bkni[_h]oo$namqaop*LKOP%

eb#laj`#ejnamqaop*LKOP6 bkni[d]od9bkni*laj`$%

napqnjdppl*DpplNa`ena_p$bkni[d]od%

ahoa6

ebbkni*eo[r]he`$%6

napqnjreas$namqaop(bkni9bkni(&]nco(&&gs]nco%

ahoa6

ebbkni[d]od6

bkni9bkni[_h]oo*naoqia$bkni[d]od%

ahoa6

bkni9bkni[_h]oo$%

napqnjnaj`an[pk[naolkjoa$pailh]pa[j]ia(w#bkni#6bkniy(

_kjpatp[ejop]j_a9Namqaop?kjpatp$namqaop%%

napqnjsn]lo$reas%$sn]llan%

Now, all that’s necessary is to set up a URL configuration that provides both a form class and a template name. This decorator will handle the rest, only calling the view when the form has been completed and submitted for processing.

Now What?

In order to be truly useful in the real world, forms must be presented to users as part of an HTML page. Rather than trying to generate that HTML content directly inside Python code, Django provides templates as a more designer- friendly alternative.

133

Templates

W

hile Chapter 2 made it clear that Django is built entirely on Python, and standard Python rules apply, templates are the exception to the rule. Templates are Django’s provided way of generating text- based output, such as HTML or emails, where the people editing those docu-ments may not have any experience with Python. Therefore, templates are designed to avoid using Python directly, instead favoring an extensible, easy-to- use custom language built just for Django.

By disallowing arbitrary Python expressions, templates are certainly restricted in some ways, but there are two things to keep in mind. First, the template system is backed by Python, just like everything else in Django, so it’s always possible to add Python- level code for specific features. It’s just bad form to include the actual Python code in the template itself, so Django provides other means for plugging in that extra code.

More importantly, drawing a clear line between templates and the Python code that pow-ers them allows for two separate groups of people, with different backgrounds and skill sets, to work together. For many hobbyist projects, this probably sounds like a waste, since the only people working on the site are developers. In many commercial environments, however, developers are often a separate group of people from those tasked with maintaining the site’s content and visual structure.

By clearly separating the tasks of development and template editing, it’s easy to set up an environment where developers work on the things that they’re really needed for, while content editors and designers can work on things that don’t really require development experience.

Django’s templates are fairly simple in nature, and easy to pick up by most anyone, even those without any programming experience.

The basic details of what the template syntax looks like and the included tags and filters are well described elsewhere. Instead of focusing on those higher- level details, this chap-ter will cover how templates are loaded, parsed and rendered, how variables are managed within a template and how new tags and filters can be created. Essentially, it’s all about what developers can do to make life as easy as possible for their content editing counterparts.

Dans le document Pro Django (Page 145-148)