django getting started

  1. Install Django: bash pip install django

  2. Create a Django project: bash django-admin startproject myproject

  3. Navigate to the project directory: bash cd myproject

  4. Create a Django app: bash python manage.py startapp myapp

  5. Define models in models.py of the created app (myapp): ```python # myapp/models.py from django.db import models

class MyModel(models.Model): field1 = models.CharField(max_length=100) field2 = models.IntegerField() ```

  1. Make migrations: bash python manage.py makemigrations

  2. Apply migrations: bash python manage.py migrate

  3. Create a Django admin superuser: bash python manage.py createsuperuser

  4. Start the development server: bash python manage.py runserver

  5. Access the admin site in a web browser (http://127.0.0.1:8000/admin/) and log in using the superuser credentials.

  6. Create instances of MyModel in the admin interface.

  7. Create views in views.py of the app (myapp): ```python # myapp/views.py from django.shortcuts import render from .models import MyModel

    def index(request): my_model_instances = MyModel.objects.all() return render(request, 'myapp/index.html', {'my_model_instances': my_model_instances}) ```

  8. Create templates in the templates directory of the app (myapp):

    • myapp/templates/myapp/index.html: html <h1>My Model Instances</h1> <ul> {% for instance in my_model_instances %} <li>{{ instance.field1 }} - {{ instance.field2 }}</li> {% endfor %} </ul>
  9. Configure URLs in urls.py of the app (myapp): ```python # myapp/urls.py from django.urls import path from . import views

    urlpatterns = [ path('', views.index, name='index'), ] ```

  10. Include the app URLs in the project's urls.py: ```python # myproject/urls.py from django.contrib import admin from django.urls import include, path

    urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ] ```

  11. Run the development server again: bash python manage.py runserver

  12. Access the app in a web browser (http://127.0.0.1:8000/myapp/).