Djitter - how to build a twitter clone using Django 2.0 - Part one

Djitter - how to build a twitter clone using Django 2.0 - Part one

Published 28. may 2018 06:44 by Stein Ove Helset

In the first part of this series we will go through how you're setting up a Django project.

Setting up the project

To be sure we are running the correct Python version, the correct Django version and so on we create a separate virtual environment for this project. We can do this by running this command.


$ virtualenv -p djitter_env
Running virtualenv with interpreter /Users/username/.pyenv/versions/3.5.0/bin/python3.5
Using base prefix '/Users/username/.pyenv/versions/3.5.0'
New python executable in djitter_env/bin/python3.5
Also creating executable in djitter_env/bin/python
Installing setuptools, pip, wheel...done.

"virtualenv" is the command we use for creating the environment and "djitter_env" is the name for our environment. If you want to learn more about how you can change the Python version you're running you can read my guide on it here.

Go into the environment:


$ cd djitter_env/

Before we can do anything more we have to activate the environment so the system knows which Python version and which packages. If you want you can use the Python version that comes with your OS, but it's smarter to put this in a separate environment because it makes it easier to maintain and easier to deploy to a webserver.


$ source bin/activate

Just to be sure we are using the correct version you can run this command:


$ python -V
Python 3.5.0

Now that we know that we use the right version of Python we're ready to install Django 2.0. When we're inside a virtualenv every package you install by running the command pip install will only be available from that exact virtualenv. Let's install Django, run this command:


$ pip install django

Collecting django
Collecting pytz (from django)
Installing collected packages: pytz, django
Successfully installed django-2.0.5 pytz-2018.4

Django 2.0.5 is the current stable version as of this date. Don't worry if it's a little bit different from yours. You can install the same version as me by running this command instead:


$ pip install django==2.0.5

There is only a couple more steps now before we're finished settings up the project. We now have to start a Django project and we do this by running:


$ django-admin.py startproject djitter

If there is no output it means that the projects is created and you can go into the projects folder.


$ cd djitter

The "djitter" folder now contains a manage.py and another djitter folder. manage.py is used to run the development server, interacting with the shell and some other commands. Inside the djitter folder is a settings file, a urls file and a wsgi file. I will explain more about this a the project goes on.

The settings file

We need to do some minor changes to the settings files (djitter/settings.py). The settings file is where you set variables like the login_url, where you're static files are located, template dirs, time format and so on.

First you can add the following lines:


LOGIN_URL = '/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

This make sure that the default login page is the front page and not the /admin/ which is default in Django. The STATIC_ROOT variable makes it possible to add static files (css, js and so on) to our project.

You also need to change this line:


ALLOWED_HOSTS = ['127.0.0.1']

Without this you will not be able to view the site. 127.0.0.1 is your local development ip address.

Further down in the file you can change the complete TEMPLATES block to this:


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,''), os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

The only difference here is that we have added two directories to the DIRS. This way we tell Django to look for templates file in the root folder and in the templates folder located in the root folder. If you want to know more about what the different variables do and what other options there are you can read the documentation here: https://docs.djangoproject.com/en/2.0/topics/settings/

So, there it is. The project is created and we are ready to hack away and build our twitter clone. If you want you can check that the project is working by running this command.


$ python manage.py runserver

Go to your browser and open http://127.0.0.1:8000 and you will see something very similar to this.

Djitter - Welcome

If you've got any questions your are more than welcome to leave a comment and I'll answer as soon as possible.

Next part

Share this post

Comments

steinovehelset

04. jun 2018 09:18

Part two is coming very soon! If you sign up to our newsletter you'll get notified :-)

Hank

17. jun 2018 15:59

Nice series! Looking forward to see the last part of this series :)

Add comment