程序设计应用 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
]