准备:安装、创建项目、APP
来源:互联网 发布:刀卡 淘宝 编辑:程序博客网 时间:2024/06/01 17:35
安装Django
在基于Debian的系统中有两种安装方法,分别为在线安装和下载安装包安装。我们采用安装包安装。
1. 首先在Django官网上下载安装包Django-x.x.x.tar.gz
。
2. 在目标安装目录下解压安装包tar xzvf Django-x.x.x.tar.gz
3. 进入解压后Django-x.x.x
目录,进行安装sudo python setup.py install
安装成功。
创建项目(project)
进入新项目代码所在的目录,为了创建新项目输入以下代码:
django-admin startproject exercise
此时,在该目录下创建了名为“exercise”的项目文件夹。进入该文件夹,可发现该项目结构如下:
tree exercise/exercise/├── exercise│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py└── manage.py1 directory, 5 files
其中:
- The outer exercise/
root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
- manage.py
: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
- The inner exercise/
directory is the actual Python package for your project. Its name is the Python package
name you’ll need to use to import anything inside it (e.g. mysite.urls).
- exercise/__init__.py
: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
- exercise/settings.py
: Settings/configuration for this Django project. Django settings will tell you all
about how settings work.
- exercise/urls.py
: The URL declarations for this Django project; a “table of contents” of your Django powered site. You can read more about URLs in URL dispatcher.
- exercise/wsgi.py
: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
创建app
创建完项目工作环境后,需要我们为web应用创建app。
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
在该myweb
项目下创建app须在myweb
目录下输入以下命令:
python manage.py startapp mysite
完成后可以发现在exercise
目录下多出一个名为mysite
的文件夹,其结构如下:
tree mysite/mysite/├── admin.py├── apps.py├── __init__.py├── migrations│ └── __init__.py├── models.py├── tests.py└── views.py
在创建完app后需要将该app添加至exercise
项目中settings.py
文件的INSTALL_APPS
中:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'mysite.apps.MysiteConfig',]
将app添加之项目后,需要为整个app创建相应的URL,该URL在exercise/urls.py
中创建。为此,在该文件中添加下列命令:
from django.conf.urls import url, includefrom django.contrib import adminurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^mysite/', include('mysite.urls')),]
除此以外,我们还需要设置Django使用的数据库,该设置同样在settings.py文件中实现。Django默认使用的数据库为SQLite3,因此,当我们没有设置settings.py中数据库相关项时,Django使用SQLite3存储数据。本项目中采用MySQL数据,其在settings.py设置如下:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangodb', 'USER': 'root', 'PASSWORD': 'XXXXXXXXX', 'HOST': '', 'PORT': '', }}
其中在’ENGINE’中指出我们采用MySQL数据库,’USER’和’PASSWORD’项分别指出我们在MySQL中使用的用户名和相应密码,’NAME’指出我们将采用MySQL数据库中名为’djangodb’的数据库。
至此,整个app的设置大体完成。
- 准备:安装、创建项目、APP
- 用eclipse创建javaweb项目的准备
- OpenGL_1:创建项目前的准备
- Django-安装,项目,app
- 指尖菜谱App从0到1-项目开发准备
- iOS 安装cordova,创建App
- vs2017 创建安装项目
- 项目准备
- 项目准备
- win 32 APP 项目简单创建窗体
- 创建第一个ionic APP项目
- 使用ionic+cordova创建APP项目
- Android studio教程:[1]创建app项目
- 使用create-react-app创建项目
- Create-React-App创建React项目
- 实验准备 安装GLUT包与创建工程
- 大数据准备-虚拟机安装之创建虚拟机(1)
- 准备ipa发布app
- npm模块之http-proxy-middleware使用教程(译)
- HDU 6105 Gameia
- 网易2017秋招编程题
- c++中++和*的优先级、cout运算从右到左
- hibernate 映射关系小模板
- 准备:安装、创建项目、APP
- 一次面试引发的问题:激活函数Relu,sigmoid ,tanh的角逐
- 简单内存池设计与实现
- 润乾V5批量直接打印 报错误400 且参数不生效
- java 常用的正则表达式
- oracle触发器实例及异常处理
- c++常见面试题(1)
- Android Studio快速定位当前文件位置
- BZOJ2434【NOI2011】阿狸的打字机 <AC自动机+Fail树+树状数组>