Django tutorial for beginners [step by step] part 3

django
Published: 3 weeks, 4 days ago   144


Fermesk Rashidi

Tutorial goal: Access shell, virtual environment, Create a new app, install it

In this tutorial we take a look at how to access the shell, building a Django app, what is the virtual environment. The techniques you’ll learn in the tutorial are fundamental to work with shell and building a Django app, and mastering it will give you a deep understanding of Django. 

What is the shell and why I need it?

A shell is a tool that provides an interface between a user and an operating system. By that, the user is able to execute programs and tools. In the graphical environment (mac, windows) we interact with the operating system(OS) with mouse and keyboard but the shell is even a more powerful tool without a graphical interface when you're interacting with a remote server. You just type commands and they will be executed in the server. Shell gives you full control over the operating system

People often think that using the command line is complicated and difficult at first; But if you learn the basics properly, your skills will grow very quickly and you will be able to work with the shell easily. :)

Acess shell

Access shell in doprax is the easiest. You don't need any special software. To open the shell in doprax, Go to project detail page and simply click on the open development shell button:

open development shell

open development shell

 development shell 

development shell 

You will get a blinking command prompt and you can start Unix/Linux commands. Note that the # sign at the bottom of the command line when you are using the system as a root user. The root user is basically a superuser who has the highest level of access and can take full advantage of all the server management tools and facilities and execute any commands. Keep in mind that uppercase and lowercase letters are important almost everywhere in Linux; So when inserting filenames, directories, commands, arguments, and… in the shell, you should always be careful because in many cases, a small mistake like this causes various problems.

Activate virtual environment 

A virtual environment, as its name implies, provides the possibility of creating a virtual and isolated environment for test and production of different projects. For example, we have two projects A and B, both of them are Django. We made A project a few years ago and started B recently. A is written with Django version 1.5 and B is made with version. We use Virtual environments so that the two projects do not overlap. This means that any changes you make to the one project environment will not affect the other projects you are developing. For example, if you install and use a specific python package and libraries in one environment doesn't affect other environments.

In the local development environment, you need to install virtual env and necessary packages but in doprax all of the packages for creating a virtual environment have been installed. you just need to activate it. To activate the virtual environment type this command in the development shell and press enter:

source /venv/bin/activate

And if the virtual environment is activated there would be a (venv) at the start of command which is the name of our virtual environment:

(venv) root@b3ccdfa5c25d:/app#

Now that our environment has been activated, we have access to install python packages. When you create and run your Django project on doprax, several python packages will be installed. These packages will be available to your Django project and can be used.  you can install any additional that you might need which will be discussed later in a future tutorial. For now, let see which packages have already installed in our virtual environment. Type this command:

(venv) root@b3ccdfa5c25d:/app# pip freeze 
Django==2.2.5
gunicorn==19.4.5
mysqlclient==2.0.1
pkg-resources==0.0.0
pytz==2020.1
sqlparse==0.3.1

Creating your first new Django app

‌Before starting this section, let me give an explanation about the Django app and the difference between the app and the project. Consider the Django website like a company with different departments. Each app is like a department in the company that has its own address(URLs.py), functionality(views.py)..., etc. Together all of these departments work for a common goal. In order to create a company, we need to create different departments with a specific purpose so that company will achieve its goals. A Django project(website) is like a company, we need to create different apps for different purposes. Likewise, a Django project contains multiple apps that each one of them like a separate department in the company designed for a specific job.

This Django feature has various advantages. First of all, this makes it easy for you to organize your code. Second, your code will be reusable and you can use a different app for a different website. If you make a payment app in a project, it could be reusable for another website.

Django by default has some pre-installed app that is provided for developers' convenience. If you open settings.py in project directory you can see this list of preinstalled apps.


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
  
]

Creating an app is just as easy as creating a new project in Django. First of all, you need to specify the app title. which comes at the end of building command. The name of the app doesn't matter but it's better to be something meaningful. In this case it's music. Open the shell type this command 

cd /app/source
python manage.py startapp music 

Python manage.py is a file in the root of your Django project. it's a tool for executing many Django-specific tasks like creating a new app within a project, running the development server, running your tests. After executing the command above, a file with the name of your app (which is music here) is created inside the project file.

Django app in directory 

music app in directory 

After creating your app, it's time to enable the app in the INSTALLED_APPS list. Open the settings.py in the myapp folder and in installed_apps add music by typing this code:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'music',
]

Thank you for reading, I'll see you in the next tutorial. :)

Django tutorial for beginners [step by step] part 2
Django tutorial for beginners [step by step] part 4
doprax

Simple Host for Django

  • Simple interface
  • Free SSL
  • Custom Domain
  • Import from github
  • and more
Get started now

Related