Initializing a django project

Initializing a project

  1. install conda env for this project conda create --name mydjangoenv python=3.8

  2. Activate conda env and Install Django pip install django

  3. using django-admin startproject withme . to create the project in the current directory

  4. Run server by using python manage.py runserver

  5. Create a pages directory using python manage.py startapp pages (in conda env)
    add app config definition in withme/setting.py

  6. Create a new directory named template, which has two directories and base.html, pages(other pages) and partials(the partials of base.html)
    In withme/setting.py, set the path of template: 'DIRS': [os.path.join(BASE_DIR, 'template')]

  7. Create a new directory named static in withme folder, which includes Static files (CSS, JavaScript, Images)
    In withme/setting.py, set the path of static,

    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATIC_URL = 'static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'withme/static')
    ]
    
  8. run python manage.py collectstatic to create a static directory outside of withme folder, and add this folder to .gitignore

  9. In the first time, Add a page using 3 steps:

    1. 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')
      
    2. 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')
      ]
      
    3. 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)

  1. 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 %}
      
  2. python manage.py startapp posts tp create a new app posts

  3. 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',
    ]
    
  4. In withme/urls.py, Add a URL to urlpatterns path('posts/', include('posts.urls')),

  5. Create a url.py file in post app, copy the code in pages/urls.py

Database setup

  1. install postgreSQL16 and pgadmin4

  2. 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

  3. create a server in pgadmin, and in withme database, set some infos

  4. pip install psycopg2
    pip install psycopg2-binary

  5. 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'
    }
}
  1. pip install Pillow

  2. Design database

  3. pythpm manage.py makemigrations

  4. python manage.py migrate

  5. refresh servers in pgadmin, and check if they create table

Design database

  1. design schemas
  2. write models in (ex.) listings/models.py
  3. If you add foreignKey, you need to import other model

Create superuser

  1. go to localhost:8000/admin/

  2. python manage.py createsuperuser to create a super user called administrator and password is 123456

Register models with admin

  1. register models in listings/admin.py

  2. reload django administration page, you can see there is a listings model

Media folder

  1. add Media folder setting, just like static folder setting we did before

  2. 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

相关推荐
程序员 小濠17 分钟前
接口测试基础 --- 什么是接口测试及其测试流程?
自动化测试·python·测试工具·职场和发展·appium·接口测试·压力测试
程序媛徐师姐25 分钟前
Python基于Django的酒店推荐系统【附源码】
python·django·酒店·酒店推荐·python django·酒店推荐系统·python酒店推荐系统
大脑经常闹风暴@小猿43 分钟前
1.1 go环境搭建及基本使用
开发语言·后端·golang
尚学教辅学习资料1 小时前
基于SpringBoot的美食分享平台+LW示例参考
spring boot·后端·美食
~kiss~1 小时前
python的thrift2pyi学习
windows·python·学习
奔跑吧邓邓子1 小时前
【Python爬虫(45)】Python爬虫新境界:分布式与大数据框架的融合之旅
开发语言·分布式·爬虫·python·大数据框架
Luke Ewin1 小时前
根据音频中的不同讲述人声音进行分离音频 | 基于ai的说话人声音分离项目
人工智能·python·音视频·语音识别·声纹识别·asr·3d-speaker
大米洗澡1 小时前
数字签名技术基础
python·学习·程序人生·面试·职场和发展
神一样的老师2 小时前
ChromeDriver版本不匹配问题的解决
python
起个破名想半天了2 小时前
Web自动化中Selenium下Chrome与Edge的Webdriver常用Options参数
python·selenium·自动化