In this tutorial we will create our first application.
Let’s look at what startproject created:
├── manage.py
├── phonebook
│ ├── asgi.py
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── Pipfile
└── Pipfile.lock
These files are:
- The outer phonebook/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
- manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
- The inner phonebook/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. phonebook.urls).
- phonebook/init.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
- phonebook/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
- phonebook/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
- phonebook/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
Contact application
In order to create our first application we need to run :
(my-env) $ python manage.py startapp contact
Our application looks like this
├── contact
│ ├── admin.py
│ ├── apps.py
│ ├── init.py
│ ├── migrations
│ │ └── init.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── phonebook
│ ├── asgi.py
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── Pipfile
└── Pipfile.lock
we will explain later the details of files.
Don't forget to add contact to your settings :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'contact' # add this
]
Now inside contact folder let's create our database schema, every contact we need to save :
-First Name
-Last Name
-Email
-Phone Number
-The use who created it
-And the timestamp
Open models.py and write :
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Contact(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
phone = models.CharField(max_length=150)
email = models.CharField(max_length=150, blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.phone
Here we import the User table from django.contrib and create a Python class, this represent our database schema.
By convention we use underscore (_) to separate words.
Before going any further we need to migrate our database.
In your terminal stop the server and run :
(my-env) $ python manage.py makemigrations
Migrations for 'contact':
contact/migrations/0001_initial.py
- Create model Contact
(my-env) $ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contact, 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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying contact.0001_initial... OK
Applying sessions.0001_initial... OK
Let's create a superuser:
(my-env) $ python manage.py createsuperuser
Username (leave blank to use 'hackerpro'): admin
Email address: admin@gmail.com
Password:
Password (again):
Superuser created successfully.
Open your browser and go to http://localhost:8000/admin
What you see ?
Now we need to add our contact application to the admin.
Open admin.py file and write :
from django.contrib import admin
from .models import Contact
# Register your models here.
admin.site.register(Contact)
See you in the next tutorial
Top comments (0)