How start Django as Web-Site

Its simple tutorial to quick start a demo site in Django. You can quickly follow the steps to check its working flow.

Here is my python version is 3.10.12. You can check you installed version on you system by following command:

python --version

Lets start from scratch to install Django first.

  1. Install Django:
pip install Django
  1. Create a new Django project:
django-admin startproject mysite
  1. Navigate to the project directory:
cd mysite
  1. Create a new Django app:
python manage.py startapp myapp
  1. Define a simple view in myapp/views.py:
from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, Django!")
  1. Configure the URL routing by editing mysite/urls.py:
from django.contrib import admin
from django.urls import path, include
from myapp.views import hello

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', hello, name='hello'),
]
  1. Run the development server:
python manage.py runserver
  1. Visit http://127.0.0.1:8000/hello/ in your browser, and you should see the “Hello, Django!” message.

May you get notice while running on the port:

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.

To address the migration warning, you should run the following command to apply the migrations:

python manage.py migrate

This command will apply any pending database migrations.

Django Database

In a Django project, the default database is specified in the settings.py file, which is located in your project’s main directory (mysite in your case). Look for the DATABASES setting in that file.

Here’s an example of how it might look by default:

# mysite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / "db.sqlite3",
    }
}

In this example, the default database is configured to use SQLite, and the database file (db.sqlite3) is located in the project’s base directory (BASE_DIR).

If you run the python manage.py migrate command, it will create necessary database tables and apply migrations. The SQLite database file (db.sqlite3 in this case) will be created in the same directory as your manage.py file.

If you want to use a different database (e.g., PostgreSQL, MySQL), you would adjust the DATABASES setting accordingly. The database configuration would include details such as database name, user, password, host, and port.

For example, using PostgreSQL:

# mysite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Remember to install the appropriate database driver (psycopg2 for PostgreSQL, mysqlclient for MySQL) via pip if you’re using a different database.

This is a basic setup, and you can expand and customize your project as needed. Make sure to check the official Django documentation for more details: https://docs.djangoproject.com/

Leave a comment

Your email address will not be published. Required fields are marked *