How To Make a Django REST API - Full Tutorial & Deployment

How To Make a Django REST API - Full Tutorial & Deployment

Show Video

in this video I'll teach you how to make a d Jango API using Python and the D Jango rest framework the API will look something like this and we'll be able to create read update and delete data now at the end of the video I'll even show you how we deploy this API to a public URL and if you want to test out the API that we'll be building I'll leave a link in the description right now if you click on that link it's going to bring you to a platform called Acorn now I've teamed up with them for this video this is a completely free platform you can sign in with your GitHub account and what will happen is it will actually start running an instance of this API once it's finished running you can click on this button right here and it will bring you to a deployed public version of it where you can actually test it out so you can go to slash blog post and this will be your own instance of the API you can mess around with it you can do whatever you want and you can test it out so I'm going to show you exactly how we do that so once the video is finished you can deploy the API that we made you can share it with people and you can use it from the internet not just from Local Host with that said let's get into the video so to begin we need to do a little bit of setup here and install the necessary packages at this point I'm going to assume that you have python installed on your system and what you'll do next is open up an editor something like vs code and simply open a folder in this case I've opened a folder called API it's important that we are working inside of a folder and that our terminal is in that location for the commands that I'm going to show you to run now what we'll do is we'll create a new file where we'll specify the requirements that we need for this project so we're going to say requirements. TX XT and inside of here we're going to write out Django Django rest framework and then we also want environs like that okay so these are the three packages that we're going to need to install in order for us to work with d Jango now that we have these in the requirements.txt file what we'll do is install them using the PIP command so from our terminal here in the same directory where we have our requirements.txt file we're going to type pip 3 install hyphen R requirements.txt this will simply install all of the the packages that are inside of this file now if you're on Windows you're going to change this command to Simply Be pip if you're on Mac or Linux it'll be pip 3 so we're going to go ahead and hit enter and it should install all of these for us once that's installed we have most of the basic setup done and now what we need to do is create a new D Jango project so now that we have our dependencies installed we're going to use a command to create a new D Jango project now to do that we're going to type django-admin start project and then we're going to call this my site you can call it whatever you want but I recommend you go with the name my site now for some of you this command will work for others it will not in my case this command does not work so the way that I fix this is I'm going to remove the admin and I'm going to type Python 3-m and then Django start project my site so if you're on Mac it's going to be Python 3 if you're on Windows it's simply going to be python okay so go ahead and run that and we should see that we get a new directory being created here called my site this is where we'll write our API and where we'll run the next commands so we're going to change directories into my site so now notice I'm inside of this directory in my terminal from here we're going to create a new app now inside of our projects we have various apps apps are ways to separate out different pieces of logic in this case we're going to have a simple app this will be for the API so we always need at least one app so we're going to create one called API to do that we're going to type Python 3 manage.py notice that's the file that's right here and then we're going to say start app and we're going to give this a name I'm going to go with API and I recommend you give it the same name go ahead and hit enter and you'll see a new directory is created here called API now what we need to do is we need to connect this app with our main python project or our Django project sorry and the way we do that is we go into my site directory we go into settings.py let's make this a little bit

easier to see and we go into where we see installed apps here and we add our application into the installed apps to do that we simply add a string and we give it the same name as the project name or the app name sorry which is API so notice we created a new app called API and I've added that here to the installed apps now as well as that we're going to add the D Jango rest framework in here because this is something we'll use to have some nice clean views for our API so we're going to say restore framework inside of the installed apps and for now that's all we need to change inside of settings.py now we have linked our API with our project what we want to do is start building out the model or the data that our API is going to interact with now you can obviously have multiple data sources in this case multiple models we're going to start with a single model what we're going to do is open the API folder inside of the API folder we see that we have a file called models.py now this is where we Define the different database models now D Jango uses something known as an OM now an omm is an object relational mapping and what this does is it Maps a pip python object to a database instance this is really nice because it allows us to use multiple types of databases and have D Jango handle all of the low-level commands that actually create update and retrieve data and in our case we can use a higher level python wrapper which is known as the omm to access our data create our data Etc you'll see how easy Django makes it using the Django rest framework in just a second but bear with me while we create our first database model so we're going to create a class here this is how you do it in Jango and we're going to say that this is blog post now this needs to inherit from the models. model when we do that we get all of the basic functionality of a database model which in this case is really equivalent to a table in something like a SQL database now what we need to do is Define all of the different columns that we have for our model or the fields or type of information sorry that it will store so we're going to begin with a title and we're going to say that this is the models. charfield and inside of the charfield we can specify a parameter which is the maximum length equal to something like 100 next we're going to specify that for each of our blog posts we'll have some content this will be a models. text field keep in mind you can make this anything you want but I'm just going with some basic Fields here and then we're going to have a published unor date and this is going to be equal to the models. DAT time field and we're

going to add a parameter here called Auto add now which is equal to True which specifies that we don't need to set what the time and date is whenever we create a new instance of this model so we have a new row in our database it's going to automatically kind of fill in the publish date for us next we're simply going to say Define uncore string uncore uncore we're going to take in a self and we're going to return the self. tile of the model just so that if we end up printing out this model we actually see some information about it so that's really it for the model you can get much more complicated here and store a lot of different types of data you can have relationships you can have multiple models but for now we're going to do a a very simple one we're going to focus on the API views and then later on you can adjust the models and you can create multiple views for making different types of models now that we have our model we need to create something known as a serializer so I'm going to make a new file here and it's going to be called serializers do PI now inside of here what we're going to do is specify a class that will take this model and convert it into Json compatible data if you're unfamiliar with Json this is Javascript object notation and whenever we work with an API we're essentially sending and receiving Json data it looks something very similar to a python dictionary but the idea is that we're going to have all of these python objects that we'll be working with in our code and these represent information in our database we want to create what's known as a serializer that will take an instance of this python object and convert it into something that we can actually return and interact with from our API so it might seem a little bit abstract right now but bear with me it will make sense so we're going to say from the rest frame workk import the serializer where they going to say from do models import the blog post like so now we're going to go and create a class and we're going to call this the blog post serializer and this is going to inherit from the serializers doth model serializer so a lot of this is already done for us we just need to kind of hook up the different models now we're going to create something known as a meta class so we're going to say class meta and we're going to say the model is equal to the blog post and we're going to specify the fields that we would like to serialize and return when we use the serializer now the fields are going to be ID title content and the published date now you don't have to do all of these but if you don't include one of them it's not actually going to be returned uh in the API okay so you need to make sure that any field you want to be returned from your API you specify in here ID is a field that will automatically be added to all of our models we don't need to specify that when we create the model okay so we now have our serializer and we have our model the next step is to create a view that actually utilizes the model and the serializer so we're going to go inside of views.py and we're going to use some D Jango rest framework views now the really nice thing about the Jango rest framework is that it provides some default views for creating updating deleting Etc and doing the standard operations that you would with a rest API hence why we installed the Django rest framework so what we're going to do here is import a few things from Jango rest framework they give us something known as a generic View and that's that nice view that you saw when I gave you that original demo at the beginning of the tutorial so we're going to say from the rest framework import generics like so now generics contains generic views that can be used to update delete Etc any type of model so what we're going to do is create a simple view for now and we're going to say class blog post list create is equal to the generics do list create API View and then inside of here we just need to specify a few basic things the first thing we need to do is specify the query set now the query set is going to be equal to blog post which we're going to import in a second dot objects. all that

means we now need to import blog post so we're going to say from doels import the blog post now what we're doing here is we're just getting all of the different blog post objects that exist this is how you do it using the omm in Django the object relational mapping if you want to get all of them you simply say the name of the model which is what we specified here doobs doall gives you all of the instances of our blog post now that is our query set and then beneath this we specify the serializer that we want to use when we're actually returning this data so we're going to say serializer class is equal to and then this is going to be the blog post serializer which we also need to import so we're going to say from dos serializers import the blog post serializer now that's actually it we've just created a view we now need to connect it with a URL what this view will provide is a way for us to create a new blog post and to get all of the blog post that exist you can extend this and you can make it more complicated but right now just with what we have once we add a URL for it it will automatically allow us to create and then return all of the different blog post objects that's what this generic AP I view does there are a ton of other generic API views you can look them up from the documentation and I'll show you one more after this now that we have the view though what we need to do is specify a root or a URL that allows us to access the view to do that we need to make a new file so we're going to make a file inside of our API app called urls.py now there's a two-step kind of routing system here what will happen is inside of my site in the urls.py file so I know it's a little bit difficult to see let's close that if you go into my site and urls.py you can see that we actually have the ability to forward URLs to different apps the way this works is we will always start by looking in this file and we'll look for a specific pattern and then we'll take the remainder after that pattern and forward it over to a different app in this case if we go to admin so we go to the URL of our server and then admin it's going to take whatever comes after the slash here and it's going to forward it over to the admin. site URLs to parse the rest of the request in our case what we want to do is we want to take any URL so anything that they type in and we want to forward that over to our API app the reason why we have this file here is you might have different apps and they may all have different prefixes and then have similar types of URLs you'll see what I mean in a second but for now we can actually remove the import from admin because we don't need that and we can change what's here to say path and just be an empty path and then we're going to write a function called include include and we're going to include the api. URLs and we're going to import the include function now what we're saying here is whenever we receive any type of URL it doesn't matter what it starts with what it ends with doesn't matter we're going to take it and we're simply going to pass it over to the api. URLs

file where it will then be parsed from there keep in mind that if you wanted to you could do something like API slash and now if we want to access a URL that we write inside of our API app to get get there we would do something like the server URL slash API slash and then whatever the root is for our different views again I know it's a little bit confusing but you'll see what I mean when we write the URLs in our API so now we have the URLs being forwarded over to that app so let's just make this an empty string again let's go over to urls.py now and let's write these different URLs and connect them with the views that we just made the view is the root it's what's going to be rendered onto the screen or what will return data and the URL is how we get there so we're going to say from Django do URLs import path and we're going to say from dot import views the dot just means the current directory or the current package in this case so we're going to do that and import the views file where we will then be able to access the view that we wrote we're then simply going to say the URL patterns is equal to an array and inside of here we're going to make a new path and the path we're going to have is blog posts and then slash and make sure you have this trailing slash now what we're going to do is we're going to say views. the blog poost list create. asore View and we're simply going to give this a name and we're going to say this is the blog post view create view now I know it's a little bit long but let's zoom out here so you can read all of it what we're doing importing the path from the D Jango URLs we are importing the views and then what we're using is something known as a class-based view that's what we've created here this is a generic View and the way that we render it is we say views dot whatever the name of the class is do as view this pretty much means anytime we go to the blog post route here we're going to be brought to this page where we can then interact with the API now keep in mind we're going to do this from the browser but you can do this from postmen you can do this from a different server you don't have to do this graphically it's just nice to be able to view it graphically as we go through the tutorial so we were actually just about to run the server but before we do that there's actually one step that we need to do which is migrating our database now we'll talk about that step but first of all i' just like you to go into the models.py file and notice that we have this Auto add now it actually needs to be Auto now add I just had or I just found that story while I was looking at it so let's just fix that small mistake there that I had inside of the published date now what we're going to do is we're going to bring up the terminal and we're going to apply a database migration now anytime you create a new model or you make any changes to your models what you'll do is you'll make a new migration and you'll apply it and what that's going to do is use the D Jango om to automatically create the correct SQL tables or do whatever it needs to do essentially in the database for this to work properly so the way we do that is we make sure we're inside of our mysite directory and we type Python 3 manage.py

and then we're going to say make migrations now make migrations is going to create the files that will specify what migrations need to be applied and this case you can see we're going to create a model blog post now we need to actually apply the migrations and to apply the migrations we simply type the command migrate so Python 3 manage.py make migrations wait for that to finish then Python 3 manage.py migrate when we do that we'll see that this is actually applied and we've now created the tables in our SQL database which is called db. SQL light 3 now we can clear and we can type the command Python manage.py and

then run server and when we do that it will start running our python API so let's go ahead and do that we shouldn't get any errors here again make sure you fix this so it says Auto now add and you will see that we have this URL which is where our API is being ran so what we can do now is simply go to this in our browser so I'm going to control click it it's going to show me an error right away don't freak out the reason for this is because we need to go to the blog post URL so we're going to go slash blog post and we when we do that we'll see our API view now inside of here we can create a new blog post to do that we can do something like title and then test content and we can click on post and now you'll see a new blog post is created we have the option to call get when we call get it gives us an array that contains all of these now in this case we get a nice graphical view showing us how the API works but of course you can use this from something like Postman curl the command line doesn't matter you just need to call the API correctly with the correct headers and the correct field names in this case the field names will be title and and content those are the two required ones to create a new blog post okay so that is that route but I now want to shut down the server and I want to show you how we create some other roots that allow us to do some more specific things so first of all let's go into the views.py file and I want to mention that you may have noticed we didn't actually have the ability to delete any of the blog posts that we had now we can make it so that we can delete all of the blog posts in a single rout the way we can do that is by adding a root here to this API view because we can actually override the de default view sorry or we can actually create a new route that allows us to delete individual blog post so let's do that first and then I'll show you the override what I'm going to do is create a new class here and this is going to be the blog post and then the retrieve if I spelled that correctly update and Destroy view now inside of here this is going to be generics Dot and this is the retrieve destroy API view or actually sorry retrieve update destroy API view I know it's a little bit long so let's make this a bit easier to see and inside of here we're going to specify the same thing that we did before so we're going to copy the query set and the serializer class we're going to paste that inside of here and we're going to have the lookup field be equal to PK which stands for primary key which in this case is going to be the ID of our uh blog post now all this is doing is giving us another generic View this will allow us to access an individual um post and then we can update that post and delete that post so that's it for the view now what we'll do is we'll go over to our URLs and we'll make a URL to map to that view hopefully you're kind of getting the process by now so we're going to put a comma here we're going to make a new path and this path is going to be blog post slash and then in this case we're actually going to take a path parameter to do that we're going to say int colon and then this is going to be PK which stands for primary key we're going to have another slash then we're going to say views. the blog post and this is the retrieve update destroy View and we're going to say asore view like that and then we're going to give this a name and we can just say update I'm just giving it a short name for now because I don't want to write out the whole thing okay so what we've done here is specified hey we're going to have the same route with blog post but this time if you pass as a part of that path the ID of the object you want to retrieve what you can do is you can update delete it retrieve it that's it so now what we'll do is we'll go here and rerun our server we'll open it up again so let me bring it over here and if I go over to slash1 uh sorry slash blogpost SL one we'll see that we access the blog post with id1 which is the following one here so I could say test content update this and then put and then if I want I can delete this by click clicking on the delete button and now if we go back and we view all of the blog posts you'll see that we don't have any because we deleted them okay so that's almost all that I wanted to show you last thing I will quickly run you through run you through here sorry before we get into the deployment is how we override some of our views so if we go to views.py you'll notice that inside of here what we can actually do is override any HTTP method if we wanted to make this slightly more custom so in this case we're just using the generic views from Django because they're quite good and they're quite useful however we can override them so what I could do is I could say Define delete inside of here now for delete I need to take in self request star args and star star quars like that I then will say blogpost doobs doall and then I can say something like delete and then I can return a response which we're going to have to import in a second and we're going to say the status is equal to status. httpcore 204 noore content like that and now what we'll do is simply import from the rest framework uh actually yes from the rest framework we'll import status and then we'll say from the rest framework dot response import response and now what we've done is we've just ridden our own route and what this will do is add a delete route now to this API View and what will happen is it will just delete all of the different blog posts that exist and then return a response which is HTTP to a for no content which is what we're supposed to return when we delete something so just to quickly show that to you so you can see how easy this is and how it works now if I load up my server and I go to the page slash blog post okay you'll see that now a delete button exists and if we click that delete button it will actually just delete all of the blog posts that we have because that's the custom logic that we wrote here we just deleted all of the blog posts so to wrap up writing our views I'll show you creating a custom API view because this can be somewhat useful so I'm going to say from the rest framework. viws import the API

View and I'm just going to paste in a view here that you can reference if you want to do this so let's say we want to make our own Roots where we don't want to use the generic ones well what we can do is inherit from the general API View and then what we can do is write different methods BAS Bas on the name of the method we want to implement so in this case get but we could write post delete put patch Etc and they'll automatically be added for us to whatever the root is that we write now in this case this is simulating getting all of the blog posts that have a matching title so what I've done is I've said okay well maybe I'm going to have a query parameter here called title what I'll do is check if the title exists if it does and I want to find any of the blog post that have that information contained in their title then if I don't have a title I'll get all of the blog posts I'll manually serialize them by using the blog post serializer which I can do using this and then I'll return a response that contains the data provided from the serializer with a status of HTTP 200 now if I wanted to connect this with the URLs same thing I would just have whatever the name of this is do as View and then that'd be it I just simply have written a simple API view here that allows me to query based on the name of a object all right so that's going to wrap up the coding phase of this video the next thing that I want to show you how to do is actually deploy this API so you can share it with other people and you can get it running on a public endpoint because currently we have this simply running on local host on our Port 8000 but instead we might want to run this on a public URL so it's a bit more flexible other people can use it and we can actually test it in a production environment so to do that bear with me I'm going to run you through the steps now for the deployment here we're going to use something known as acorn acorn is free you do not need to pay to use it and I have teamed up with them for this video now what Acorn allows us to do is create something known as an acorn file now this Acorn file will specify our entire application and pretty much describe how it should be deployed we can then use this Acorn file to build an acorn image somewhat similar to something like a Docker image we can then publish that image and we can use it to deploy as many instances of our application as we like now this is great because it allows us to actually deploy this out quite quickly but it also allows us to share our application with other people by giving them the acorn image you're going to see what I mean and I made an entire video about this if you want some more details so I'll link that here by putting it on the screen and leaving a link to it in the description now the first thing we need to do here is actually create a Docker file which specifies how we run our application and then the acorn file will specify all of the services and the additional configuration we need when we actually want to deploy this out so we're going to make a new file inside of my site called called Docker file now I'm going to paste in the contents of this file and you can copy it from the links in the description all of this code will be available on GitHub it just doesn't make sense for me to write all of this out because it's fairly long so let's quickly go through what this file actually looks like we start by specifying the Syntax for the docker file we specify the image we want to build this from then we have that we're exposing Port 8000 because that's where our server will be running we specify the working directory we install some system dependencies we copy the requirements.txt file which means we actually need to take this file and we need to put it inside of the my site directory so make sure it's inside of there we then use the PIP command to install all of the requirements we copy all of the contents of this directory we specify the entry point which is using Python 3 and then the command that we want to run to run our server which is Python 3 manage.py run server and then we just specify we want to run it on all the public endpoints so the docker file is pretty straightforward it's just explaining how we actually run the the application now the thing that we're missing though is all of the instructions for deploying the application we know how to run it when we want to deploy an app we need to do things like set up the database we may need to do some database migrations there's some other steps and that's where the acorn file comes in now before I write that Acorn file I'm just going to create a new file inside of my site and this is going to be db- script.sh and this is going to be a

simple script that we're going to use whenever we start running our application so that we Mig the database and ensure that everything is set up so I'm just going to paste this in here again you can copy it from the link in the description we're running the Python 3 manage.py make migrations and then we're migrating just so that if we make any changes to the database that'll automatically be applied and when we do a new deployment the database that we're connecting to will have those tables created so this is a script that will run and I'll show you how we'll end up running that okay so now we have our Docker file we have our database script and we're going to go outside of this core directory here and we're going to make a new file file and we're going to call this the acorn file like this now for the acorn file to be a little bit more useful to us we're going to go into the extensions here in vs code and we're going to install the acorn extension it just gives us some syntax highlighting for this Acorn file so go ahead and do that and now I'm going to paste in the acorn file and quickly walk you through it now this can seem a little bit overwhelming but I promise you that if you read through the documentation it's not overly complex and there is all kinds of examples in the acorn docs for creating your own Acorn file this is just one for jeno okay so what are we doing here well inside of the acorn file we're essentially specifying everything we need to be able to deploy this application so we have some Services we have some jobs we have the containers that we need to run and we have the images that we're using so we start with some arguments and the one argument that we need is the Jango database name which is what we'll use when we actually create the database with whatever database server service sorry we choose to use so we're calling this D Jango DB we then have a service we're going to use now for our Django app we need to have a database this database can be many different types it could be postp it could be mongodb in this case we're going to use the Maria DB or Mariah DB whatever you want to call this what we do is just specify that for this database we are going to have the argument of Jango database name which is simply D Jango DB and we're using this image to essentially create a database for us so that's a service that's going to be a part of our deployed application we then have a job now what the job will do is initialize the database for us so we've called this dbit we're going to build images. app. container build which we specify down here so don't worry about that we have an environment inside of the environment we're using some environment variables or we're actually creating environment variables that come from this service so whenever we connect to a database well we need a username password address port and database name so we're just specifying what all of these are as accessible variable names that we can use within our code which we'll actually do in just a second we're consuming the database we have an entry point where we're using this script the bin bash and then the command that we want to run is this what this does is make our database script file executable and then it executes the database script file so we actually go and apply the migrations to our new database that we're connecting to we then have a container this is the container that runs our application the job is what runs beforehand to set everything up then what we do is we build the image that we specified down here which is inside of our Docker file this describes how to run the application we make sure we publish HTTP on Port 8000 we make sure we depend on this job so this job needs to finish before we start running this container and then we consume the database service that we specified up here we have some arguments for being in Dev mode like the directories that we're going to be using and then again we specify the variables that we need in our environment for this application to run properly down here we just specify the containers so where they're located essentially where the docker file is I know it seems seems a bit complicated but trust me if you've ever worked with Docker before or you start doing deployment you'll see how easy this is to actually write out okay so now that we have the acorn file there's a few last adjustments we need to make and then I'm going to show you how we run it so from my site we're going to go into requirements.txt and we're just going to add a package that we need when we start working with a mySQL database we're going to say MySQL clients like this which will allow us to work with the Mariah or Maria DB okay now that we have that what we're going to do is we're going to go into my site we're going to go to settings.py and we're going to make a few adjustments to allow this to actually be deployed because right now this is just for development on our local machine and we just need to adjust a few things so it works in a deployed environment so first of all we're going to go to alloud host and we're just going to add an exclamation or sorry an asterisk here which lets us run this on any host the reason we want that is so that we're able to run this on the acorn host which you're going to see in a second we're now going to make a variable here which is csrf uncore and this is going to be Trust trusted uncore Origins and this is going to store The Trusted Origins for when we submit our forms so we're going to have HTTP colon SL SL then this is going to be asteris uh do on- Acorn like that doio we're then going to copy this and we are going to paste it and just change this to https now we just need this so that when we're submitting the forms from the API site we don't get any csrf issues so we're just allowing all of the corn Origins because that's where we're deploying it to last thing we need to do is we need to import Os from the top here so we can access some environment variables and we're going to change our database configuration here so that we end up using the database that is created for us by the aord file for our deployment so what we'll do is we'll go down to databases and I'm just going to copy this in again you can copy it from the link in the description we're going to change default to be equal to this now what we've done is we've specified that that the engine we want to use is the back ends. MySQL engine this is different than the SQL light 3 engine we were using before we then want to connect to the Maria DB database so we have the name user password host and Port which all come from the environment variables which are specified inside of the acorn file so basic idea is Acorn file will spin up a database for us and then we want to make sure we use the environment variables that allow us to connect to that specific database so that's why we need to modify that inside of our python API or our Jango API okay that's all of the setup required now that we have that finished what we need to do is simply run the acorn file or run the acorn image or build the acorn image whatever you want to call it now in order to do that we first need to install the acorn CLI which is pretty straightforward let me get the command for you all right so I just have this page open which shows you how to install the acorn CLI you can go to docs. aorn

doio install we can Brew install this if we're on Mac or Linux which is what I'm going to do so I'm going to copy that command or you can use scoop this is a tool that you need to install on Windows okay so I'm just going to go into my command prompt here and I'm going to paste in this command I already have Acorn installed so this should be fairly fast and for you guys it should install the acorn CLI now we simply run the command Acorn login when we do that it will log us into our Acorn account again you can just use GitHub to do this it's completely free and once we're logged in which I already am what we can do is run our Acorn file now to do do that we're going to CD into the root directory here so notice we have our Acorn file and then we have my site and now what we're going to do is simply type Acorn Dev and then dot now when I do that it's going to start running the acorn image for me and it will actually give us a deployed version of our app and then I'll show you how we can share this so let's hit enter we're going to give this a second to spin up you're going to see that it creates a database for us it gets a public URL it exposes the port just runs through everything which is really really nice and then we can access access the deployed URL which you'll see in a minute all right so you can see now that it actually ends up giving us an end point because this was deployed successfully so I can click on this open it up and now you'll see that we have our API and if I go here to SL blog posts we're able to view the API view but now this time it's on a publicly deployed https URL endpoint which could be quite useful especially in deployment now the way that I ran this is using something known as Dev mode Dev mode will give you some file synchronization and it just really quickly spins this up for you so you can start using it and you don't have to actually go to the acorn website however what I want to show you now is how you would actually share this with someone else so what you can do is simply create an acorn image you can publish that image and then you can just give someone the URL to that image and now they can create the same instance of your application pretty much instantly they can mess around with it they can break it they can change it and it doesn't affect your deployment because every time you deploy the acorn image you get a new deployment which is quite cool so let me show that to you and then we'll explain more about how it works all right so I just shut this down by hitting contrl C and now we're just going to run a command that allows us to actually kind of tag this image and upload it to a registry so other people can use it and you can share it now to do this we first need to log into a registry now the one most of us are familiar with is the docker one so that's what we're going to sign into we're going to say Acorn log and then index. docker.io now I'm already signed in but in your case just sign into to it and this allows you now to have a place where you can publish your different images there's a bunch of other registries as well uh but this is one that I'm sure most of you are probably going to want to use now what we're going to do is simply build the image so we're going to say Acorn build- T which stands for the tag name and then we're going to do the name of the registry which is index. docker.io slash this is going to be your username on the registry in this case mine is Tech with Tim Z and then you're going to give the name to your image so I'm going to say Jango tutorial YouTube like that okay I'm going to go ahead and run that it's going to build an acorn image for us once the acorn image is built we can then push it to the registry and we can really easily share it with other people all right so this has been built now that it's built what I want to do is push this so I'm just going to change the command so that rather than having Acorn build we simply say Acorn push and then it's just the same name of the image we just created when I do that it will push it to the registry and then we can simply take this name and we can create a URL that allows other people to deploy this image really really cool it's just like a Docker image except in this case it specifies the entire deployment of the application which makes it really easy to share and deploy without interfering with your actual code so you can see that this finished so I'm just going to copy the name of what it is that we deployed there or what we pushed sorry and now we can simply go to the acorn website so let me just load this up here so I just simply went to here and what I can do is click on my username I can click on shared acorns and then you can see that it shows me actually all the stats for different acorns that I have shared what I'm going to do is just paste in the image which is this and notice it gives me a link that I can share with people so now I can copy that link this will actually be what's in the description by the way and now you can simply paste that link into your browser when you do that it's going to bring you to the acorn platform it's going to show you the acorn image so we can view the source file of the acorn file we want we can pick where we want to deploy it to so what project or what folder we can just press on deploy and it's going to deploy a new instance for us so this is exactly what you guys will be able to do whatever you just saw pretty much in my terminal is going to happen exactly for you in your own Acorn account and you're going to get a running instance here that allows you to mess around with it now it's worth noting that these are not going to be deployed persistently they are going to be in kind of a container that will die after a few hours you can extend that time and you can still use Acorn to deploy persistently but it's a little bit of a different process and not something that I'm going to get into in this video all right so the acorn is finished provisioning here and you can see that it is running what I can do now is I can simply click on this button and it's going to bring me now to a URL this is different than the one that we have before where this is deployed now same thing I can go to SL blog post I now have a new instance of the API here that I can mess around with and do whatever I want with so there you go that's pretty much it for deploying with Acorn I think this is really cool especially because in a development environment it makes it really easy to share this with other people I now don't need to spin up a virtual private server I don't need to go do all this Linux config I just write a Docker file like I normally would I write an acorn file I now have the entire deployment of the application described I can just send this to my boss I can send this to a coworker and they can spin up their own version they can have it alive for as long as they want they can mess with it they can break it and it doesn't affect me and I don't have to constantly change things around or fix things that they're breaking anyways guys that's going to wrap up this video as a reminder all of this code will be available from the link in the description if you want to deploy your own version of this API you can click the acorn link that I have below and I look forward to seeing you in another [Music] video

2024-03-09 11:58

Show Video

Other news