DEV Community

Mageshwaran
Mageshwaran

Posted on

Django For Beginners - Project Setup P1

Django is a python web framework used to build web app and api services. Its comes under Model Template Views architectural pattern. Django is battery included tool, shipped with many built in feature like authentication, admin panel, session handling, ORM, etc.

Virtualenv

Advantage of using virtualenv is machine can have multiple django projects which are running in different version of django.

create Virtual environment using virtualenv

virtualenv --python=python3 venv
Enter fullscreen mode Exit fullscreen mode

to know more about Virtual environment

Activate Virtual environment

This command will work on bith Mac and Linux

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Use this to activate on Windows

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

To deactivate Virtual environment, this command common for all the platforms

deactivate
Enter fullscreen mode Exit fullscreen mode

Install Django

After activating the virtual environment install the latest django version in venv

pip install django
Enter fullscreen mode Exit fullscreen mode

To check django is installed, list the pip packages, this command will list all the installed packages in venv

pip freeze
Enter fullscreen mode Exit fullscreen mode

or you can check by entering this command in terminal. if django installed, you should see the version of your installation.

python -m django --version
Enter fullscreen mode Exit fullscreen mode

Creating a project

Commad will create a project called mysite, its just a directory. mysite will act as the root directory for the project.

django-admin startproject mysite
Enter fullscreen mode Exit fullscreen mode
mysite/ ---> root directory
    manage.py
    mysite/ ---> django package directory
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
Enter fullscreen mode Exit fullscreen mode

Alternative command, both do the same process with a small difference. Dot . will create the project at the at location from where the command is being executed. Then the directory will act as root directory

django-admin startproject mysite .
Enter fullscreen mode Exit fullscreen mode

Now lets check the project, go to the root directory and run this command to start the django project

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Above one will run the django in http://127.0.0.1:8000/ Ip and port, we can change the running ip and port.

python manage.py runserver 192.168.43.135:8080
Enter fullscreen mode Exit fullscreen mode

This will run the project in http://192.168.43.135:8080

runserver 192.168.43.135:8080
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 26, 2020 - 19:25:29
Django version 3.1.2, using settings 'mysite.settings'
Starting development server at http://192.168.43.135:8080/
Quit the server with CTRL-BREAK.
Enter fullscreen mode Exit fullscreen mode

Open the link in browser with http://192.168.43.135:8080/, you will get an error this is because you need to add the ip in allowed host in settings.py.
Alt Text

For now use the default 127.0.0.1, run the project

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 26, 2020 - 19:38:32
Django version 3.1.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

???

You have 18 unapplied migration(s).

Django shipped with build in feature like admin page, session, auth for that it has to apply the migration table to the DB.

By default django shipped with SQlite DB.

Lets apply the migrate to the database

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

You can see default table structure are applied to SQlite database

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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
Enter fullscreen mode Exit fullscreen mode

Thanks you, have a great day ahead.😎

Top comments (0)