Please enable JavaScript.
Coggle requires JavaScript to display documents.
Django - new project, Models, Admin Console - we can usually use to…
Django - new project
django-admin startproject projectname
manage.py
python3 manage.py startapp appname
App
urls.py
from django.urls import path
from . import views
urlpatterns = [ path("", views. , name = "") ]
urls.py
from django.urls import path, include
urlpatterns = [ path("", include("appname.urls")) ]
settings.py
INSTALLED_APPS = [ 'appname', ]
STATICFILES_DIRS = [BASE_DIR/"static"]
Models
Migration
after each model filed changing (schema changing) - we have to create migrations and migrate
open terminal for the project
python3 manage.py makemigrations
python3 manage.py migrate
Models creation
Data models planning
Describe data model in models.py in App path as a class to interact with db to store data
from django.db import models
a model class represents a single db table
"Field" is abstract class that represents a db table column
Abstract class - the class to be inherited by other classes
Admin Console - we can usually use to operate data
python3 manage.py createsuperuser
admin.py
from django.contrib import admin
from .models import
admin.site.register(Model_name)
to customize admin page for specific data model - we need to use ModelAdmin class (the model representation in admin interface)
use "prepopulated_fields" variable to pre populate fields: prepopulated_fields = {"slug":("title",)} - the field you base must be a tuple
Forms