程序设计应用 2023-03-11

 

Django does support the Model-View-Controller (MVC) architectural pattern. However, Django uses a slightly different approach called Model-View-Template (MVT) that separates the logic into the model (data), view (presentation), and template (user interface). The model defines the data schema, the view handles the data processing logic, and the template displays the final output to the user.

这里的view对应的controller

这里的template对应的view

 

 

django-admin startproject myDjango

 

defualt fodler structure of Django

The default folder structure of a Django project looks like this:

- manage.py: A command-line helper tool that allows you to interact with your Django project

- myproject/: The root project directory that contains other subdirectories

- myproject/__init__.py: An empty file that tells Python to treat the myproject directory as a Python package

- myproject/settings.py: A Python module that contains all the settings for your project, such as database configuration, installed apps, middleware, and security settings

- myproject/urls.py: A Python module that maps URL patterns to views in your project

- myproject/asgi.py: An ASGI configuration file used for running the project on an ASGI server

- myproject/wsgi.py: A WSGI configuration file used for running the project on a WSGI server

- myapp/: A subdirectory that contains one or more Django apps

- myapp/__init__.py: An empty file that tells Python to treat the myapp directory as a Python package

- myapp/admin.py: A Python module that defines the configuration of the Django Admin site for this app

- myapp/apps.py: A Python module that defines some of the configuration for this app

- myapp/models.py: A Python module that defines the database schema for this app

- myapp/tests.py: A Python module that contains tests for this app

- myapp/views.py: A Python module that defines the views for this app

- myapp/templates/: A directory that contains templates used by this app

- myapp/static/: A directory that contains static files used by this app, such as CSS, JavaScript, and images.

 

运行网站

.\manage.py runserver 8080

 

新建一个应用

 .\manage.py startapp first

 

Django settings file - step by step Explanation - GeeksforGeeks

Once we create the Django project, it comes with predefined Directory structure having the following files with each file having their own uses. 

Lets take an example 
 

// Create a Django Project "mysite" 
django-admin startproject mysite

cd /pathTo/mysite
// Create a Django app "polls" inside project "mysite"
python manage.py startapp polls

After these commands on Terminal/CMD Directory structure of project “mysite” will be: 
 

mysite           <-- BASE_DIR      
    --> mysite                 
            -> __init__.py
            -> asgi.py
            -> settings.py    <-- settings.py file 
            -> urls.py
            -> wsgi.py
    --> manage.py
    --> polls

Insights about settings.py file

A Django settings file contains all the configuration of your Django Project. In this article the important points of settings.py file of Django will be discussed. 
A settings file is just a Python module with module-level variables. 
 

BASE_DIR

BASE_DIR points to top hierarchy of project i.e. mysite, whatever paths we define in project are all relative to BASE_DIR. To use BASE_DIR we will have to use os module provided by python. 
 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

 

DEBUG

In Development Error is very obvious to occur. There is no fun in writing a program where we do not face any error. but sometimes tackling errors is very hectic. Django provides an inbuilt Debugger which makes the developer’s life very easy. We can use it by doing: 
 

DEBUG = True  // It is Default value and is preferred in only Development Phase.

In production, DEBUG = False is preferred. 

 

ALLOWED_HOSTS

ALLOWED_HOSTS is list having addresses of all domains which can run your Django Project. 
 

When DEBUG set to True 
ALLOWED_HOSTS can be an empty list i.e. ALLOWED_HOSTS=[ ] because by Default it is 127.0.0.1 or localhost 
When DEBUG set to False 
ALLOWED_HOSTS can not be an empty list. We have to give hosts name in list. i.e. ALLOWED_HOSTS=[“127.0.0.1”, “*.heroku.com”]. “127.0.0.1” represents Your PC, “*.heroku.com” represents this application can be run on heroku also. 

 

INSTALLED_APPS

In this section, we mention all apps that will be used in our Django project. Previously we made an app polls we have to tell Django its existence To do so have to put into INSTALLED_APPS: 
 

    INSTALLED_APPS = [
        // Some preloaded apps by Django,
        'polls', // don't forget to quote it and also commas after every app
]
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
 
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
 
ROOT_URLCONF = 'myDjango.urls'
 
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',   //模板引擎
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-03-11 CSS opacity 属性
2021-03-11 nvarchar(max) still being truncated
2020-03-11 The view 'Index' or its master was not found.
2019-03-11 JMeter -- Getting Started
2019-03-11 237. Delete Node in a Linked List
2016-03-11 SuperSocket中的Server的初始化和启动
2016-03-11 SuperSocket中的Server是如何初Start的
点击右上角即可分享
微信分享提示