python的django框架架构_用Python的Django框架搭建前后台[官网教程]

人生苦短,我用Python–不管你之前写过多少其他语言,当你有一天回来写python的时候,你就会觉得“世界是那么的美好!”

今天我就来讲讲我是如何用Python的django[https://docs.djangoproject.com] 来搭建后台的:

我是在mac的环境下搭建的,linux也试过,其实搭建环境来说,都差不多,这里就以mac为例子

首先mac或者linux的话,应该是自带python的,如果是win系统的话,你可以自己去下载[https://www.python.org/]

数据库(database)也是很重要的,因为我们想做的是一个健全的前后台系统、后期还需要给移动端提供数据来源,所以我这里选用mysql,我这里偷懒就用了MAMP一键安装,在linux下也有相应的叫LAMP,一步步点击下一步之后就完成了,这里我用的是Apache+Mysql。安装完成之后,你就可以看到mysql的端口、密码、用户名啥的(这里可能有些坑,需要自己去踩,这里就不说了,自行google),这里要弄好,因为我们后续在配置django的时候需要配置。

下面进入正式安装jdango,官方建议通过pip去安装。

1pip install -U pip

如果报错就执行下面的命令:1pip install

不出意外的话,此时你的pip,应该已经安装好了。下面来安装环境,这里环境,官方的意见是安装virtualenv或者virtualenvwrapper,这里其实我都试过,你也可以试试,这里就用virtualenv [https://virtualenv.pypa.io/en/stable/] , 你也可以自己参考一下virtualenv的官方介绍 [https://virtualenv.pypa.io/en/stable/installation/] :1[sudo] pip install virtualenv

最后执行安装django的命令:1pip install Django

官方给出了严重你是否安装成功,以及安装的版本,当成功打印的时候,就代表已经成功安装了。1

2

3> import django

> print(django.get_version())

1.11

也可以直接在终端输入1python -m django --version

直接开始项目:直接在终端输入:此时在当前路径下就会看到我们创建的GhCoder_Site文件夹:

1django-admin startproject GhCoder_Site

文件夹的结构:

1

2

3

4

5

6├── GhCoder_Site

│   ├── __init__.py

│   ├── settings.py

│   ├── urls.py

│   └── wsgi.py

└── manage.py

这里官方给了一些解释,我觉得很好,有助于我们的理解, 其实我之前都是写php的,深有感触–note:If your background is in plain old PHP (with no use of modern frameworks), you’re probably used to putting code under the Web server’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.

Put your code in some directory outside of the document root, such as /home/mycode.

官方对于每个文件夹以及文件的作用都做了一定的解释,这里我就不解释了很通俗易懂:The outer GhCoder_Site/ 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 GhCoder_Site/ 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. GhCoder_Site.urls).

GhCoder_Site/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.

GhCoder_Site/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

GhCoder_Site/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.

GhCoder_Site/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

下面直接运行下面的命令:1python manage.py runserver

你会得到这样的反馈:1

2

3

4

5

6

7

8

9

10

11

12Performing system checks...

System check identified no issues (0 silenced).

//请忽略这里

You have 13 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 09, 2017 - 07:08:16

Django version 1.11, using settings 'GhCoder_Site.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CONTROL-C.

此时打开浏览器,输入http://127.0.0.1:8000/,你就会看到Welcome to Django” page, in pleasant, light-blue pastel. It worked!,就代表成功了。

切换端口:1python manage.py runserver 8080

切换ip、监听所有的ip1python manage.py runserver 0:8000

Django的开发模式都是依赖于一个个app,这一点我非常喜欢,后期就算项目很大,也不会很乱,创建第一个app:1python manage.py startapp polls

结构如下:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21.

├── db.sqlite3

├── GhCoder_Site

│   ├── __init__.py

│   ├── __init__.pyc

│   ├── settings.py

│   ├── settings.pyc

│   ├── urls.py

│   ├── urls.pyc

│   ├── wsgi.py

│   └── wsgi.pyc

├── manage.py

└── polls

├── admin.py

├── apps.py

├── __init__.py

├── migrations

│   └── __init__.py

├── models.py

├── tests.py

└── views.py

创建第一个views,打开polls/views.py,输入:1

2def (request):

return HttpResponse("Hello, world, You are in polls app")

现在配置一下路由,到文件polls,创建一个名为urls.py的文件。将之前写的view和这个url对应上:1

2

3

4

5

6from django.conf.urls import url

from . import views

urlpatterns = [

url(r'^$', views.index, name='index')

]

最后将app的url,写到项目中的urls.py的文件里面去,到GhCoder_Site/urls.py文件里面,添加如下代码:1

2

3

4

5

6

7from django.conf.urls import include, url

from django.contrib import admin

urlpatterns = [

url(r'^polls/', include('polls.urls')),

url(r'^admin/', admin.site.urls),

]

这里关于include的官方介绍:The include() function allows referencing other URLconfs. Note that the regular expressions for the include() function doesn’t have a $ (end-of-string match character) but rather a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.

此时你访问http://127.0.0.1:8000/polls/就看到你刚才写的view以及url

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值