上篇我们介绍了搭建Django项目需要的基本环境配置,这篇将介绍新建一个简单的Django项目。
源码:ben-ying/django-toturial
Commit:init project 1.0, 2.0 · ben-ying/django-toturial@551c62e
git clone -b templates https://github.com/ben-ying/django-toturial.git
基本操作
1. 新建项目,有些关键字不能使用如django,test等。
django-admin startproject myproject
2. 创建完以后项目结构如下。
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
- myproject:根目录只是您的项目的容器,它的名字与Django无关。
- manage.py:一个命令行实用程序,可让您以各种方式与此Django项目交互。
- myproject/ __ init__.py:一个空文件,告诉Python这个目录应该被认为是一个Python包。
- myproject/settings.py:这个是Django项目的设置, Django设置会告诉你有关设置如何工作的所有信息。
- myproject/urls.py:这个Django项目的URL声明,包含你的Django项目所支持的网站的URL列表。
- myproject/wsgi.py:WSGI兼容的Web服务器为你的项目提供服务的入口点。
3. 进入项目目录以后运行项目。
(venv) ben@ben-VirtualBox:~/app/myproject$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 15, 2018 - 06:54:18
Django version 2.0.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
4. 然后用网页打卡http://127.0.0.1:8000/就会出现如下界面。
![331ba14966dfd292cd2d556f1744962b.png](https://img-blog.csdnimg.cn/img_convert/331ba14966dfd292cd2d556f1744962b.png)
5. 8000是默认端口,如果想用其他端口可以使用其他端口,如8080。
python manage.py runserver 8