您的位置:首页 > 移动开发

准备:安装、创建项目、APP

2017-08-11 11:22 197 查看

安装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.py

1 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, include
from django.contrib import admin

urlpatterns = [
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的设置大体完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django