Creating a New Django Website

Although setting up a new Django/Python website requires a few steps, it is easy to get up and running!

The first steps are below. After that, you might want to see about putting your project on github, or gitlab, and setting up Apache on Ubuntu to serve your site.

  1. Create initial folder.
  2. Create virtual environment.
  3. Install Django.
  4. Create project.
  5. Couple little modifications to skeleton files.
  6. Run manage.py commands.
  7. Initialize Git
  8. That’s It!

Create Initial Folder

First, create the root folder for your project. Something like ~/projects/demo1

$ mkdir demo1

Create Virtual Environment

$ cd demo1
$ python -m venv venv
$ . venv/bin/activate

Depending on your platform, this may not work. The bin may be placed in a scripts folder, so …

$ . venv/scripts/activate

Install Django

$ pip install django

Create Project

$ django-admin startproject demo1

Couple little modifications to skeleton files

To Template>Options in settings.py


1
2
3
'builtins': [
    'django.contrib.staticfiles.templatetags.staticfiles',
],

At the bottom of settings.py


1
2
3
4
5
STATIC_URL = '/static/'
STATIC_ROOT = 'static/' # os.path.join(BASE_DIR, STATIC_URL)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

Run manage.py commands

$ python manage.py makemigrations
$ python manage.py collectstatic
$ python manage.py migrate
$ python manage.py createsuperuser

Initialize Git

Before initializing git, we first need to add a .gitignore file, a .gitattributes file, and a README.md file.

$ git init
$ git add .
$ git commit -m "initial commit"

Finished!

That’s it, you’re done. You are now ready to look at your site at http://127.0.0.1:8000 by running:

$ python manage.py runserver 127.0.0.1:8000

Now you can work on your project. Don’t forget to commit your changes. You should setup a repo on github, or gitlab to host your project.

You may also want to look into hosting your site. Setting up Apache on Ubuntu is a recommended way to serve your site.

You May Also Like

Leave a Reply

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