$ django-admin startproject web_django
$ tree
.
├── manage.py
└── web_django
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
$ django-admin startapp user
.
├── manage.py
├── user
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── web_django
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
# settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'user.apps.UserConfig',
]
# settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'web_django',
'HOST':'localhost',
'PORT':3306,
'USER':'root',
'PASSWORD':'********',
}
}
# settings
LANGUAGE_CODE='zh-hans'
TIME_ZONE='Asia/Shanghai'
在settings文件最后添加
# settings
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
]
并在向项目中添加static文件夹
在web_django的urls.py文件中找到urlpatterns,向其中添加path(’’,include(‘user.urls’)),并在user当中添加url.py文件。
# web_django/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('user/',include('user.urls'))
]
# user/urls.py
from django.urls import path
from . import views
urlpatterns=[
path('',views.index_handler),
]
# user/urls.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def index_handler(request):
return HttpResponse('hello world')
绑定80端口
$ python manage.py runserver 80