django

Django(win10)

官方文档

文档2.2

安装

使用virtualenv (pip安装,配置path)

在项目文件使用$ virtualenv eb-virt

$ eb-virtScriptsactivate

进入虚拟环境

pip install django==2.1.1

查看安装

pip freeze

输出结果

1
2
Django==2.1.1
pytz==2018.9

完事

概述

跳过,看不太懂

helloworld

win10下使用dir查看文件列表

django-admin startproject mysite

创建新的web

得到文件目录

1
2
3
4
5
6
7
8
/myweb
|-- mysite
| |--mysite
| |--__init__.py
| |--settings.py
| |--urls.py
| |--wsgi.py
| |-- manage.py

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable

settings.py是一个Django的配置文件

url.py是一个Django的网站目录

wsgi.py是一个Django的web服务器的入口

在mysite目录下$ python manage.py runserver

然后就完成了本地的第一个Django

应用与项目

“项目和应用有啥区别?应用是一个专门做某件事的网络应用程序——比如博客系统,或者公共记录的数据库,或者简单的投票程序。项目则是一个网站使用的配置和应用的集合。项目可以包含很多个应用。应用可以被很多个项目使用。”

虽然没有看懂,继续

$ python manage.py startapp polls

创建了一个应用pulls

1
2
3
大专栏  django class="line">4
5
6
7
8
9
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

在polls/views.py中paste

1
2
3
4
5
from django.http import HttpResponse


def (request):
return HttpResponse("Hello, world. You're at the polls index.")

新建urls.py

1
2
3
4
5
6
7
from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
]

在 mysite/urls.py 文件的 urlpatterns 列表里插入一个 include(), 如下:

1
2
3
4
5
6
7
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]

然后跑起来,好像失败了,然后cmd重开了一下就找得到目录了

显示一行:

Hello, world. You’re at the polls index.

helloworld结束

原文地址:https://www.cnblogs.com/lijianming180/p/12389476.html