官网详细参考https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial01/
linux下
查看pip装了哪些包
yum install mariadb-devel mysql-devel #用mariadb的就装mariadb-devel,用Mysql的就装mysql-devel
python3 -m pip install django==2.1.2 #指定版本安装,不指定默认安装最新版本
#指定文件安装
cat requirements.txt
pymysql
django
mysqlclien
pip install -r requirements.txt
python3 -m pip freeze #查看装了哪些文件,python3 -m 指用python3安装的模块去执行命令,如python3 -m django startproject test
ln -s /usr/local/python3.2.7/bin/django-admin /usr/local/bin/django-admin
import django
django.get_version()
django-admin startproject mysite
django-admin startapp polls
python虚拟环境安装
法1:python3 -m venv venv3.6
法2:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108300322c61f256c74803b43bfd65c6f8d0d0000
pycharm下
一、项目初创测试
1、新建项目
2、查看django版本
3、测试运行django
法1:IDE右手边
法2:IDE头顶
法3:cmd下执行(进到mysite目录)
python manage.py runserver #默认端口访问
python manage.py runserver 8080 #更改端口访问
python manage.py runserver 0:8000 #0 是 0.0.0.0 的简写
二、创建投票应用
1、创建应用(cmd下执行,进到mysite目录)
python manage.py startapp polls
2、编写视图
1) 打开 polls/views.py,把下面这些 Python 代码输入进去:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world, Your're at the polls index.")
2) polls新建urls.py文件,在 polls/urls.py 中,输入如下代码:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
3) mysite/urls.py 文件的 urlpatterns 列表里插入一个 include(), 如下:
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
4) cmd运行python manage.py runserver
5) 访问http://localhost:8000/polls
pycharm汉化
pycharm汉化包.zip解压,把resources_cn放到C:\Program Files\JetBrains\PyCharm 2018.3.3\lib下
重启Pycharm
wsgi:web服务与python程序之间的桥梁,web服务的入口
注意
如果在linux下运行项目报错
报错1
django No installed app with label 'admin'
法1:注释项目url.py的admin
法2:https://www.88cto.com/article/MsIvjMx6
报错2:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决方法:https://blog.csdn.net/weixin_33127753/article/details/89100552