Building a Video Sharing Web App With Python3 and Django Part 1: Setup & CRUD Operations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this tutorial we're going to start building a video sharing web application with django and python this will spin across multiple videos as we add more and more features to the application in this video we're going to set up the project and add some class-based views to get the crud operations added cred stands for create read update delete we will add the ability to create video uploads to the site retrieve them from the database update the uploads and delete them from the database we won't worry about authentication or any other important parts of these views yet we're just going to get it functional for now this tutorial is definitely not meant for complete beginners to python or django the entire application will build from scratch but we'll go through the beginning stages relatively quickly you should have a good understanding of models url patterns and views including class-based use before starting this project with that said let's start building this application so the first thing we do is open up a terminal and in here we will start by creating a virtual environment so i'll type python3 dash m vnv we'll call it env here create that and then we'll go ahead and activate it by typing source emv slash bin slash activate and now with our environment activated we'll start by installing django so i'll type pip install django and now django installed we'll go ahead and start a project so to do so i'll type django admin start project and i'll call this one video share and you'll see here that created this video share folder with our um so some settings or urls anything else we need to start a project and now with the uh project started we can create an app and django if you're not familiar an app is just like a small function of the the total application so in this case we want a videos app to handle all the um video functions including all the credit applications we're going to create right here and to do that we'll type python 3 manage.py actually first before we do that make sure we gotta we need to uh go into this directory first so i'll type cd video share and i'll type python three manage.py start app videos now we'll create our videos uh app right here and now before we start with that when you go into our settings when you add this videos app to our onto our installed app so right here the stall apps i'll go ahead and just type videos and quotes make sure add a comma here save that be aware of that and now with our app added and everything else ready to go we can start building our index page and so just to test everything's working i want to create a simple html page that will just say index and just load at the root url just so we know everything's working properly so do that i'll go into my videos app i'll go down to my views i'm gonna start by creating a simple view and this will be a function based view but most of the views will create will be class based views but just as a test i'll create a function based view to start so i'll type def index pass and request and within here i'll just return render request and i'll put videos slash index.html go and save that and then i'll jump into my urls and add this to the root url to do so i'll type path empty string and i'll type video before we do that let's go and import them before if we could do that i'll import from videos import views as video views i'm doing it this way in case we end up adding more apps later on um i don't want them the naming i don't want the names to overlap so i was calling video views so we know what app we're in to get these views so now i'll add video views dot index and i'll add a name which will just be index add a comma here we'll save that and now let's jump back into our videos app we'll create a new folder in our app we'll call it templates now within this folder we create a new folder and we'll call it videos this is where by default django will store all the templates so it has to be within our app we'll create a folder called templates then another folder that's the same name as the app and then within here we put all of our templates so i'll create a new file in here i'll save this as index html and just to start we'll create a very simple html page and inside of here we will add just an h1 and put index and now we can we should run this and test our application so open our terminal window back up and i will type python3 manage py run server you'll see this error for unapplied migrations we'll fix that later for now it won't be an issue i'll open up the open book browser go to localhost port 8000 press enter and you get our index page right here so everything's working great let's go ahead and add a little bit more to it so right now we have just one index one html page and that's great and all but once you add a lot we don't want to create every page from scratch like this it'll be a lot of work to a lot of copying and pasting to get just a html page ready to go so to avoid this i'm gonna create a new phone here i'm gonna call it base html and i'm gonna go ahead and grab bootstrap and here i'll put my base template i'm trying to make just an html template down here i'm going to create a code block if you're not familiar with this is this is django's templating engine and it will just allow us to put code within these curly brake and allow us just to put blocks of code within the base template so i'll type block content let's spell it correctly content and then below here i'll put in block content so all we're doing here is we're going to take our index code and we're going to put it in between here on our base template so back in our index now first we'll save this back in our index we'll go ahead and delete all this and i'll put in the very first line of the file will be extends and then videos slash base.html so you gotta put the name of the app slash the base template and then underneath here anchor that same content block i can block content and then here and block content we'll go ahead now in between these two tags on h1 just put index and now we bring our browser back over here we reload the page and it works fine just as before alright so now let's go into creating our video model so inside of our videos app you will find a models of ui file and here we're going to create our model for the video object for creating the database so we're trying to do here is we're trying to create a class that has a bunch of different fields that we'll need in our video in every video on the site so for example we'll need a title description video file thumbnail and date posted all saved inside the database and so we can do this with our models file we can come in here we can type class video to create a model and we'll we'll extend models.model and then down here we can add all the fields we'll need for the for each video on the site so we'll type title equals models dot models dot char chart field and even a max length which will be uh we'll also give it a max length of 100. give it a description models dot char field uh action our description is not bhar feels to be a text field because he wants more a bigger area to type text into uh we got a video file [Music] for the models dot file field and i'm gonna come back to what we put inside of here for now leave it like that leave a thumbnail and once again leave that just like that for now as well as a date posted i'll come back and explain this here in a second for now so type in date time field default equals time zone.now and then up here we'll import from [Music] django.utils imports timezone all right so here are all of our fields created every video on the site when we add a video we'll need to give it a title description video file a thumbnail and a date post will happen automatically these are all the attributes we'll need for our video and we'll save this in the database so our title description are pretty self-explanatory our video file will hold the actual video file for this for this video the thumbnail will hold an image to display on the site and the date posted you see here we did models.datetimefield default equals timezone.now so what this is doing is if there's no if no date is entered it will default to the timezone.now but when we put this on the site we're not going to give them this field so always just default to the current time so this date post will always hold the current time when the video was originally posted now for these video files and thumbnails we need to add a couple extra things so first we add an upload to so we'll go in here and we'll type upload rscore2 equals and now here you want to give it a path to put it in i'm going to go ahead and create over here i'm my let's see what would be right here at the root of the project i'm going to create a new folder i'm going to call it media and then inside of here i'm going to create a new folder and call it uploads and this is where i'm going to put all the video files i can upload to the site so in here it will automatically go to the media so all i got to do is give a relative path from there i'm going to uploads and let's just go ahead and do one more we'll do video files so let's go ahead and our insider uploads folder here we'll create a new folder we'll call it video files so that's where all of our video files will go let's go ahead and also create a thumbnails folder as well so type thumbnails i'll go in there and we're going to go ahead and put that within our thumbnail field we'll put upload 2 equals uploads slash thumbnail so what we're doing here we give a relative path starting at the media directory so in our file path here we have media then we have uploads video files so it goes to uploads and here video files and looks inside this folder and same thing for the thumbnail if it goes to this thumbnails folder but we also want to add a little extra here we could leave like this but we don't want the user to upload like a jpeg for the video file that's not going to work so we can import some validators from django.cor.validators import file extension validator and this just like its name it will validate the extensions on the files i can type this correctly so now after upload two put a comma we'll type validators and here create an array and within this array we'll write file extension validator and then we'll give it some allowed extensions and within here we get a list of all the allow extensions so for this case all i'm allow right now which is mp4 so i'll just type fp4 in here we'll do the same for the thumbnails we don't want them to upload a text file or a video file for a thumbnail so we'll give it a validators as well with this equal to a list file extension validator allow extensions equals then a list of all the extensions png jpeg and why jpeg spelled this way so we'll upload png jpg images for our thumbnails and that is most of all we need for now for the model later we'll add a user field as well we want to add user validation to all this but for now we'll eat like this so go ahead and save that but now one important thing to do before we do anything else we have to set up our media directory root and media directory url inside of our settings so it knows where to look for for these these folders and these files so if you go to your settings py here scroll on the bottom we'll set it up right here below the static so type media files and i'll make two variables media root underscore root that will equal os.path dot join and we'll take our base stir and add media to it and then below that we'll make our media url which will equal just slash media slash so we're doing here is we're defining where our media route is we'll take our base stir which is defined up here and this just goes to our base directory for the project and from there i'm just adding media to it so it knows to go from our base directory just into a folder called media and there's everything we need go ahead and save that and then we'll go into our urls and this will only work for development projects once you put it live using apache engine x you'll need to set up differently based on whatever media or whatever web server using to serve the media files there but for development server we can just go into our url so we can type plus static settings dot media url comma document underscore root equals settings that media root and then up here we need to import from django.com import settings uh from django.com.urls.static import static and all this is doing is just telling where to get the files adding them to our url patterns you can read more on the documentation for that on django's website to learn more about what that's actually doing um for now we'll leave it at that we'll save that now let's go and check everything see if anything is working if it's broken yet so we'll cancel that run this everything seems to be going well so now we'll go ahead and fix that um error here we have this unapplied migrations so to fix this we're gonna stop our server type python3 manage.py make migrations and this will make this migrations file and it creates the model for our video and i want to migrate those to the database so type python3 manage.py my grade there it goes and so our database is already set up by default with django i was sqlite three database which i'll see down here and we'll keep it like that for now maybe we'll change it later to a postgres or mysql but for now this will be fine and now those changes our model should be ready to be used now so now let's go ahead and make sure we can view it on the admin page so first let's go into our admin.py and this is where we can register any models or to be seen on the admin page which we'll go to in a second so let's go ahead and add it here first we import it so we'll import from.models import video and then we'll add admin.site.register video and this just tells it to make it visible and usable on the admin page so that's saved let's go ahead and open up our browser again let's go to localhost port 8000 slash admin oh i didn't start my server let's go ahead and start the server again there we go now let's go here load this and now it gets this login page but we didn't create a login yet so to fix that open our server up we'll go ahead and stop it again and we'll type python python3 manage.py create super user um we'll go ahead and just keep it admin uh we'll leave the email address blank give it a password [Music] and we're going to bypass that for now just for this tutorial and now we have that user created we can go ahead and run our server again log in and here we go now we're inside the admin page uh down here you'll see the videos model click on it click add video and now we can add our video i guess our title our description our video file our thumbnail and it automatically gives it to current date and time so early is working great now let's go ahead and start creating our actual views for the uh the video quote operations so to start let's create a review so we'll go into our views uh and for now we'll leave this index one here for now we're gonna change this later to give make it a list view i'll probably down in the next tutorial but for now let's go ahead and just add our create view so below this view we'll go ahead and create a class called create video we inherit from create view and now up here let's go ahead and import create view from django dot views dot generic dot edit import create view and now inside this class we need to give it a model so we'll get model equal video which you also need to import so up here we'll do from dot models import video now below our model we need to give it the fields we want so we'll type fields and on inside of here we need to give it all the fields we want so in our models of ui we tell which one of these do you want to show up on the form so we could do just in quotes underscore underscore all underscore underscore and that would give all the fields but we don't want all of them in this case we don't want the state posted showing up because we want this to automatically go to the current time so instead we have to give an actual list whoops so back in reviews um we won't do that instead we'll give an actual list of all the different fields we want so we title description video file and thumbnail and the last thing we need to add real quick we'll send the last thing there's two more things the last field we had here is our template name so we'll type template underscore name and this will equal videos slash create underscore video html which this we have not created yet which we'll do in a second but the final thing we want to add is we want to tell it where to go after the form submits successfully so to do so we'll type def get success url self pass itself there and this won't make a ton of sense right now um because we haven't created the video detail but that hopefully will make more sense once to do so so for now let's type it in out reverse input video dash detail underscore orgs equals and i'll pass in a dictionary of pk the key and the value being self dot object dot pk i'll come back to this in a second for now we'll leave that here for now and now it's going to create this create video html template so what's out of our videos uh our temples and videos here we'll create a new file let's call it create video.html and i already have all this html created i don't want to create it right now because i'll take a while to go through everything so i'm going to go ahead and just copy that in real quick and all this code will be available on github paste it in here and all we're doing here is once again we're creating this videos or extending the videos that based html template um this tag here will be loading crazy forms we'll do that later so for now i'm going to delete that out and remove this as well i'll wipe that in a second but then we have our block content and we have a container a row column all this stuff all these different css styles we're going to use bootstrap to make them look good here in a second but we'll do that here in a second so let's go ahead and save that and save this and now this won't work because of this so i'm going to go ahead and comment this out for the time being and save that and now let's go ahead and see after we add our urls and we can get this this view to load so let's go into our urls here and we'll add another view and what we'll do here instead of just adding in all the video views here we could it would get kind of long and it might make this a little more not as easy to read i guess so i'm going to include another file here for the rest of the video views that are not at the root url so first let's go ahead into our videos app let's create a new file we'll save this as urls.py and then back in this urls file here we're going to go ahead and just we'll just copy it just just to get a starting point where it's right here paste that in there we can get rid of this and here this as well as our views here go away and now we want to add our include here so up here with our path we need to add include as well import that and then below this path we'll create another path we'll put out the url videos so go to localhost forty thousand slash videos go to all these urls at our training slash and we'll include [Music] videos.url urls and we'll add a comma there we'll save that let's go ahead and copy this here and go into our urls and paste that there now in this urls file everything will start at videos slash and add whatever else we put here um hopefully in a second also the browser it'll make sense but for now we'll add just a create and this will go to this this will go to our create view or create video view so up here that for the imports we'll type from dot views import create video and now down here we'll put create video and if you're not sure the class space views we need to put dot as video or as view to load it as a view on django and they'll give a unique name let's give it uh video dash create go and save that now see if we can get it to run open up in our browser so let's go back to the site go to slash videos slash create right there and here's our here's our forum obviously right now it looks terrible which we'll fix here in a second but at least we know it's working and it's loading um correctly we have all of our fields loading correctly as well so that's good let's go ahead and make it look a lot better so over in our html template or our index or our crate view i got rid of that line here i'm going to add that back in so we'll add in this load crispy forms tag right here and then we'll add down here a filter of crispy if you don't know what filters or crusty forms are this is just a way to add bootstrap and make the forms look a lot prettier automatically without having to really do anything and then this crispy filter just adds all the bootstrap styles and classes to the form itself without doing anything extra it's very easy to get good looking forms up and running pretty quickly i'm using this but we need to go ahead and set all this up so first we need to add bootstrap we don't have boost of ad yet so let's go into our base html and we're going to change this a little bit so open up a browser here this we'll go to get started and we'll come down here and we'll grab the starter template we're going to paste that over here and then down here we'll grab this hello world we add our content block back again so we'll type block content here we will add end block content go ahead and save that and this will automatically add in the required meta tags it'll add in our link to the bootstrap css as well as a link to the bootstrap javascript and jquery are they also needed right here so that added when you go ahead and install crispy forms and we should have better looking formula for that so to get that installed we'll open our terminal back up we'll stop our server and we'll type pip install tip install django dash crispy dash forms well that install our christie forms and i'm going to open this up over here you can go ahead and just search crispy forms and they'll give you this user guide that kind of tells you how to use it we're using the crispy form tag here as you can see it gives you the example of little christy forms and add the filter so yeah i'll put the links to everything in the description below so you can get to these links to read more about these different things that installed let's go ahead and just start server again so we don't forget and now let's go to our settings that py the last thing we need to do is tell christy forms what um typo pack to use to load to add the styles i guess so really easily we can just come in here below all this and you just type crispy all caps template pack equals then in a string to type bootstrap four that's the version bootstrap we're using so we'll just type bootshot4 you can use two and three as well but you might as well use the newest version go ahead and save that and now with all that added i think that should be it just double check we have our blog content here let's go ahead uh that's fine actually yeah i think i'll show you here but you added ready go now let's go ahead and open up our browser so the browser go back here let's reload this let's see what we get we get a oh one last thing i did for you to do we need to go to our settings of py and we also need to add this as an app just like we did for our videos so up here below our videos we'll go in as a string type crispy underscore forms add a comma here save that now let's go ahead and go back to our browser reload the page and there we go and now we have this page automatically created with a much better looking form without doing a whole lot with it which is really nice it also gives some good validation as well and gives you good styles for the validation as well for now we can submit we're going to error i'll be filling out the test test um just choose any video we go and hit submit and get error that's because we took away our success url so there's nowhere to go but let's go to admin for the videos we have our video object created so everything else is working great um we set that success url back and that will give us a place to go once it's it submits the form let's go in for now we'll go and just delete this so we can test it again in a second back to the site you'll also see that our index page has some more styles now to make a little better as well okay with that added let's open our text editor back up um we have everything kind of set up let's go and create our detail view um which will also hopefully make this create view um get success url added makes a little more sense as well um so let's go back to our views and now below this class we'll create our class we'll call it uh what's called detail video this will import from detail view which you'll need to import so up here um below our create view we'll go ahead and import from django dot views dot generic dot detail import detail view and now within this class we'll go ahead and add a model which will equal video and below this model will add a template name which will equal videos slash video underscore detail that you should about which we'll create in a second but that's it that's all we need to do uh for this to make this work for the view at least so now let's go ahead and uncomment this and comment this as well just fix our indents and now we add our url first and then we'll come back and explain this so let's go to our urls.py and here we'll include we already included our videos so we'll go to our videos.url which will be right here and this will have what we need so we'll go to localhost slash videos and we'll go slash the primary key yes let's go put the primary key of the video so to do so type path inside a string we'll put a lesson sign int colon pk greater than sign and then put us uh trailing slash and so we're doing here is we're saying we want to add a a printed added variable to our url and so every model object automatically comes with a primary key we didn't put it here but it's automatically added in here um just as a unique identifier for each object so the first thing we'll put we'll have the primary key of zero the second one we add will be one two three etc for as many as we add so we're doing is we're just putting that in here so we get a unique url for each video that we have on the site and now let's go and import our detail video uh yeah for detail video and we'll add it as the view here so put detail video and like last time i'll put as underscore view and we'll give a name which will equal video dash detail we'll go ahead here and save this and now let's go back to reviews for a second and you'll see for create view we put a def get success url return reverse which actually we need to import up here reverse and then what we're doing here is returning reverse this url so it's going to this url pretty much video detail which is that's this detail view we've created so we're going to that view a url i guess we're passing in a pk of the current object.pk so whatever video we create we'll take that primary key we're passing into the video detail url so in our urls that pkn pk number in right here we're going to that view hopefully that makes a little more sense all we're doing is we're just going to that view the detail view of that video we just created and we're passing the primer key to give it a new unique url it knows which video object we're going to and which one to load on the screen so finally let's add our detail video url or our detail video template so we'll go into our videos create a new file we'll save this as uh detail underscore video.html once again i'm not going to type all this from scratch right now because that'll take a while i created this this very simple um html file here we'll go ahead and copy it i'll paste it in here you see there's a lot here i won't go into everything i'm using some different um things here to show the uh the video a reason this video js library slide here in a second to make the video look a little better than just the normal html5 video editor editor but the important stuff here we're passing in this passing in this and a few other things so let's go ahead and go through these real quick so when you put two curly braces like this inside django's temple engine you're passing a variable into the html file so what we're doing here is we're taking the object which is automatically added normally if you were to add something to like a function based view you have to go in here and create context and then create a dictionary that holds like object which will equal whatever object we have and we'll pass that we pass that context in down here in our render to make it available in the in the template but with all these create views and classes use what's really nice about them is that's done automatically so none of this is needed and it automatically happens so the auto passes object context to our template which just holds the current video object that's all it's doing so back in this detail video we're taking that title and we're passing it inside this h5 um to show the video title and then down here we're getting the date posted and we're passing this filter with date colon and then this string here which formats the date so we just want the month the day and the year we don't have much extra stuff it'll format it weird otherwise so this makes it look a little more readable for the user uh we have some more stuff here we have a modal for the share button which we can look at later but the important thing is anytime we have these two curly braces we're passing in a a variable from the template and that's really all it is there's some other stuff here just to make it look better and whatever else and that's similar functionality but more or less that's all we're doing here so now before we load this up um we need to add this video js and if you're not familiar with what that is let me go ahead and just pull up real quick dojs it's this javascript video player and you can choose different ones there's all these different choices um available and they have these better looking video players than you would normally have just from html5 video so i'll put a link to this in the description as well and you can go ahead and find the docs here to explain a little more what we're going to do but to make this work all we have to do is add some things to our base html template so first let's go ahead and add in a few things so first we need to add in this link in the head so all we add in is just a cdn for the javascript and css to make it work and so they have in our documentation they have links to all this which i'm going to go ahead and copy them all over real quick and this is just some scripts and a link tag there and then finally i'm going to add in this script down here at the very bottom for the body before the closing body tag and that's all we need for the base video.js so in our detailed video what we're doing here is we create this tag that's how you use it so how to use this real quickly is you create this video.js tag and inside here we'll pass in some different attributes passing that class to make it look better and we get a width and a height give the controls and then we add this data setup attribute and in here i'm adding a couple other custom attributes to it so i'm adding here is a playback rates and this list that has the different playback rates this just allows you just to change the speed of the video i'm just a feature i want to add to it and fluid true just to make it more responsive on mobile devices but down down here we got a source tag right here and to we need to pull up the source of the video and to do so all we need to do is go into our media uploads video files and find this video file but if you remember our models we already specified this in here so we already know where it's at back in our detail view all we need to do is give it slash media slash and then pass in using our double curly braces to pass in a variable object.underscore video file and give a type which is video slash mp4 that's the only type of video we're allowing to be uploaded to the site currently and this p tag here is just in case they don't have js enabled a javascript enabled or don't have a web browser that supports it it gives them this instead of the video that's really it that's all we need to do to make this video work once again there's links here for their documentation i'll put all this in the description so you can see how to use this and learn a little more about it if you'd like to do so without all that added let's go and see if we have everything working so open up our browser let's refresh this let's go to slash videos slash create now it's going to create a video um it's called test subscription um we'll go ahead and choose the thumbnail now with all that there we'll go ahead and hit submit we've got an issue here temple does not exist oh so issue here is when i typed in i type video underscore detail and in here i called the video details for video let's go and fix it real quick and our views round reviews we'll go to our uh we interviews right here we'll type to change this here to detail and this to video let's go and save that and try this one more time so we'll go ahead and open up this let's go back refresh the page type test video test description uh we'll choose our video file and choose a thumbnail and with all that selected we'll go ahead and hit uh with all the added we'll go ahead and hit submit so we have a video here loaded now hits play it plays the video we have our playback features here that we added and we can go on the other screen but it goes into that little it's what we call it a little small screen in the corner it can also go into the full screen so that's working well all right so let's go ahead and add our um update view and delete view and that'll be it for this first tutorial so first we'll add our update view we'll go ahead and go into our text editor here let's go ahead to our views and add our update view so below the detailed video we'll go ahead add a class update video update view and once again you import the update view and that will be imported from the same place we import create view so let's put comma update view now inside of this class let's go ahead and add a couple things so first once like like all the other uh class based views we need to give it a model so model equal video and then once again here we add the fields you want to update so uh for this site you can do it how you want but for this one i'm making i'm going to look ahead and only let them update the title description so let's go ahead into our fields and we'll create a we'll create a fields variable and we'll add in a list here to add the fields we want to build update in the form so just like in our create video create the fields for the title description video file and thumbnail except for the update view i don't want them update the file or the thumbnail once they submit it that's going to be it for those so inside this fields list we'll create a title and we'll add when i create tab we'll add title and description um as strings inside this list and let's go ahead and type fields correctly as well that would be useful and now it's going to give a template name and for this all we need to do is give it the same template we used before so let's do videos slash create video.html the form will be the same just the the feels we different but we can give the same exact uh template as before so we don't create a template for update view at all which is very nice and then once again once we submit this we need to tell it where to go so i'll go ahead and add a def get success url pass himself because we're in class and we'll go ahead and return reverse video detail quarks which is the keyword arguments to pass in our primary key and give it the value of self.object.pk and this also is just the same as we did before for our create view we're returning this video detail url using quarks which is keyword arguments to pass in a primary key and give it a value equal to self to object.pcat which is the current primary key of the one we just updated and now with that added we'll go ahead and save this we'll go ahead and go into our urls and now inside of here we're going to go ahead and add the path for the update view so to pass once again we'll do int colon pk inside of less than greater than sign slash update so up here right here real quick again we're taking the primary key from the one we just updated or from what we want to update or pass it into our url right here so we have a unique url for each video and we're doing slash update to give it your pattern that we want to load the form to update this specific video and then once again up here we'll import update video and then down here we'll add it to our view right to our path update video dot as view like we did before give it a name which equals video dash update add a comment here we'll save that and now let's go ahead and go to this um which i updated we just added so we'll go into our browser right here via slash three and we'll just add at the very end of here update and right here you can see we can get update the time description so we'll just type in here updated test video we'll submit it and see what happens and down here you can see we have our updated test video our title updated to what we just changed it to and that's it now we have our update video added these classes used make it very very easy to quickly get all these different um operations cred operations added and with very little amount of code really i mean we did this whole thing in just a few lines and now one last thing we add is our delete video so we added a way to add the videos view them and then update them the last piece for our code operations is a delete operation so to do so for now all we're going to do is just make the url we'll delete that way eventually we'll go to our we'll go into the template we'll give it a little button here if you're logged in to delete it but for now we're not worried about that we're just trying to get just the basic operations working so back in our views below this update video we'll add a class we'll call it delete video and this will import delete view this will not import but inherit delete view and then this also will be imported from the exact same place we did our create update views so let's put delete view and now down here this fusion looks very similar like we just did we're gonna have this look almost exactly the same really we'll have model equals video template name equals videos slash delete video that html and this will just be a confirmation um django requires a like are you sure sort of page um for every delete video for every delete view um that's what this will take this temple name will take you to and finally where do you want to go after we delete the video well we don't have a actual video to redirect to so instead we'll just go to uh the index page so return reverse index we'll save that go into our urls add the path for this as well so we'll go ahead here and type path [Music] and once again we want to pass in the primary key so we know which video we want to delete let's inside less than greater than signs slash delete and let's go and import our delete video and then we'll do just like i did before we'll put delete video dot as underscore view now we have a name of just video dash delete add a comma here for later [Music] and there we go now let's go ahead and go back to our browser and see if we do this correctly so we'll go to videos slash three slash delete or actually i already know we forgot to make the template so before we do that let's open back our sublime text and now let's go ahead and make our are you sure template so we'll go here and i don't remember what i called this already just delete video okay so here we'll do a save and we'll call it delete underscore video.html and once again i already have the setup so i'm going to go ahead and copy this in for now and i'll go over here in a second so we'll bring that in here and once again we're extending the base template adding a block a content block within that content block we're creating a little just html page here it just has a form and as it says delete video this complete the video forever this cannot be undone just kind of are you sure sort of thing and it'll be a button to delete it and that's all it is one thing i don't think i explained yet was a csrf token that you might have seen earlier and what this does this just gives it it's cross-site request forgery it protects against that it gets this little token to protect against this malicious use of different forms in your site you can look into that it's pretty interesting if you look in like kind of what that does and how to protect against it um but that's all it is just across that request forge your token it's required on forms you'll get air if you don't have it so just make sure to add that in to avoid uh those errors okay everything's safe let's go ahead and go back to our browser we'll do videos slash delete and now we have this little form here delete video are you sure we'll go ahead and press delete it heads back to index now we try to go back to that video you'll see we'll get an error hopefully yep so the page not found so it did delete the video correctly and we can double check that again if you want to by going into admin go to videos and now we have this video object which is our other one this is not the one that we just added because it doesn't have the updated one so the one we we added is deleted so that's good and that's really it for this first video um there's still a lot more to add watch a video this page here has nothing on except index we want to list all the videos we have like these little boxes here like you do on youtube to list all the different videos we want to add ability to log in to post videos we want to restrict logins just to those who are logged in we want to add some more functionality for users maybe add a profile there's a lot more we're going to add to this but for now we're going to stop here for now and next video we'll add more to it and make this better and better after each video hopefully so thank you for watching and i'll see you in the next video
Info
Channel: Legion Script
Views: 2,882
Rating: undefined out of 5
Keywords:
Id: T4XZgW3nEX4
Channel Id: undefined
Length: 52min 34sec (3154 seconds)
Published: Wed Jul 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.