Welcome to this section, great to have you onboard. My name is Henry and in this lesson, we will continue with the process of building blog application with Python 3 and Django web framework, Django works well with Python 2 or higher. In the first lesson, we created our virtual environment and we also learned how to activate the virtual environment.
In our first lesson, we created a virtual environment called djablog, I will navigate to that environment on my terminal. In order to use our virtual environment, we need to activate it and we can do this by running the following command: . bin/activate After activating our virtual environment, the next thing we need to do is to install Django. We will install Django using pip, pip is the preferred method for installing Django. Python 3 comes with pip pre-installed, to install Django run the following command: pip install django Here is a screenshot of my terminal showing the installation.
Great, we have successfully installed Django in our virtual environment.
Creating Our Project
Django provides a command that allows you to easily create an initial project file structure. Now, lets run the following command to create our project: django-admin startproject mysite This command will create Django project with the name mysite. I am going to open this project on my pycharm text-editor so that we can have a look at what was generated, we will open mysite directory. Here is the screenshot:
Here is a screenshot of mysite directory:
Now let’s see what are these files:
- manage.py – A command line utility to interact with your project. You don’t need to edit this file. When you open the directory mysite, as you can see in the editor image above, we have the following:
- __init__.py – An empty file that tells Python to treat mysite as a module.
- settings.py – settings configuration for your project. It contains initial default settings.
- urls.py – This is a place where your URL patterns live. Each URL defined here is mapped to a view.
- wsgi.py – Django uses WSGI as a primary deployment platform. This file is used both by Django’s development server and in production WSGI deployments.
Since we are using python 3.5 which comes with SQLite database built-in, we can use this database in our project for development. If you plan to deploy your application in the production environment, you should use an advanced database such as MySQL, Oracle or Postgres. The generated settings.py files include a basic configuration to use SQLite database and the list of Django application that are running in your project by default. We need to create tables in the database for the initial application.
Now back to the terminal where we have our activated environment, cd to mysite. After changing directory to mysite, run this command: python manage.py migrate You should see the following in your terminal:
Here, the table for initial migration has been created in the database.
Running Django Development Server
Django comes with a light weight development server to run your code without the need to spend a lot of time to configure a production server. When you run this server, it keeps checking for changes in your code. It reloads automatically. To run our server, run the following command: python manage.py runserver You should see the following output:
Now open your browser and go to this URL: http://localhost:8000/ you should see the following on your browser:
Congratulations on your first Django-powered page! Remember this server is only meant for development use and is not suitable for production.
Goals achieved in this lesson
- We were able to install Django in our virtual environment.
- We also learned how to create Django project.
- We learned about the initial project file structure and briefly explained them.
- We learned how to create tables in our database.
- We also learned how to run our development server and navigated to our browser.