Django layui的第一个项目创建(pycharm开发工具)

在这里插入图片描述

1. 项目文件结构

我的项目创建如上

1.1 dojango0813/settings.py文件

"""
Django settings for dojango0813 project.

Generated by 'django-admin startproject' using Django 3.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g(ia4-zq9vxzqih(kuiuyo$+!@dpoodh@k9y4$o3f4sb1sz(z#'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'DjangoAppTest.apps.DjangoapptestConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
###根路由
ROOT_URLCONF = 'dojango0813.urls'

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',
            ],
        },
    },
]
##项目控制哪个。可见是dojango0813
WSGI_APPLICATION = 'dojango0813.wsgi.application'


# Database  数据库
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
###语言
# LANGUAGE_CODE = 'en-us'
#
# TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
###引入静态部分,静态文件资源。资源拼接
STATIC_URL = '/static/'
STATICFILES_DIRS = [
##通过BASE_DIR进行拼接
    os.path.join(BASE_DIR,'static')
]

1.2 dojangoMode/views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.

##请求定位到index,html
def index(request):
    ###返回字符串
    # return HttpResponse("hellp Django world")
    return render(request,"index.html")

1.2 dojango0813/urls.py

"""dojango0813 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url
from DjangoMode  import views
###引入html和css。模块导入
from django.contrib.staticfiles.urls import staticfiles_urlpatterns



urlpatterns = [
    url('admin/', admin.site.urls),
    #访问地址为http://127.0.0.1:8000/index/字符串显示
    url('index/',views.index)
]
###开始引入
# urlpatterns+=staticfiles_urlpatterns()

1.4 templates/index.html文件内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
{#    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">#}
{#   1.重点关注这里,引入的操作#}
{#    全局的{% load static %}#}
    {% load static %}
    
{#    1.1引入layui.all.js#}
    <link rel="stylesheet" href="{% static  "layui/layui.all.js" %}" media="all" >
{#        1.2引入layui.css#}

      <link rel="stylesheet" href="{% static  "layui/css/layui.css"%}"  media="all">
{#    <script src="../static/layui/layui.js"></script>#}
{#    <link href="../static/css" rel="stylesheet">#}
{#    <link href="../static/css" rel="stylesheet">#}
</head>
<body>
<h1>welcome</h1>
    <button type="button" class="layui-btn layui-btn-primary">原始按钮</button>
    <button type="button" class="layui-btn layui-btn-normal">百搭按钮</button>
</body>
</html>

错误出现

1.[13/Aug/2020 22:18:50] “GET /static/layui/css/layui.css HTTP/1.1” 200 562
问题:提示错误信息500信息,引入错误

[[13/Aug/2020 22:18:50] “GET /index/ HTTP/1.1” 200 669
[13/Aug/2020 22:18:50] “GET /static/layui/css/layui.css HTTP/1.1” 200 562

G:\dojango0813\dojango0813\urls.py changed, reloading.
Watching for file changes with StatReloader

解决:对照settings.py、html的引入css是否路径正常、引入信息完整
settings.py

###引入静态部分,静态文件资源。资源拼接
STATIC_URL = '/static/'
STATICFILES_DIRS = [
##通过BASE_DIR进行拼接
    os.path.join(BASE_DIR,'static')
]

index.html

    {% load static %}

{#    1.1引入layui.all.js#}
    <link rel="stylesheet" href="{% static  "layui/layui.all.js" %}" media="all" >
{#        1.2引入layui.css#}

      <link rel="stylesheet" href="{% static  "layui/css/layui.css"%}"  media="all">
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值