Django introduction

In this section, we are going to review how to start working with the Django framework. To install django, just execute the pip install django command.

Once installed, we can use the django-admin.py script to create the file structure that's necessary to create applications with the framework.

These are the self-generated files that appear when you run the $ django-admin.py startproject djangoApplication command:

  • __init__.py tells Python that this folder is a Python package and allows Python to import all of the scripts into the folder as modules.
  • manage.py is a utility script that executes from the command line. It contains some functions to manage your website.
  • settings.py contains the settings of your website. This file is simply a number of variables that define the configuration of your site.
  • urls.py is the file that assigns the URLs to the pages.

We can see that in the settings.py generated file, there is a default configuration for sqlite3 database:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

To create a database in our application, we can run the following command in the djangoApplication directory that contains the manage.py file:

$ python manage.py migrate

If the execution is correct, you should see something like this:

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

In this way, we can start the web server by running the $ python manage.py runserver command, and we will have the application running on http://localhost:8000.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset