1.创建项目——Django学习

1. 创建运行项目

(python3.8) [root@node8 python-project]# django-admin.py startproject test1
(python3.8) [root@node8 python-project]# cd test1/
(python3.8) [root@node8 test1]# python manage.py runserver 0:8080

2. 视图

vim test1/test1/views.py 
from django.http import HttpResponse
def index(request):
    return HttpResponse("test1")

3. 路由

vim test1/test1/urls.py  
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.test1),
]

继续奏乐,继续修改

vim  test1/test1/urls.py  
from django.urls import path 
from . import views 
urlpatterns = [
    path('^$/', views.hello),
]
vim test1/settings.py
ALLOWED_HOSTS = ['192.168.0.81']

4. 模板

(python3.8) [root@node8 test1]# pwd
/blueicex/python-project/test1
(python3.8) [root@node8 test1]# ls
db.sqlite3  manage.py  test1
(python3.8) [root@node8 test1]# mkdir templates
vim test1/templates/temp1.html  
<h1>{{ var1 }}</h1>

增加HelloWorld/settings.py关于模板的设置

vim HelloWorld/settings.py          
...TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],       # 修改位置
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...

增加模板视图及路由

vim test1/test1/views.py 
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
    return HttpResponse("test")
def temp1(request):
    context={}
    context['var1']="test"
    return render(request,"temp1.html",context)
', context)
vim  test1/test1/urls.py  
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    path('temp1/',views.temp1)
]

————Blueicex 2020/07/21 07:14 [email protected]

你可能感兴趣的:(Django,django)