Python Django Tutorial 2018 for Beginners Part 1 – How to Create a Project

Python Django Tutorial 2018 for Beginners Part 1 – How to Create a Project


– What is up, how’s it going, this is Qazi from cleverprogrammer.com. What I wanted to cover
in this series is Django. And what I wanted to cover
was the most commonly referenced Django documentation. I personally couldn’t
really find any videos of people covering it, so
I just wanted to do that, because, you know, I see,
everybody who starts learning Django, this is the first
place you’re gonna go to, like the docs, the Django
official documentation that shows you how to do
their getting started guide. And so I just wanna kinda
cover that in this series. This first video, we’re gonna do part one, and then every video after
that we’re gonna just cover one part, so like the
second video will cover part two of the documentation. It’s gonna be done in a
more conversational style, and I hope you enjoy it,
so let’s get started. So basically Django at a glance. The beautiful thing about
Django, what they’re saying here is that it lets you do
web development tasks in a really fast way, so
it’s pretty intelligent. Like for example, one of the
awesome things about Django compared to any other
framework, even like node.js, right, which is very popular,
and it’s a great, great, great framework for JavaScript,
but what makes Django really cool is that it
comes with a built-in and very robust admin interface. So, if you’re building
an app for your client where, let’s say you want
to build a blogging app for somebody, or eCommerce
app for somebody in Python, if you built it using
Django, it gives them an admin interface, so
they can just go in there and do stuff from there,
or you can manage the app for them from the admin interface. And, imagine this, you had to
spend zero development time on doing that, which means
that they’re gonna think you’re a fricking magician,
and they’re gonna want to hire and be like, wow,
how did this guy do this. So, the admin thing is pretty cool. So, and just right now I
just wanna kind of cover the overview, and then we’re gonna jump in to actually the parts
of the tutorial, okay, and you’re just gonna follow me along. All right, so, I’m gonna cover
this, kind of touch on this. So if you have some experience with it, it will make a little bit of sense. So designing your models, so in Django you can kind of design your
database and models like this. You don’t need to type in raw SQL queries, or mess with too many ORMs,
it’s just simply classes, so it’s literally like
Python and object oriented programming, and boom,
you have stuff populating your database, which is pretty cool. It also has a built-in kind of API, so it says, you know, as
soon as your models are done, the API’s created on the fly, no code generation necessary, okay. So here you can see that
if you create a model, let’s say that you’re
creating an app, right, and it has reporters, and
your app has articles, you know, well, what you
can just do is just be like Reporter.objects.all and it will tell you how many reporters are there. For example, if you made
a game app for let’s say Streetfighter, and you
did Fighter.objects.all, it might say empty set, which means you didn’t put in any fighters yet, right. But then let’s say you create
a fighter or a reporter with the name John Smith, and you save it, then, when you check
objects.all, all of a sudden it’ll say hey, reporter John
Smith actually does exist. So, that’s pretty cool,
and a really nice way to interface with it. And then also you can search for things in a really easy way too. So you can search in
your database with an ID, but you know, a lot of times
a more human way to search instead of an ID is, like, searching with what does the name start
with, or if the name contains the word mith, which
Smith actually contains, and it’ll match it. So then what you can do on
your front end later is, right now, you’re not gonna
have your client interface with your app from the
code command line, right. What you can do later is
then give them a front end, an interface, so they can actually type it in the search bar and look up John Smith, or type in mith, and it’ll
still find John Smith. Kind of like when you
guys go to Shopify stores, or YouTube, and type in a video’s name, and even if you’re off,
it’ll still find it, it’s using this contained
search mechanism, which Django comes built-in with. Another thing I wanna touch on is, yeah, if you have your
model, you can register them in the admin interface
with just a simple line that says admin.site.register, and then that model will, so let’s say you created
the Article model, you can now register it
in the admin interface, so then you can go an point and click in the admin interface, and then delete or create new articles or what not. For example, when you
have a WordPress blog, you know how you have an
admin interface there, and you can create a new
blog post, or delete a post or edit, you have full CRUD functionality. Well that’s what it mimics
by you just doing this. And that’s you creating code from scratch, that’s kind of powerful. And then, obviously, let’s
you design your URLs, so how do you want your URLs to be? So for example, you can
have it like, you know, myapp.com/articles/the year, followed by whatever, right. So you can create your own
URL parameters and whatever. So this is pretty standard,
but Django let you do it in a really clean way,
especially with Django 2.0, their latest release. All right, so, writing your
views is also pretty easy. You can have it just like
return an HTTP response or HTML file you made. So for example, here you can see we render and return this archive.html file. So if you create an HTML
file in your templates, then it’s gonna be there. Again, if you’re watching
this, and you’re like, I’m a complete beginner,
and I have no idea what the heck you’re
talking about, don’t worry. If this part is just not making any sense, skip a little bit ahead to the part where we jump into the tutorial. This is just for people
who are maybe coming from other frameworks,
and they wanna grasp how Python and Django is working. So, you do need some
experience to understand what I’m talking about here. And then yeah, templates are using it’s using Django templates,
which is kinda like Jinja. I’ll go into this later. So, at this point, let’s just get started. All right, so for your
installation all you really need to do is make sure you
have Python installed, so for myself I got Anaconda installed, which is what I would recommend for you. So if you do anaconda download online, kind of install that,
and that’s pretty much all you need to go on. And then, obviously,
install Django as well, which I’ll show you in one second, okay. All right, and then you can go ahead and create a project. So we’re gonna just get
started from scratch and follow along with this
tutorial, so let’s get started. Okay, so I’m gonna open
up my command line here. So I’m just gonna open up my terminal. If you’re watching this,
you can open up your, you know, CMD on Windows, or, what I recommend installing
is CMDER on your Windows. Basically just see if you
have anaconda installed. And if you do, and if you type in conda, this thing should come up, okay. If you don’t have anaconda
installed, that’s okay, you can still follow me
along, but what I’m doing here is kind of like for best practice. So, if you want to do it
like the best practice way, then I recommend that you
get conda installed here. Let me just make this
a little bit smaller. All right, so basically
what I wanna do here is first, check my, so, what
I’m gonna do is install my environment with conda,
okay, so I’m gonna do, and again, installing
anaconda, and like making you understand everything
about a virtual environment is outside the scope of
this specific tutorial. You can look up stuff like
how to install anaconda on Windows, or how to
install anaconda on a Mac. But I’m just gonna go ahead
and do conda, or first, I’m gonna create this
project, so let’s just see. I’m gonna go to my GitHub. All right, what I’ve
also done is I have gone into preferences in my Atom, and I have went in under install, and I’ve installed terminal, platformio-ide-terminal,
and I’ve installed it. So then the beauty of that is
that when I’m coding, right, if I’m coding I can just pop
open my terminal right in here, and I don’t have to leave my Atom. So, I will do django-admin startproject mysite like that, and then go into mysite. Okay, and then I will come
over here, and I will open, I’ll go to GitHub, and
then I’ll go mysite, and then just click that, okay. That’s about it. This is just so if you
guys are following along, like, I don’t want you to get confused. I’m gonna open up my Chrome. So they’re saying that this is pretty much what it should look
like, and for us it does look like that, right, we have mysite and it has all these files inside of it. And you can see mysite,
and it has all these files inside of that, okay. And then they say, hey, just go ahead and run python manage.py runserver, so let’s go ahead and give that a try. So I’m gonna open up my terminal, and how I do that, how I
open up that little prompt is by doing command + shift + P. For you on Windows, that
might be a different command, like, maybe Control + shift + P. But yeah, for me that opens it up, and then I just click terminal,
pops open my terminal. I’m gonna do conda create –name mysite python=3.6 And I will activate this environment. And now, I will install Django. Okay, so if I do pip freeze
it should show me that Django and a bunch of
other stuff is installed. Django right here, so we’re good. All right, I’m just make the fonts bigger so you guys can see it a lot easier. Okay, so now we’re gonna try to run this. Python.manage.py runserver. And we will go to this URL on our Chrome, and see if it shows us something. Okay cool, so it says
install worked successfully, congratulations, and, believe it or not, this is actually pretty exciting
because this is the first hint that your app is actually running. Now to this we’re gonna add
a lot more features to it and make it really awesome. But right now it’s being actually served over your localhost and it’s running. And once we get it to a
point where it’s doing a lot of cool stuff, we’ll develop
it locally and then later on, what you can even do
is then put it online, so then anybody in the world
can use your Django app. Let’s go back, and let’s take
a look at what the tutorial is telling us to do at this point. So it says that you should
see this stuff, and we do. And then it says ignore the warning about unapplied database
migrations for now, we’ll deal with the
database shortly, cool. And it says we have started
a Django development server, which is a lightweight web
server written purely in Python. We’ve included this in Django
so you can develop things rapidly without having
to deal with configuring a production server such as Apache until you’re ready for production, cool. All right, and they say
don’t use the server in anything resembling a
production environment, it’s intended for
developing, cool, no problem. All right, and now they
want us to get started on creating the polls, the polls app. Now that your environment
– a project – is set up, you’re set to start doing work. Each application you write
in Django consists of a pure, of a Python package that
follows certain convention. Django comes with a utility
that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories, cool. All right, projects versus
apps, so what’s the difference between a project and an app? Now, the cool thing with Django is that everything is considered an app. So let’s say that you create a website that has a blog, that has
a eCommerce capability. Now, the blog is considered, so let’s say you created this
website with Django, right, the blog would be considered a Django app, and your eCommerce store would actually be considered a different Django app. So there would be two different apps that your web application is comprised of. That’s how Django handles the logic. So, basically it’s one project that has multiple apps inside of it. That’s pretty much what
they’re saying here. Okay, so an app is a web
application that does something, okay, so web blog system, a
database of public records, or a simple poll app. A project is a collection
of configuration apps for a particular website. A project can contain multiple apps, an app can be in multiple projects. So, you can have one app that
you build for one project, and you can actually have
it in different projects that you’re doing, which is really cool. It’s like plug and chug. Your apps can live anywhere
on your python path. In this tutorial, we’ll
create our poll app right next to your manage.py file, so that it can be imported
as its own top-level module, rather than a sub module of mysite. To create your app, make sure
you’re in the same directory as manage.py and type this command. Okay, so now this command,
manage.py startapp polls we’re gonna do that, okay. So, we’re gonna break our
server with control + C, okay, so I did that. I’m gonna hit ls, and it looks
like I’m at the same level as my manage.py file. If I hit pwd, pwd, it’ll
show me where I’m at. And now, I’m gonna do
python manage.py startapp and the app is called polls I believe, just let’s double check. Okay, so now our polls app is created, and let’s just take a
look inside of polls. Okay, so inside of polls you can see that we have a bunch of, oh actually, let’s look right over
here, it’s easier that way. So polls comes with
migrations, _init_.py, admin, apps, models, tests, views,
there’s a lot of things that polls comes up with automatically, which is really nice. But a lot of this stuff is, you know, just kind of empty. The main things that you’re
gonna be working with is models and views,
okay, that’s the thing you’re gonna be working with all the time. When you’re developing apps for yourself later, you’re also going
to be playing around quite, you’re gonna be
adding stuff to tests, to make sure you can test your
app as you’re building it. And then migrations is
gonna be important because it’s gonna kind of let you
time travel in your database, so when we keep making
changes to your database, with migrations, you can roll
forward to a certain time. But let’s say things get really messed up, you can roll back to a
previous point in time. All right, so the this
directory structure will house the poll application. So this is the directory
structure that we actually saw. And now, they want us
to write our first view. A view is what lets you
go to a specific URL and then it returns some kind of response. So for example, so Django
works off of something called MVT, which is called
model view templates. Your normal apps, you
know, node.js, or whatever, those frameworks work off of
MVC, model view controller. So, to give you an example
of this in real life is, when you go to Google.com/, you know, when you go to Google.com
and you type in cats, right, or let’s say you go to Google.com
and you type in whatever, the response that comes to you, right, that maps to the current URL you are at. So how does Google know to
show you the Google logo and the Google homepage
when you go to Google.com? So, Google.com, when you type it in, it sees what your current URL path is, then it goes into the Google code base, and then it finds this HTML file that says, if somebody goes to this path that says Google.com, then
show them this HTML page that has Google’s image on
it followed by a search bar. And it shows you that, okay,
so it returns a response. You request something,
and it returns a response. If you go to apple.com/watches, or watch, I don’t know if that’s
actually a real Apple URL, but let’s just say you’re
trying to get the Apple watch. So if you go to apple.com/watch, how would it return to
you all of their watches? So, what actually happens
is, apple.com/watch will match that path in their code base and then it’ll see if
there is an HTML file that corresponds to it,
and it will show you that HTML file, okay, as a response. And that’s essentially
what we’re gonna do, but we’re gonna have a
very basic version of it. So, we’re gonna go in our polls/views.py. So I’m in my polls, and I’m
gonna go in my views.py, so when they say it like
this, polls/views.py that’s what they mean. And then here I’m gonna
say from django.http import HttpResponse, right, and then I’m gonna go here,
I’ll say def index(request) to take in a request, and
then return HttpResponse. And I will say Hello World, You’re at the let’s do it with double quotes because Hello World, You’re, what is it, at the polls index, right, and then the have a little comma here. This doesn’t matter so
much, this is just a string, so it doesn’t matter what you do. Make sure you always
save what you’re doing, because otherwise it
won’t take any affect. So make sure you do Control + S or Command + S the whole time. So I just created this, but the thing is it’s not gonna show up. So now, what I need to do is tell my app that when somebody goes to the homepage yo, you gotta show this
exact, this thing right here. This is the simplest
view possible in Django. To call the view, we
need to map it to a URL, and for this we need a URL conf, or a URL configuration. To create a URL conf
in the polls directory, create a file called urls.py. Your app directory should
now look like this. So now notice there’s a urls.py here, which was not there before. So we’re gonna go in our
polls, right click here, and create a new file and call it urls.py. And now, in our urls.py we
gotta add from django.urls import path, from django.urls import path, and then we’re gonna do from import views and then we’re gonna do a urlpatterns. So, what you wanna do is try not to indent but use four spaces instead,
okay, one, two, three, four, if it doesn’t automatically
bring you to the right place. And I wanna do path, so if
somebody goes to the empty path then I want you to go into our views file and use the index function,
and we’re gonna call give it a name index. So what does this mean? If somebody goes to,
let’s say your website is called john.com, right. If somebody goes to john.com
followed by nothing else, so not like john.com/article,
/blog, none of that, it’s just john.com, your
homepage, what happens? Well then, we say go into the views file and run the index function. So under views, this
is the index function, and it’ll run that. We’re naming it index, so then later, if we want to, from
our templates, our HTML if we want to refer to
this specific URL path, we can just call it by index
and it’ll reference it. Okay, the next step is
to point the route URL configuration at the polls/url module. So, URL conf, and we
gotta point it to this. So, all right, in mysite/urls.py add an import for django.urls.include and insert an include
in the urlpatterns list, so you have this. So, we’re gonna go in our
mysite/urls right here. In here we’re gonna the,
this line, okay, because we’re saying if somebody
goes to the path polls, then run the polls URLs. Okay, so from django.contrib import admin from django.urls import, from django.urls import include, path. And that’s because we’re
gonna do the include thing right now, just like
that, and then hit Save. So if somebody types in john.com/polls, now we’ll say hey, try
to match this pattern by going to the polls.urls file. Well, where’s that? That’s in the polls app, and
it’s this file right here. So then it’ll go to this file, and then it’ll match this
first guy and it’ll say okay, I’m gonna run the home function. So anytime somebody
goes to anything polls, it’ll refer it to that
file, that URLs file. All right, this is what
they’re basically saying here. The include function allows
referencing other URL confs. Whenever Django encounters
include, it chops off whatever part of the URL
matched up to that point and sends the remaining
string to the included URL conf for further processing. The idea behind include is to make it easy to plug and play URLs,
since polls are in their own URL conf, they can be
placed under, or any path, and the app will still work. Okay, you have now wired, so now it says you should always include,
you should always use include when you include other URL patterns. And now it says you have
now wired an index view, let’s verify it’s working,
run the following command. Okay, so now we’re gonna run this thing, and we’ll go to our localhost,
or this http port over here. And hit run. And now we are getting an error, oh yes, basically, you have to do
/polls, okay, of course. Because it’s not a homepage
thing that we’ve added to, because I just saw this and
I was like, oh, homepage. But it’s because we gotta
go to the polls path. So everything after, so it’s like, when you put this online,
when you put this app online, it will essentially be
like yourapp.com/polls is where you’ll have to
go to, and then it’ll know what to do, okay, so it’s
saying your at the polls index. And that’s exactly what we see now, cool. Go to this in your browser,
and you should see the text which you define in index. The path function is
passed four arguments, two required, route and
view, and two optional. At this point it’s worth reviewing what these arguments are for. All right, so we’re not going
to go into too much detail, it’s gonna be like still casual and we’re gonna keep moving forward. All right, awesome, I
hope you were excited watching it up until this point. Now, in the next video
we’re gonna go to part two. Before I let you go, I
do wanna let you know that I have a course called
a profitable programmer coming up, where if
you’re more serious about, you know, becoming a web developer, learning Django more in depth, and especially learning how
to get clients with Django and Python, and to start
working as a developer, then definitely go to this page. We’ll put this link right
over here, and basically I want you to sign up and
put your email for our VIP wait list, so then you’re
the first one to know when this course opens
up, and also I’ll send you my resources that helped
me become a developer in less than three months,
and I’ll give you that as a gift for free, okay,
for joining this wait list. With that said, that’s
it, and I’ll see you in part two of this tutorial. For those of you guys
wondering what the hell those choice.set thing,
it actually comes built in and you can actually change that by passing in a related
name keyword argument when you’re creating
the question attribute, and you can give it
whatever name you want.

100 thoughts to “Python Django Tutorial 2018 for Beginners Part 1 – How to Create a Project”

  1. glad i found your channel – should I be using my MacBook Pro or my Linux machine to make it easier to code?

  2. Hi!!! I have more searching about dynamic menu that should be linked with database and applicable for all app but unable to get. Can you please help me?

  3. OK, It's 1am in Serbia, i've been practicing python and django whole day. My head is pulsing, but it was good, i learnd so much…

  4. I feel really happy to have seen your webpage and look forward to so many more entertaining times learning here. Thanks once more for all the details.https://www.besanttechnologies.com/training-courses/python-training-institute-in-bangalore

  5. just a comment: here without applying the initial migrate (that we are supposed to ignore) the page won't open, but doing once the migrations at the first beginning it made it able to open (:

  6. Hello guys, I am using edx bitnami and I have a problem. As you know for login page, actually when I click to button of login page, I don't wanna change my main page. I wanna create modal for my login. This was created with django and I don't know how can I update it. Please if you can help me, it would be very nice for me .

  7. from django.contrib import admin
    from django.urls import include, path

    urlpatterns = [
    path('polls/', include('polls.urls'))
    path('admin/', include('admin.site.urls')),
    ]
    After i wrote a code inside of mysite/urls.py ,then after sum error is occured.and also its not run properly.plz help me.

    The error is : File "C:UsersadminDesktopmysitemysiteurls.py", line 21
    path('admin/', admin.site.urls),
    ^
    syntaxError: invalid syntax

  8. The thing you were doing at 23:30 to 24:00 gives me this huge error when I try it. Here's the beginning. The rest is just the last part copied about a hundred times:

    creating migrations
    Traceback (most recent call last):
    File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
    File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
    File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)

  9. The problem with these kinds of tutorial is that u say u wanna walk us through how to do stuff and u just go ahead and skip the major part (the beginning) its really frustrating

  10. conda command is not working on my cmd and PowerShell and i installed cmde too but there also not working and then i tried on Anaconda prompt and it worked there why it is and what should i next i don't have github account

  11. I RARELY make comments on ANY YouTube tutorial…BUT! I have to say this series is WORTH YOUR TIME!! Very realist. Thanks so much for your time!

  12. i dont understand how he has that GitHub folder where I find my packages and how the hell do I create a PROJECT

  13. import turtle
    sq=turtle.Turtle()
    sq.pencolor("red")
    sq.speed(10)
    def square(length,angle):
    for i in range(4):
    sq.forward(length)
    sq.left(angle)
    for i in range(40):
    square(100,90)
    sq.right(11)

  14. <iframe src="https://trinket.io/embed/python/7121bc425b" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>

  15. I didnt understand anything……teach from the beginning …i didnt get the installation process…and also introduce the tabs and the functions of ATOM….

  16. Ok…Im pretty disappointed by this video.I have done with your first video which as you claimed was aimed to help beginners get into django frameworks.The beginning of the video was pretty decent then I got lost @ 8:23 when you said you'll install the environment using "conda"…then without warning you switch and you went on automatic mode. You talked about a github is it a folder that you have created for future projects? or is it a folder that is linked to the version control github that is created when you install the version control on your machine? and what about those commands? none of them work on my windows 10, I guess you are using a Mac but what about others like me who doesn't have a mac?….Man this tutorial is too confusing because it is claiming to be for beginners while actually it is taking so many things for granted 🙁

  17. you just dont know how to teach you are good programmer but you dont know how to teach you just jumped from comand lind to the project with out explaining how the fuck you got there

  18. hlo Qazi
    I know c and c++ language now which language should be teach??
    plz suggest me

  19. from django import settings
    ImportError: cannot import name 'settings'
    How can I resolve this problem on Windows 10 and django2.1

  20. Hey guys, soon we are going to be organizing an online Python & Django Meetup. Our target audience is beginners who have little or no knowledge about Django. However, you should know a little bit about Python.
    Do you want to join us? Follow the link – https://www.facebook.com/groups/322739708516805/

  21. Why the fuck does it take 4 hours to install ATOM and ANACONDA its harder to install than to actually program the shit fuck everything python related idk why installing it is the hardest part.

  22. HI Quazi i am not visible what you are doing i am not able to understand what you saying please help to zeroth knowledge about me please try explain and put how to install django and how to work on it basically

  23. What's the point of fast forwarding the official tutorial without explaining anything explicitly (I had to bring that joke :'D)?

    08:01 How to confuse beginners I "If you have Anaconda installed"
    08:50 How to confuse beginners II "I go to my github"
    10:17 "Just go ahead and run … and lets give that a try" -> 10:45 let's type something completly different
    18:11 Hey wouldn't there a small sketch be more helpful than your waving hands?
    21:46 You really indent manually?

  24. Awesome tutorial, I like your tutorial because you start with official documentation. Most of the tutorial doesn't do that…

  25. Coming from java and j2ee and understandig on how much is being delivered here out of the box, i just feel like arsenio hall in this chappelle sketch https://www.youtube.com/watch?v=6Akkg65Wizw

  26. how about if I would like to revive a website that is developed using Django framework and Python language? I have the files but I have no idea how to re-activate it. Tried to move it to a new hosting but failed. Hope you can give some lights on this issue, Thank you.

  27. this project tutorial isn't very clear you already have a project open and you skipped how to open and create a project… very lost

  28. Dear Sir, Thank you so much for the nice services over globe on Python. here are the required code for the project (making circle with square), kindly response
    import turtle

    tasir_turtle=turtle.Turtle()

    def square(length, angle):

    tasir_turtle.forward(length)

    tasir_turtle.right(angle)

    tasir_turtle.forward(length)

    tasir_turtle.right(angle)

    tasir_turtle.forward(length)

    tasir_turtle.right(angle)

    tasir_turtle.forward(length)

    tasir_turtle.right(angle)

    tasir_turtle.color('blue')

    for i in range(200):

    square(60,90)

    square(0,4)
    #you people should also try this

  29. When installing Django and Anaconda to my desktop computer, depending on what processor my computer runs on will it slow my computer down?

  30. should i copy and paste the code from django documents or should i type the code like ur doing… is it more important for me to understand or just to practice?

  31. ok soooo after all that work i came back and my computer was F%%$$ Up, so i factory reset my pc and did it all again… but now i understand it better and understand virtual environments better!! im just curious to find out, why is it best practice to install Django inside virtual environments??

  32. hey, im new to python. im currently learning the basics. I wanted to set up my envoirement. Is it a good idea to learn python by building my own projects with django?

Leave a Reply

Your email address will not be published. Required fields are marked *