We have spent a good time learning Django. The Framework is simple, modular yet very powerful. There is a catch though. We have been running our website on the local computer.
A website’s true application is on the web. I am sure we all concur on that. So, we have to put out a website online. It is also referred to as going live, on a production server, etc….
In this article, we are not going to make a new Django application from scratch. Actually, we are not going to develop anything on Django in this article. We are going to learn how to upload it on a server computer.
We are using the SQLite database in this article. You should change your project settings accordingly.
We are going to learn
- What is the Production Environment?
- Git – Introduction, and Installation
- Hosting Service of Choice – pythonanywhere.com
- Setting up the Production Environment
- Configuring settings for a production environment
- Uploading project on server
- Creating a superuser
- Making a Django web application
- Settings for security
- Settings for static files and allowed hosts
- Search the URL in the browser
This article is going to be a long one. It is highly recommended that you follow all the steps. Django is relatively easier to put on a production server. Still, the process takes some time of around 30 mins.
Let’s get started.
A server computer is nothing but a computer. The special thing about a server computer is that it’s connected to internet 24×7. It has a server program, backend and everything your website needs. Therefore, it is often called a production environment.
A suitable example could be smartphone manufacturing factory. The end product of this factory is ready for end-user. The same thing is your website on the server. The server is the factory which processes your website. The user consumes the processed product. Every website on the internet is on a server. That server provides a developer a production environment.
A production environment is capable of running and serving your website to users. The production environment can include:
- Computer Hardware
- Operating System like windows, ubuntu, etc…
- Support for a programming language like Python Interpreter
- Web Server like Apache, Nginx, etc…
- Application server
- Databases which our website is built on
- Some extra tools like a load balancer, proxy server, etc…
So, there are a lot of components on a web server. Server technology is a domain in its own. We need not bother with it. For our purpose, we just need a server which is connected to internet 24×7.
That server can be situated in local premises or you can choose a hosting platform. Generally, developers prefer hosting platforms. It has its own benefits. We are going to learn in this article how to host our website using one of these hosting platforms.
Hosting platforms provide Infrastructure as a service to developers. So developers need not buy hardware. they can skip to the part of software configuration. This is an application of cloud computing and is called IaaS (Infrastructure as a Service). These hosting companies can also provide platform as a service. You can actually create a computer with chosen hardware specifications. You can select everything from OS to software installed. Cloud technology has lots of applications. We are going to use one of them.
Hosting platforms are better for every developer who is a beginner. Since we as developers get a chance to showcase our work for free. We will be using pythonanywhere.com for this purpose. There are several hosting platforms available. like AWS, Heroku, Digital Ocean, etc.
You can check out this website to know which hosting service provide Django support:
Git – Introduction and setup
This part is for developers who have no experience with Git. Feel free to skip if you have installed git and have an account on GitHub. We are going to introduce Git and GitHub in this article.
So, Git is a version control system. that means it will track your progress on the software. There are many similar tools but currently, Git is the most popular. Beginners often think Git and GitHub as the same thing. They are very different.
As stated earlier, Git is a version control system. it is a piece of software that you can install on your computer. You can track the changes you made to the project. It is a powerful software for fast and manageable development. You can make repositories of your code and then mark what changes were made. It really helps out when you have been making changes to project over a long period of time.
Download and Install git as instructions are given on the link above. Once you have installed Git, try executing this command in Terminal/ PowerShell.
If this works, then you have successfully installed git on your machine. It is an amazing utility for project management and version control. You should learn about this tool. It will help you a lot in project collaboration and working with a team.
GitHub is a hosting platform for Git. As you have experienced Git is a command-line tool and can be difficult for beginners. GitHub provides a graphical UI so it is fairly easy then Git. Although, GitHub is really powerful when you use it alongside Git.
Github.com provides developers a place to freely host their project code. That can be manipulated by the Git CLI tool. Its integration is what makes this tool awesome and so productive.
Just open https://github.com/ for making an account. This GitHub account is a developer’s portfolio. So, I advise you to upload your work on the platform. This will help you a lot in getting a job or recommendation.
Okay, so you need to make a GitHub account. Just follow along with the instructions provided by the website. ok so, now we are prepared to make some changes in the project.
Let’s get started with configuring some settings in Django.
Hosting Service: pythonanywhere.com
We are going to host our website on pythonanywhere.com. its really easy and you get to do it for free. The free account does have some limitations. It is more than enough for our Django Project. We will also need to have an account on this platform.
So, visit pythonanywhere.com and make a free account now.
click on the signup link and follow along with the instructions.
Making an account on PythonAnywhere
Click on Create a Beginner account link. Then fill in your name and email. Then we are good to go.
Setting up the Production Environment
Now we need to make a repository on GitHub. Open github.com and make a new repository.
Making a new Repository
Use a small and sensible name. once you have made the repository start git bash program. You can also execute these commands on the command line or the terminal or PowerShell.
First, move to the directory where the project exists. For example –
The highlighted folder contains manage.py and other apps. You get the idea of the root. now you need to move to this level from command line.
Once, you reach this level type in this command:
git init command
if you are using git bash then this will be the output. You can run the same command in PowerShell, Windows Command Prompt or Terminal.
This command will initialize an empty git repository. Then you can add code or project in this repository.
Command to add your project in the repository:
git add project_name
git add command
this will add the files in the repository. now we can commit the changes to the repository. we will be using this command to commit the changes made so far. You will be using these commands a lot.
git commit project_name -m ‘first_commit’
git commit command
if you see an output like this then we have done a good job so far. git commit is used to commit your changes in code. We have given a message of first_commit in the command. That can be any message you want to associate with your code. That message is displayed on GitHub or when you track your changes. It’s really helpful if these messages are short and accurate.
Now, to add these files to a repository on GitHub.com. type in this command.
git remote add origin repository URL
now, for repository URL you can copy the URL of the repository tab. as you can see in the URL bar. Just paste it in place of repository_url. that’s it.
git remote URL
git remote command
one, last step we need to perform. We need to push this code to online repository now. For that execute this command:
git push -u origin master
git push command
when you execute this command, this popup appears. You need to login for the command to be executed. As these changes will be registered by your name on GitHub.com.
git push command executed
your push command was successfully executed. Now, you can check the repository online. Our GitHub work is now complete.
Now, we are working with pythonanywhere.com. open the url pythonanywhere.com and log in to your account. Click on the consoles link. There click on Bash link.
Opening Bash console
As you can see the highlighted boxes in above screenshot.
Since, its pythonanywhere.com you get python pre-installed. This is one of the reasons PythonAnywhere is chosen as hosting services.
We will make a virtual environment for our project to run.
Run this command in bash.
mkvirtualenv –python=python3.7 env_name
Making virtual environment
Now, we will install Django on the server. It’s really easy. Just execute this command.
pip install django==2.2.1
we have been using that version of Django. it is the latest version currently.
Uploading the project on Server
Now, finally, it’s time to load our website on the server. Now you will just need the repository URL for this. Then execute this command in bash.
git clone repository_url
Now, we will migrate all the models. As we have done a lot of time in our project. Just move to the manage.py file and run the commands. The commands are the same for PowerShell and Terminal users.
We will now create a superuser. Since we have cloned our project only code was cloned. There is no data entry on the website’s database.
Execute this command to make superuser.
Python manage.py createsuperuser
You should know, this user has total control of the project. Therefore, make the password strong. Use original email id.
Making a Django web application
Now, follow along the pictures and instructions to get everything right.
Click on web link and then on create a new web app link. As highlighted in the screenshot.
Make a new web app step -1
Click next, when a prompt comes. Then select manual configuration option.
Select python 3.7 option.
After clicking next you will get to this page.
You can click on the link highlighted but you will see a different page. We need to configure the WSGI file to redirect to our web app.
Now scroll down to virtualenv section.
setup your virtual-environment
your virtual environment will be: home/user_name/.virtualenvs/env_name
now, to set up the path to the actual project folder.
Move up to code section and enter the path in the highlighted area.
This path can be easily looked up to in Files link.
From there you get the path .The path is a bit long, but it should show no errors. Remember that your folder contains manage.py at the inner level.
Click on the highlighted link to open the file. When the file is
Open erase whole code and paste this code.