django创建第一个python web项目
2018-11-28 本文已影响0人
风一样的存在
1.首先在已经安装pip命令行的情况下,安装django
pip install django
2.创建第一个应用
django-admin startproject djangoApp
生成的项目目录如下:

3.进入djangoApp创建DjangoApp应用
python manage.py startapp DjangoApp
4.在settings.py文件中修改配置如下:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'DjangoApp',
]
即添加应用到文件里。
5.设置urls路径,进入到urls.py文件:
from django.contrib import admin
from django.urls import path
from DjangoApp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]
6.在应用的views.py文件中创建index函数
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
7.在DjangoApp文件夹中创建:templates文件夹(不要改文件夹的名字),在templates文件夹中创建index.html文件:
<html>
<head>
<title> Django Page</title>
</head>
<body>
<h1>Django</h1>
</form>
</body>
</html>
8.运行项目
python3 manage.py runserver
访问url:http://127.0.0.1:8000/index:

9.访问url:http://127.0.0.1:8000/admin会出现以下界面

10.运行以下命令:
#初始化数据库
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
生成的表结构如下:

创建用户时,依次输入username、email、password :

11.然后输入账户信息就可以登录成功了:

下载地址:https://github.com/doraemon4/djangoApp.git