zoukankan      html  css  js  c++  java
  • Django系列2:MTV简单流程

    1、模板使用方式1:在App目录下建立templates文件夹

    views.py

    from django.http import HttpResponse
    from django.shortcuts import render
    
    # Create your views here.
    
    
    def hello(request):
    
        return HttpResponse("views中的hello函数返回Hello")
    
    
    def index(request):
    
        return render(request, "index.html")
    
    

    urls.py

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

    settings.py

    # Application definition
    # django开头的为Django内置的应用
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'App'
    ]
    
    
    

    2、模板使用方式2:在项目目录下建立templates文件夹

    settings.py

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                os.path.join(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',
                ],
            },
        },
    ]
    

    views.py

    from django.http import HttpResponse
    from django.shortcuts import render
    
    # Create your views here.
    
    
    def hello(request):
    
        return HttpResponse("views中的hello函数返回Hello")
    
    
    def index(request):
    
        return render(request, "index.html")
    
    
    def home(request):
    
        return render(request, "home.html")
    

    urls.py

    from django.contrib import admin
    from django.urls import path
    
    from App import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('hello/', views.hello),
        path('index/', views.index),
    
        path('home/', views.home),
    
    ]
    
    

    3、Project下建立多App路由使用

    1) python manage.py startapp Two

    工程下建立Two的App

    2)settings.py注册Two

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'App',
        'Two'
    ]
    

    3) Two 下建立urls.py

    from django.conf.urls import url
    from Two import views
    
    urlpatterns = [
        url('index/', views.index),
    ]
    
    

    4) 工程下urls (include)

    from django.contrib import admin
    from django.urls import path, include
    
    from App import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('hello/', views.hello),
        path('index/', views.index),
    
        path('home/', views.home),
    
        path('two/', include('Two.urls')),
    
    ]
    
    

  • 相关阅读:
    前端开发 Knockout
    一套基于Spring Boot+Vue+Shiro前后端分离的代码生成器
    七个开源的 Spring Boot 前后端分离项目
    Java老司机:把这些主流技术搞懂,拿20K没问题
    svn无法cleanup解决方案
    软件测试-1挡板测试
    电子琴
    myeclipse10激活注册码生成器代码
    LNK2005
    无法打开包含文件:"fstream.h"
  • 原文地址:https://www.cnblogs.com/xidianzxm/p/12222088.html
Copyright © 2011-2022 走看看