django--创建网站小样

安装Django

首先在cmd下输入pip install Django,然后开始安装,Django将要被安装到Python的Lib下site-packages。

然后是配置环境变量,将这几个目录添加到系统环境变量中:C:\ProgramData\Anaconda3\Lib\site-packages\django. (前提已经安装了Anaconda3,且它自动会添加路径),添加完成后就可以使用Django的django-admin命令新建工程了。

检查是否安装成功

输入以下命令进行检查:

>>>import django

>>>print (django.get_version())

django--创建网站小样

新建项目和应用

  • 放项目文件:这里是E盘
  • 按住shift,再点击右键,打开CMD
  • 输入:django-admin startproject demoproject
  • 这样就新建了个名为demoproject的项目文件夹
  • 输入:django-admin  startapp demo
  • 这样就新建了一个名为search的应用文件夹
  • demo文件夹移到demoproject文件夹里面,并删除migrations文件


修改demoproject和demo相关.py文件
  • 打开settings.py,找到INSTALLED_APPS
  • TEMPLATE_DIRS = (“E:/search_engine/search_engine/search/templates”,)#模板文件路径
  • STATICFILES_DIRS = ( 
    os.path.join(BASE_DIR, “static”).replace(‘\’,’/’), 
    )#静态文件路径
settings.py
"""
Django settings for demoproject project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2xc$$9-f374bge^!m3j--1dn4c^_v1s152stc6mb&9het3vr+('

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

TEMPLATE_DEBUG = True
# 指明模板文件路径
TEMPLATE_DIRS = ("E:/demoproject/demoproject/demo/templates",)


ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     'demoproject.demo', 
]

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 = 'demoproject.urls'

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

WSGI_APPLICATION = 'demoproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__),'static')
STATICFILES_DIRS = (
    ('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ),
    ('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ),
    ('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ),
    ('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ),
)

修改urls.py
"""demoproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import staticfiles
from django.conf.urls import url
from django.contrib import admin
from demo import views

urlpatterns = ['',
    url(r'^$', views.search_form, name='home'),
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def search_form(request):
    return render('search_form.html')

url(r'^admin/', admin.site.urls),]#设置静态文件路径urlpatterns += staticfiles_urlpatterns()
 
    
 
    
 
   
修改views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def search_form(request):
    return render('search_form.html')

定义html模板search_form.html




    {% block title %}{% endblock %}


    {% block content %}
    {% endblock %}

    {% block footer %}
    {% endblock %}

  • 先在templates文件夹中新建一个基类模板base.html
  • 然后再新建一个search_form.html继承并扩展
{% extends "base.html" %}

{% block title %}Search_form{% endblock %}




欢迎来到樱桃小丸子的世界!

{% endblock %}

放入静态资源(图片,js,css)

  • static文件夹中新建一个images文件夹
  • 里面放入qi.png图片文件
最后显示:


最后图片加载不出来,我需要在进行了解,我有按照一篇博客http://www.cnblogs.com/pyfreshman/p/5046887.html还是不能加载出来,看来对Django 仅仅还在入门阶段,需要花费
本篇博客参考http://blog.csdn.net/ns2250225/article/details/43925975

你可能感兴趣的:(django--创建网站小样)