Welcome to a Django web development with Python tutorial series. Django is a Python web development framework, aimed at rapid development and deployment.
One of the more common questions people have is "which framework" they should use. There are quite a few for Python, with Django and Flask being the two most popular.
Flask is more of what we call a "micro" web framework. It is much "lower level" than Django is. This allows for more customization and control for the developer.
Django is much more of a higher-level framework, and imposes a set structure on the developer.
Thus, with Flask you can create systems your way, which is probably not most efficient, fastest, or secure way. With Django, you are a bit more constrained, but you are going to most likely do it the best way possible. As with almost all questions people ask me regarding which to use, the answer is: Try a few, and choose the one you like best. In the end, Django and Flask can be used to make the exact same websites!
To try Django, you need to get Django first (you will also need Python installed). This is exceptionally simple:
pip install Django
Problems? Check out the Django installation page.
You can also install a very specific version. I will be using 1.9 for this tutorial series. If you wanted to use that exact version:
pip install Django==1.9
Now you're ready to begin. It can take some time to digest the paradigm used by Django, but the way it works at the most macro level is you have an overall project, and then within the project, you have various apps, which you can call upon, using them in one or even multiple projects/websites. For example, you may make blog app that you use on a bunch of your websites for their blog sections, following the DRY (Don't Repeat Yourself) principle.
The main project has a urls.py
file that dictates the URLs that lead to the apps, and then the apps themselves have their own urls.py
, that dictates URLs from there. This concept was very foreign to me initially, but it actually makes a whole lot of sense. For example, your main project's urls.py
would have a pointer to /blog/ for the blog app, then your blog app would have its own directions for, say, /authors/ in its urls.py
. What is neat about this, is on one website, you might have many things, and a blog. Maybe you are running a business, so you point your main project to /blog/, and then the authors would be yourwebsite.com/blog/authors/. Then, using that exact same app, you could have another website that is just a blog, where the app location for the blog app is just / (the index), but the app's urls.py
definitions need not change. yourwebsite.com/authors/ will work here. What's more, this means you can extremely easily implement other people's apps.
Generally when I am working with large frameworks like this, the first task I always have is to actually make something appear successfully. Until then, I am not really sure I get it at all, so let's get a page to display some text for us. The first step here is for us to create a project. Navigate via the terminal or command prompt to an area where you want to work on your project, then do:
django-admin startproject mysite
This will create a directory called mysite. Within that directory, you have another one called mysite, along with a manage.py file. The manage.py file lets you easily interact with your project via the command line. The contents of the second mysite directory contain your settings and urls mainly. Broken down:
mysite/ - Simple container, Call whatever you want. manage.py - Lets you interact with your project via the command line. mysite/ - Actual project directory. __init__.py - Tells python this is a Python package. settings.py - Settings for the project. urls.py - URL rules. Django docs aptly describes as your table of contents. wsgi.py - WSGI magic begins here. Worry about this when it comes time to actually deploy to a server.
The paradigm of Django is that either a website is an app, or a collection of apps in most cases. We currently have our website, called "mysite" for now. Now we need an app, which is what we're going to be covering in the next tutorial. For now, run the following via the command line or terminal: python manage.py runserver
. This runs the local development server, which you can reach at http://127.0.0.1:8000. Head there and you should see the "it worked" message. If not, check out the error and try to debug. Also check out the Django install documentation if you're having installation issues.
Download the entire site's code for this tutorial here: Part 1