Webmaster and More

Smart WordPress & AI-Powered Solutions for Your Business

Lesson 2: Install Django on Ubuntu + Create Your First Project

πŸš€ Introduction

In this lesson, you will install Django on your Ubuntu server, create your development environment, and run your first Django project online using your server’s IP address. By the end of the lesson, you will have two working pages:

This lesson is intentionally beginner-friendly, with detailed steps and a troubleshooting section based on real issues that learners often face.


🧰 1. Update Your Server & Install Required Tools

SSH into your server:

ssh youruser@your-server-ip

Update and install required packages:

sudo apt update && sudo apt upgrade -y
sudo apt install -y python3 python3-venv python3-pip git

Verify:

python3 --version
pip3 --version

πŸ“ 2. Create a Root Folder for All Django Projects

A good habit is to keep all your Django projects in one folder:

mkdir -p ~/django-projects
cd ~/django-projects

Create a new project folder:

mkdir lesson2_project
cd lesson2_project

πŸ§ͺ 3. Create & Activate a Python Virtual Environment

A virtual environment ensures every project has its own isolated dependencies:

python3 -m venv venv
source venv/bin/activate

You should now see:

(venv)

If you re-login later, reactivate with:

source ~/django-projects/lesson2_project/venv/bin/activate

🟒 4. Install Django

Inside the active venv:

pip install --upgrade pip
pip install django

Check version:

python -m django --version

πŸ› οΈ 5. Create Your Django Project

Use the name config for the base settings:

django-admin startproject config .

Now your folder contains:

manage.py  
config/  
venv/

🌐 6. Run Django on Your Server (Port 1011)

Django’s development server can run on any port.

We’ll use 1011:

python manage.py runserver 0.0.0.0:1011

Open in your browser:

http://YOUR_SERVER_IP:1011

You should see:

πŸš€ β€œThe install worked successfully!”


πŸ—οΈ 7. Create Your First App

Every Django feature lives inside an app.

Create one called core:

python manage.py startapp core

🧩 8. Register the App

Open your settings:

nano config/settings.py

Find INSTALLED_APPS and add:

'core',

Save and exit.


✨ 9. Create Your First View (Home Page)

Open:

nano core/views.py

Add:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("""
        <h1 style='text-align:center;color:#007bff;'>Success!</h1>
        <p style='text-align:center;'>I finished Lesson 2 in Django!</p>
        <p style='text-align:center;font-size:14px;color:gray;'>
        View generated by Webmaster and More's Django server.
        </p>
    """)

🌍 10. Add URLs for Your App

Create:

nano core/urls.py

Insert:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.hello, name='hello'),
]

Then connect it in config/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

πŸ“„ 11. Create the /about/ Page

In core/views.py:

def about(request):
    return HttpResponse("""
        <div style='text-align:center;padding:40px;'>
            <h1 style='color:#0066ff;'>My Path to Full Stack</h1>
            <h3>This Django training will help me upgrade to Full Stack Developer.</h3>
            <p>Setting clear goals is the first step to achieving them. Keep pushing forward!</p>
            <p style='font-size:14px;color:gray;'>
                View generated by webmasterandmore's Django server.
            </p>
        </div>
    """)

Then add the URL:

path('about/', views.about, name='about'),

πŸ–₯️ 12. Visit Your Pages

Home page:

http://YOUR_SERVER_IP:1011/

About page:

http://YOUR_SERVER_IP:1011/about/

Both should work now.


πŸš‘ Troubleshooting (Beginner-Friendly)

These are real errors you may encounter (and how to fix them).


❗ 1. Browser shows HTTPS error (ERR_SSL_PROTOCOL_ERROR)

Cause: You used https:// instead of http://.

Fix:
Use:

http://YOUR_SERVER_IP:1011

❗ 2. DisallowedHost at /

Error:

Invalid HTTP_HOST header …

Cause: You didn’t add your server IP to ALLOWED_HOSTS.

Fix:

ALLOWED_HOSTS = ['YOUR_SERVER_IP', 'localhost', '127.0.0.1']

❗ 3. Port already in use

Error:

Error: That port is already in use

Fix: Choose a different port:

python manage.py runserver 0.0.0.0:1012

Check used ports:

sudo ss -tulnp

❗ 4. favicon.ico 400 errors

Ignore these β€” browsers request /favicon.ico automatically.


πŸ“ Quick Exercise for Lesson 2

  1. What is the purpose of creating a virtual environment?
  2. How do you create a new Django project using django-admin?
  3. How do you start the development server on port 1011?
  4. In which file do you register your newly created Django app?
  5. What is the purpose of core/urls.py?

Answers PDF: βœ” Delivered in previous message
(lesson2_answers.pdf)


🏑 Homework for Lesson 2

1. Make sure your home page works on port 1011.

2. Create a custom β€œSuccess” message.

Add your name + your company.

3. Take a screenshot of your working page.

4. Create the /about/ page.

(PDF contains the required answer.)


πŸŽ‰ Conclusion

You have successfully:

This completes Lesson 2 of the Django Full Stack Developer Path.