DjangoCon 2015

Las vistas de Django: funciones, clases y programación genérica

Andrew Pinkham  · 


Extracto de la transcripción automática del vídeo realizada por YouTube.

so as just mentioned my name is Andrew I'm a freelance software consultant and also a technical instructor I teach Django in corporate and startup settings I have a class coming up at the end of September which I'll come back to because perhaps most

interestingly isn't also mentioned I'm the author of Django and leash the book is currently available for pre-order pre-order on Amazon and Pearson's hedging their bets they're saying that it's going to ship in December we sort of expected

to actually ship at the very end of November but you didn't hear that from me so the goal for today is to make it to that by the end of this talk if you currently can't answer some of these questions I want to make it so that you look at these questions

you can go oh you know what I got this I know exactly how to answer these questions and so the goal is to make it really easy to get through all of these will actually come back to the questions themselves the talk itself is split into two three totally unequal

parts we're going to start at the very beginning with the largest section and it's essentially trying to define the problem and understand the high-level concepts that allow for the solution we're then going to actually examine the solutions and

possible solutions and see how to interact with them and then finally the smallest section is going to be a little bit about the tools that might help you ease the the solution process the fundamentals right the problem solution we're going to look at

Python cobbles we're gonna examine HTTP and this is going to allow us to step back and look at django views from a very high-level perspective then we'll actually look at views we'll start with a little bit of a history and then we'll look

at each type of view available in django at the moment and finally we will enhance we'll look at a couple options that might be useful depending on what your choices are um I typically like to provide slides and code and eventually I will sit down and

write an article with all of this and based on feedback and so you can find all of that material at this link currently the slides are up online so if you want to follow along you can do that and all of the code that will be appearing including an eye Python

notebook which you can fool around with is currently posted on github so we're going to dive straight into it let's talk about Python callable z' um you're used to seeing Python calls all the time right the function is sort of the most basic

it takes an input it gives you output this is this might seem like okay Andrew why are you talking about this this is really really basic but I want to make sure we're all on the same page for maybe some of them the the more advanced stuff python comes

with anonymous functions which allows you to sort of define them on the fly they're interesting but not really the point here it should come as no surprise that methods are also callable right a method takes input and it provides output may be a little

more surprisingly however is the fact that you can see that when I instantiate the class I'm calling the class and that is in fact I am calling it it makes it a callable so some people in the room will go oh yes well of course there's the dunder init

method and we use that to make it so that a class is called that's where we control initialization yes it's a lit that if you look at the the Python C code it's a little bit more complicated than that but that is in fact yes what makes a class

callable and you'll say great so we have we have functions and we have methods and actually turns out we can also make it so that on top of functions methods and classes you can make objects callable with the dunder callable method so what that means is

that you have virtually everything in Python that can be a callable given certain requirements right and you can see here I simply asked Python hey can I call this right so now that we all are really really clear on what a call we can move into the rest of

the talk and we're going to start with HTTP you you've surely seen books about this articles about this presentations about this and and we could we could sort of start we go yes well it's it stands for this it defines how a client and a server

communicate right the client will issue a request method the server does whatever it needs to and the server is going to return a response and at that point we could begin to talk about the the different methods available and we could go through and we outline

all the ones that are important we can then get into a real battle about post input and the meaning of idempotent methods and I can stand up here and I can really go through this and you know ye shall not and whatever goes but that really misses the point

for us and the point is this your website must adhere to HTTP when you were building a website the problem you are trying to solve is adherence to HTTP right so if I go and build a server and I have the following request methods and then I define the following

response codes not only will my web server not work but I have created a monster and you really need to pull me aside and have a word with me so I'm going to you know really emphasize the fact that when you are building a website your goal first and foremost

is to adhere to HTTP now I'm also going to assert a little more baselessly that the goal of all software is to solve a problem or to automate behavior and I admit look sometimes it's really not clear which of the two things that's doing but I'm

still going to tell you yes I am absolutely correct on this you will take my word for it um that means that in the context of HTTP and Jango HTTP is the central problem that the web frame that any web framework must solve right it is it is everything else

is a nicety it's there to help you but as a website is defined as just something that adheres to the HTTP spec then that is the first problem it must fix and it also means that your the behavior it's automating it's trying to make it so that you

have to do the least amount of work possible to adhere to this which allows us to move directly into Django I don't know how many people here have not done the Django tutorial who here's really just getting started okay so I want to I'm glad to

see that I just want to talk real quick about what a view is when you do the tutorial you're gonna see that there's some text it says if you as a type of webpage Russell Keith McGee two years ago Django Khan said a views are for displaying content

I'm gonna go so far as to say that it's dynamic generation of content right the Washington Post is a collection of web pages some of these web pages are similar right there are a whole bunch of articles on the web site and they all meet the following

structure right the articles are all the same thing and so you typically if you were building this in Django would have a view for the articles right it becomes a type of web page so you have your user show up and what's going to happen is they're

going to that oh yes they're going to issue you an HTTP request the website is going to compute and it's going to get a response back this is how every website works and we're interested in the internals in Django you're gonna start by building

[ ... ]

Nota: se han omitido las otras 3.677 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.