Initializing a project
-
install conda env for this project
conda create --name mydjangoenv python=3.8
-
Activate conda env and Install Django
pip install django
-
using
django-admin startproject withme .
to create the project in the current directory -
Run server by using
python manage.py runserver
-
Create a pages directory using
python manage.py startapp pages
(in conda env)
add app config definition in withme/setting.py -
Create a new directory named
template
, which has two directories andbase.html
,pages
(other pages) andpartials
(the partials ofbase.html
)
In withme/setting.py, set the path oftemplate
:'DIRS': [os.path.join(BASE_DIR, 'template')]
-
Create a new directory named
static
in withme folder, which includes Static files (CSS, JavaScript, Images)
In withme/setting.py, set the path ofstatic
,STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = 'static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'withme/static') ]
-
run
python manage.py collectstatic
to create astatic
directory outside of withme folder, and add this folder to .gitignore -
In the first time, Add a page using 3 steps:
-
In pages/views.py, write a view
from django.http import HttpResponse def index(request): return HttpResponse("Hello")
or render a page
from django.shortcuts import render def index(request): return render(request, 'pages/index.html')
-
Create a URLconf in pages/urls.py(need to add this file)
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index') ]
-
Point the root URLconf at the pages.urls module. In withme/urls.py
from django.contrib import admin from django.urls import path, include # add include urlpatterns = [ path('', include('pages.urls')), # add a url to urlpatterns path('admin/', admin.site.urls), ]
-
Create a new app (posts)
-
In template, create a folder called
posts
, which includes 3 files, 'post' 'posts' 'search'-
Each file using this code to init
{% extends 'base.html' %} {% block content %} <h1>listings</h1> {% endblock %}
-
-
python manage.py startapp posts
tp create a new appposts
-
add app config in
withme/settings.py
# Application definition INSTALLED_APPS = [ 'pages.apps.PagesConfig', 'posts.apps.PostsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
-
In withme/urls.py, Add a URL to urlpatterns
path('posts/', include('posts.urls')),
-
Create a url.py file in post app, copy the code in pages/urls.py
Database setup
-
install postgreSQL16 and pgadmin4
-
open terminal: type in this
postgres=# \password postgres
Enter new password for user "postgres":
Enter it again:
postgres=# CREATE DATABASE withme OWNER postgres;
CREATE DATABASE -
create a server in pgadmin, and in withme database, set some infos
-
pip install psycopg2
pip install psycopg2-binary
-
change data setting in withme/settings.py
from
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'withme',
'USER': 'postgres',
'PASSWORD': '123456',
'HOST': 'localhost'
}
}
-
pip install Pillow
-
Design database
-
pythpm manage.py makemigrations
-
python manage.py migrate
-
refresh servers in pgadmin, and check if they create table
Design database
- design schemas
- write models in (ex.) listings/models.py
- If you add foreignKey, you need to import other model
Create superuser
-
go to
localhost:8000/admin/
-
python manage.py createsuperuser
to create a super user calledadministrator
and password is123456
Register models with admin
-
register models in listings/admin.py
-
reload django administration page, you can see there is a listings model
Media folder
-
add Media folder setting, just like static folder setting we did before
-
add url to urlpatterns
in localhost/admin, add some realtors and listings, the image that have been uploaded will display inside media folder(auto-create)
Custom the admin page
go to template, create admin/base_site.html, and then custom
if you want to custom the data display, like not just show title(name) instead of other field, goto listings/admin.py
Pull data from listing model
listings/views.py
import models