so have the bulk of the work and the and the and the and the and and the ch so and as mentioned my name is andrew I'm a

00:21

freelance of consultants and also to the instructor I teach genuine corporate and start of settings but a question at the end of September which I come back to this perhaps most interestingly also mentioned that I am

00:34

the author of gender on which of the book is currently available corpora Europe preorder on Amazon and on Pierson said that's the same as ship of in December of while we sort of expected to actually ship at the very end of November but you don't hear that from me also the good days to

00:54

make it so that by the end of this talk if you currently can't answer some of these questions I wanna make it so that you would these questions you go 0 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 and try actually come back to the questions themselves of the talk itself is splintered 3 totally unequal parts of 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 with and actually examine the solutions and possible solutions and see how they interact with them and finally the smaller section is going to be a little bit about the tools that might help you these the the solution process under the fundamentals right the problem solutions will look at Python callable from exam http and this is going to allow us to step back and look at gender views from a very high level perspective and then actually look at views will 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 were asking enhance our we'll look at a couple options that might be useful i depending on what your choices are on 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 the material but at this link currently the slides are up on online few and follow along you can do that and of all the code that will be appearing including an IPython notebook which you can fool around with is currently posted on GitHub so we're going to dive straight into it let's talk

02:41

about the Python calls on you're used to seeing Python calls all the time

02:46

right of the function is sort of the the most basic it takes an input it gives you out this is this might seem like OK inter wire you talking about this this is really really basic but I wanna make sure all on the same page 4 maybe some of them the the more advanced stuff and the python comes with an anonymous functions of which which allows you to sort of define on fly they're interesting but not really the point here on it should come as a surprise that methods are also calls right a method takes input and provides an output of a little more surprising 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 makes it a callable and so some people in the room go yes will of course there's the Dundar method and we use that to make it so that a class is called that's where we control initialization would yes it so it turns out that if you look at the the pregnancy could it's a little bit more complicated than that but that is in fact yes what makes a class called war and say great so we have have functions and we have methods and actually turns out we can also make it so that and on top of functions methods classes you can make objects callable with the down the cold fit so that means that you have virtually everything in Python that can be a callable given certain requirements for a meeting you can see here I simply ask Python hate chemical right so now that we have a really really clear on whether Claribel is that we can move into the rest of the talk and when start with http

04:28

and you have surely seen books about this articles about this presentations about this and and we could we could

04:35

sort of start we go yes well it stands for this and it defines how a client and 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 can begin to talk about the the different methods available on and then we could go through the outline all the ones that are important but we can then get into a real battle about post input and the meaning of item input methods and I can stand up here and I can really go through

05:06

this and you know you shall not and whatever goes but that really misses the point for us in the point is this your website must

05:16

adhere to HTTP when you're building a website the problem you are trying to solve is adherence to HTTP right so I don't know the 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

05:39

monster and you really need upon me aside and have a word with me on so I'm I'm going

05:44

to this you know really emphasize the fact that when you are building a website your goal 1st and foremost is to adhere to http but now I'm also going to assert a little more based loosely that the goal of all software is to solve a problem or to add a behavior and I admit look sometimes it's really

06:05

not clear which of the 2 things that's doing that but I'm still going to

06:09

tell you yes I am absolutely correct on this you'll take my word for it on that means that in the context of HTTP and Django http is the central problem that the web framework that any when framework must solve all right is it is the everything else is a nice city it's there to help you but as a website is defined is just something that adheres to the each piece back then that is the 1st problem it must fix on and it also means that your the behavior tautomer it's trying to make it so that you have to do the least amount of work possible to adhere to this what's which allows us to move directly

06:51

into tango but I don't know how many people here have not

06:57

done the Django tutorial who here is really just getting started I so I went on it by the CIA up I just wanna talk real quick about what have you it's 0 when you do the tutorial you're going to see that there's some Texas as a view is a type of web page on Russell the committee 2 years ago gender concept views or for displaying content under so far as to say that it's dynamic generation of content right the Washington Post is a collection of web pages out some of these web pages are similar right there are a whole bunch of articles on the site and they all meet the following structure right the the oracles are all the same thing and so you typically if you're building this and Django would have view for the articles right it becomes a type of web page so you have your user show up

07:52

and what's going to happen is they going to that 0 yes there are initiated the request of the website and a computer and it's going to get a response back with this is how every website works and were interested in the internals engendered a stock by building Django models and you might go ahead and build forms 0 yes you be generated the database based on the the the the models you then going to go ahead and make a template files and which allows you to build the views if you're websites really complicated you might have a middleware and context processes at which point you will then load GenGO it will get you the following tools and you can now work with this year USA is going to be just as before issuing GP request goes through the middle ware gets into the dispatch it will find you pattern and then find the view the view might interact with a model it might interact with forms and it might go and interact with the templates it's then always going to return back to the user at the opposite way through the middle ware and then send back data on you'll notice that in all these cases i said might because the truth of the matter is that this is the only thing that's really important right everything else is simply a way to try and I'm glad and this is this is simply a this is the core of gender this is the only thing that you would really need to be a web framework everything else is very useful phenomenally helpful but this is this is spot on the highly only further I want to know that that was a simplification that animation

09:35

is nice but it doesn't deal with all sorts of things and so on so please don't e-mail me about the fact that it's wrong and then somewhat wrong right on so we the

09:47

focus on this and continue going to focus on the use now because the user so heavily tied to your all patterns I'm going to have to do with your all patterns a little bit and the entire goal is going to be to deal with views in the context of http so this is an HTTP or the format of an HTTP method this is what your server actually expects to receive when he comes in and we have these 2 systems and they are going to be looking at that request and trying to handle it the 1st thing is it's going to the your pattern is primarily interested in handling the path to the Resource Ready looks at you all passengers that I'm trying to figure out which if you recall and maybe it will take that some information from the you all packed and hand it to the view maybe not on but that means that the view is really interested in the HTTP method there's nothing else in gender that's going to handle that for us this allows us to come back to the original question which is what is the view and we look at it from a much more technical standpoint now we accept and it should be request object at which point we're going to interview generate data based on the HTTP request method of any data that is coming along with the request and an data that the world dispatcher is giving or is taking from the euro pattern and handing 2 of you at which point the view will always always always returned in each G T P response object on that should be ringing of the bells look it's we have to accept and we have to return the secretive use is that what's the secretive use is that any Python callable can be a view which sort of raises the question why do we talk about functions and classes and generics what we limited let's take a trip down

11:43

memory lane to I take it back in

11:46

2005 when gender was nothing but a at horrible and move and about the official recommendation for building views was to use gender functions we now know that that's not really because functions any sort of an inherent part of Django but simply the simplest form of Python callable and so they were they were what made the most sense to use I'm very quickly thereafter people said you know it would be great we are constantly reprogramming all of these webpages again and again and again can we just put them in Django so that we just use them straight out of what people said lecture that sounds like a great idea and there's a problem with this right when you when you try and have extensible and over here you have this behavior and you say this is almost exactly what I want right this is almost exact exactly behavior I want it just a little bit different but the difficulty there is that if you're using a function functions rigid right you can't sort of just digging there and change a little thing you have to use the function entirely or not at all on natural became a problem with the generic views and they said we can only to we do this that all we can try extend the number of parameters we can do x Weinstein eventually realized 0 we're implementing object-oriented programming let's ridiculous why don't we simply use of a class to instantiate objects that are views on this was this was found a pretty successful idea where this is this is how been saying about this would be so useful and serve as a large enough of it so useful to have tried we be able to go ahead and just extended on and so in in 2008 Joseph programs on sorry from France and and 1 ahead and open the ticket and so the discussion became where we're going to fix generic views by allowing them to be extensible but 1st we actually need a class that is of view right we need to be able to instantiate just a simple object view in serviceable what what we want for that we need to make sure that when people are using it it really obvious how to use it me to make sure that's extendable that was the idea and we need to make sure that it's threads the turns out that last really hard and and so it took until 2011 to really get it right to really sit down and say OK we're gonna work through the entire thing and get it right and at which point they the class you along with the generic views and this is sort of where we ran into a naming problem because we started with generic views and was it was obvious what those were right like 0 it's just behavior that program for us and now we introduce this class you right it's just a view that instantiates an object you are the class that instantiates object you use this will just add class based in front of generic everyone will understand right there the note intuition unfortunately there was a lot of confusion in this sort of the remains a lot of confusion about the most on a certain discussing it is to talk about class please use as object used yes a class but the actual views of the the objects that you instantiate and that helps a lot of people go 0 yes of course on the other thing is that the the main point of the generic users not the class store objects that's that's certainly incredibly useful for extending behavior and taking control but that's really not the point the point of the matter is that the generic use on and that leads us the state of the the the pony as of gender 1 . 3 0 for all of the the the beginners in the room of the same the ponies is a joke a long-standing gender joke where we talk about of people saying no you can't have that pony in terms of new gender features and so you will regularly hear people say the state the pony to refer to the state of Django I did nothing about an inside joke on I'm sorry about that and so we can have 3 options right the function simply because it's a callable but we can use any callable into naturally you you can use a function you and the object views of the classes used for their and useful because it's they put it you know 3 years of time to really making sure it was safe and usable and so people don't create their own because it's a lot of work to do that and why when I just use these and then the generic views are simply pre-programmed use for you to use and so you know not really missing category and the and that means that we can now move into looking at each 1 and so the function of

16:23

use of we now know even the considered the original it's simply that they follow what it means to be a few and the callable strike and so we're

16:33

going to begin by setting up of a little project this is a model it's really uninspiring but you get the point it's got a name and a slot on because I can't get away from building views without you a pattern I'm going to go ahead and build a new pattern is going to call my view and it takes in a single parameter called the slot and this allows us to program this and this is sort of your stereotypical view for all the beginners style in the room this is this is sort of how we think of you sigh taking some data life at something from the database and then I return something fire the template on that this is not a great example unfortunately because it means that I'm using

17:15

all of these other subsystems when I meant to just focus on 1 thing and the only thing that really

17:22

matters is you can see the 1st parameter I'm taking in is the request and the render is using that to return an HTTP response and you can see that the idea here is that we're doing can anyone tell me what's missing from our stereotypical view is anyone see something that's that's that's sort of should maybe be in there to read actually shot the out I'm actually asking a question the method yes we haven't bothered to handle the HTTP method at all it's is not even our code there are many ways we can do this it's it's fairly easy right I can just add and if conditions say look if it's the get method on you know do the following and if it's not the get method please return immunity response not allowed and tell them that the only thing we accept is the GET method this is horrible you approach this command like not that's not great thankfully Django introduces the HTTP and of the decorator market about and to help that and so here we simply say look this view of it only accepts get and head and everything else it will turn in HEP 5 method on the returned in HTTP response for a 5 Erica there and it turned out that there is a shortcut for a shortcut for gettin had not called require safe and so we can simply move that over and the at which point we can actually see all the changes were code by using a tool called that you can actually write the HTTP the HTTP request directly in your command line but if you're running a server this is actually kind of fun right so I'm actually typing get a path and it is returning this data to mean I can see 0 yeah that it expects that gives that stuff back to me if I give it a method that is it's not expecting like options it's say no I have no idea what this is I'm not programmed to handle this and so what I can tell you is that I can handle getting head in see on the 3rd line from the bottom on your notice that throughout this talk i'm going to avoid of post at you know it's commonly used because this yes RF middleware is going to get in the way and simply kind interject itself and go high by the way you're not being secure and if you want to disable it you would get a 4 or 5 air to please don't disable unless you really really really know what you're doing on going to go through and we're just gonna create another view just to see at a slightly different case I'm so here is just another URL pattern and here is of you and you go out what this is for handling forms and we say OK so if it's a post I behave a specific way and if it's not a post you know I expect again and I I behave a a different way on that slide showing up at your 24 hour diner and 90

20:24

per cent of the time you're going to say yes I would like the scrambled eggs and you're going so people are is to ask for scrambled eggs 90 % of time as you just make it resemble the eggs but you also for 10 per cent of the time getting the people who were asking for hamburger or the hash browns scramble that's that's really frustrating and you pry nothing to do well with that crowd and so even tho you're handling post you not actually

20:50

handling any of the other each you methods right you can see that it's I'm I'm sort of broader laser on working on you can see that the the topmost conditionality else simply says else doesn't check for any other HTTP method that's that's not so great so even if you

21:10

partially handling beach be method you really do wanna think about whether you're handling all of them and that's how you should deal with function views In Django but we can now turn to the class spaces

21:26

and the key advantage to class species is the fact that there are classes you're dealing with object oriented programming and it's a really powerful way not the only way but a very powerful way to adhere to draw which is don't repeat yourself in Python and so once again we're going to we're going to convert the last 2 views that we have that we built into class bases on I have to deal with the oral part and I

21:50

can go and I don't have time to to go into detail as to why I'm changing this and there's a lot of documentation and I cover this in great detail in my talk in Agenda Cont 2013 the bottom line is you have to believe me for the moment that I have to make this change for this to work on we can actually look at the code and the 1st thing I'm going to do is get rid of the decorator it's gone I don't need it the next thing I'm going to do is change the name of this function to get and I'm going to add self in anticipation of having this as a class I condensed and I put it under a class that is the only difference right it is exactly the same thing except is now it now has a step class that it belongs to his our method on there is however a huge amount of beauty to this because it makes explicit the problem you are trying to solve this code only works if an HTTP GET method is used and everything else is not handled or rather will return a 4 5 here on almost everything as it turns out that class this is the kind of neat if you give them a get method they will automatically give you head method handling and it will also always give you option handling which is really really cool and much better than what you're getting with with the functions on the same thing is is sort of similar when we come back to our form view and we say OK well we need you a little bit more here because what we actually wanted was GET and POST and so we can go ahead and create 2 methods for the class views and we're going to go ahead and define a bunch of attributes the GET method is completely uninteresting break take input I provide output to a page number the code is not really the point input output just again method and with post that's sort of the same thing you handle the form how ever you need to but the bottom line is that this is behavior that is defined only for the post method on and of course you're getting options and head because of the way in which we built so this is an very different from what we had originally a lot of the things that I found some with beginners is if you if you look at this as a beginner it's it's kind of a lot to take in you go well there's an if condition but then there's a 2nd condition and there's this implicit return that has multiple behavior and when ask people taking my class how many behaviors you think is in here the answers I typically get are either to or for when the truth if you look here is that there are 3 and that becomes much easier to see in this context from so CTVs despite the fact that we say the class issues despite the fact that we say are more complicated and take a little bit more of cut more code can actually be used in certain contexts in really interesting very helpful ways and so part of this if you're totally new you may not know 1 and defending these are quite so much but there's a certain of when they were introduced because of the confusion around them a lot of people said well that they're not worth using at all or ever and so part of my position is weight these are a tool and tools do have purposes and can be used in certain contexts as long as you understand what the tool does anyone this context are so I'm just trying to provide when the context of this analysis to sort of look at it the the order of view said

25:26

in in the game which all the generic class use just dive right and because there's so much here

25:32

and you can see that this is also modeled on the right at completely removed again it's it's nowhere and sort of indicating this on and this is this feels awful because you're looking at this you like bold wait this is like a game of matter that's how I know what a feeling how to fill in and this gets even worse with with the model create removed get another removed post but the attributes are exactly the same as they were before and I mean least you have some small comfort and saying well at exactly the way it worked before right injury no it isn't no and and that's where the problem is is suddenly on on new form we now have the put verb which has been instantiated and look dominant Windsor back and go through this what is this look like

26:14

that's the actual graph of how all of the class generic use or related it's really kind of insane and when I give my talk Agenda Cont 2013 this is the image that are presented

26:27

on you feel it takes so much time going on I have no idea what I'm doing I don't understand the point really wasn't that it's a terrible idea because if you

26:39

start using generic views it turns you into a real powerhouse and you can become a super code you will leave websites in your

26:46

wake the problem is is that you spend

26:48

so much time doing stuff it doesn't make any sense to try and get there that it can be really frustrating there are tools that are

26:56

very helpful and we'll come back to this 1 but the idea of for the the idea here is that if you're a beginner the idea was that by having these be classes we would be able to go ahead the and extend them and by over complicating and it's it's become a bit of a problem because beginners go how I extend and how I learned how to go about and fortunately my advice is that if you're a beginner don't right trying to use them if you see that there is a generic class this view that does exactly what you want to use it see how you just get it to do exactly what it was built for if you then once you've become comfortable with that then begin the go out OK well maybe I can begin to look at the methods but this is thing that happens and you can see on the mailing list as the allotment classes were people go all I would like to use the following generic view to do x y and z and the answer is always well yes you

27:59

can do that but there are better options for how you can go about building that and I thought that was going to more laughs really on the basis of little contingency over there OK I and so that's that's the thing is that with with generic views yes you can absolutely customize them to do whatever it is that you want but maybe it's not always the best idea to do that unfortunately the only way to figure it out is to get familiar with them and that sort of unfortunate some yeah now that we

28:33

understand really how all of these work but how do we sort of tackle the problem of using and building views on for the function of use and all that jazz but I think it's fairly straightforward I do wanna take a quick look at classical cost used because last

28:51

time I presented this in the last 3 times a given this and similar talks and the most will go away could you show the tool had is this actually work to finish up on this website and it's going to

29:04

show you all of the generic use at the top level and you can click on any of them and sonically condiiton of

29:12

you and it's going to show me of all of the classes that are related that this view inherits as well as it provides a link directly to the documentation and below this there the actually these are all the attributes that I can override including their current D faults so that I can see exactly what is going on this is really where you wanna start right if you if you decide 0 I'm going to use a generic view I'm going to go ahead and under try and figure out what actually it's need define once you're comfortable with defining the attribution going OK yeah I can get that behavior by changing just attributes you can begin to look at the

29:53

methods right this is where you go get totally comfortable them the attributes but I need to just tweak this 1 thing this allows you to see all of the methods that are being used you're typically wanna start with the methods that the class view users right GET POST PUT so right here I can just click on get and it was shown that

30:16

the implementation as well as Tommy where it's actually defined because it's not actually defined in detail view if you recall the way to the right you can see it's defined in beast detail view Lee & and so that's that's class-based their classic us this use and the sort of the ego go to tool for trying to get through generic views on I'm also when I was

30:40

built when I was putting my but when I was compiled in my book on I I found that I spent a lot of time writing decorators in Django to get it to do what I wanted and I'm slowly going to be taking them and putting them out onto the internet and so I started a project called Jango decorator plus I'm a little burned out from the writing and to be perfectly honest with you and so I haven't not as much work done on this but I I do have my replacement for the required decorator for function views because I think it's silly that the class-based use get you options and head out of the box but that the the decorated doesn't so I fixed that and I've gone ahead and recreated the decorator these too short cuts for the decorator on and their equivalent to just get and get posts on but again you know I say get but that's actually get ahead options and get post is GET POST had options so I'm hoping that that will and provide the basis for for adhering to GP methods of were adhering to the Jedi perspective on in of function keys so I keep getting distracted by the time of on which basis for inclusion on or

31:58

unwanted we can come back to these these questions

32:01

are the the decree ask the questions that I was hoping you would find easy at this point make hopefully these these sort of time tackle if that's a word from so tangled handles HTTP from why do I need to worry about well as we know it doesn't handle it for you right it will translate the HTTP into a nice Python objects and allows you to manipulate all of that data into to easily use that in in Python but it's not handling it for you right you are still taking care neural patterns of the all path to the resource and views you really do wanna be considering at all points what each of the method is being used and how you react to it because of what you're building an API that is only using part of the information and some user on the other ends going to be really unhappy with you on when is gender deprecating function views on it's not right that the the the idea between behind of view is you know except specific input provide specific output and any callable can can be that and so functions users are not anything inherent to gender on and they're not going anywhere and band all class-based use generic no but hopefully the walk through history helps a split the difference right the idea is we have generic views and that is very much its own concepts and then we have object use and that is very much its own concept and we there is some discussion about whether each 1 solves the problem it was designed to solve and there is a valid conversations to have but it's difficult to have a conversation if people continue to confuse the 2 and finally how I choose what kind of you to use this is this is easily that the hardest question on here on and I have a set of questions that I ask myself whenever I'm building few and the 1st is is the generic view that does exactly what I want or almost what I want and can I simply plug into it if yes user generic you right that's you know it's there for just that if there isn't you sort of left with the choice between a class this view and a function-based view but we now know that there is essentially equivalent the only reason that you might want to use a class view is you have behavior that is shared across multiple views at which point using object-oriented programming is incredibly helpful but if you don't it's entirely up to preference right you could use function used for class-based use it doesn't really matter and which runs mainly to serve the key takeaways you is changes solution to handling HTTP on all views are callable color Python called doesn't matter on please use the require each method if you're using a function of its or at least consider how you're handling chiefly methods and finally but please think of the 2 concepts of generic and class used as very different varies on thank you very much for coming I hope this was helpful but elegant all of the related material is about 1st link of my book is currently available for pre-order and so it's currently discounted Amazon is in charge of that and they won't give me an answer as to whether the discount remains after if that's simply a preorder thing but it's currently discounted and finally at the end of september as I mentioned beginning at the beginning of october rather but I'm going to be teaching a class in gender locally as a thank you for all coming to this and that is a promo code that you can use for 20 some of the questions I'm also if we're out of time I'm happy to take questions in the hallway I have I have 40 seconds I'm I cut this so close now we started late further and questions again feel free to describe me throughout the conference if you do at any point but I think there's a there's a mike based on the flexibility a class this view doesn't it and the fact that it gives you all those methods to define kind of free doesn't make sense always is a class based you over a function you because deal with the other methods I it's it's sort of your honestly is is really a preference because I'm at that point you know it it will get you head and options but let's say that you're using the decorators from decorative plus you get that same behavior and a function for just calling return and there's so there's really very little difference but if it's really a very very simple view is there possibly like performance optimizations overhead for that forms the freezing class last method of In the last 2 there might be a small performances but that's I mean that's not really where your performance problems are going to be greater than you performance problems are going to be an I O on so called like that mn right so I'm wondering about um you know let's let's say for example you have uh a model or a or an object of model such as like a task and you can perform different modifications right so as you use like an updated view or something like that as a class this you know if you wanted to modify it in different ways at different stages of time I do you have a recommendation on how you do that so like I you know let's say uh at 1 point the task is unassigned and then you post that it's been assigned to a particular person I'm thinking of you knowledge for the simple ticketing system for in the person updated and so they post you know the status of and eventually gets close kid could you handle all that in a single class would use split into different murals the other thoughts fact depends entirely on how you want to handle it you certainly could have a single edit view where you can change all of this on all of the the fields on the model you certainly could have multiple right you you might just have 1 where you click on it and it immediately updates and changes the status of a flag on and so it's sort of up to you come I'm leaving this understanding that the question that are not super clear and the gate I'm happy to talk about it afterward from thank you all very very much for coming I hope that was helpful feel free to grab me about during the conference if you have any questions thank you are there in the and the things

Inhaltliche Metadaten

The goal of this talk is to make views and HTTP as clear as daylight. This talk is for you if you're confused about: how function views compare to class-based views when to use generic class-based views- the difference between class-based views and generic class-based views- or when to use any of these This talk will start with an introduction to HTTP and how Django handles HTTP. We will then look at each kind of view in Django, focusing on how each works and why Django implements it that way. This will allow us to look at the advantages and shortcomings of each type of view. Finally, with a full understanding of Django views, we will be able to easily determine when to use each type of view. Table of Contents: What is HTTP, anyway? Django's HTTP Request/Response Cycle What is a callable? History of View Functions Functions Generics Classes and Generics View Functions (or How I Learned to Stop Worrying and Love Non-Compliance) Class Based Views (or As DRY as the Sahara) Generic Class Based Views (or Oh For The Love of Graph Theory) Enhancing Views (or 1-Size fits no-one) Fixing Your Views When to Use What