python 使用django基本步骤和示例
python在windows中啟動方法兩種:
1.通過windows中使用控制臺
2.通過pycharm啟動
?
?
一、在windows中啟動:
1.Win+R ?--> CMD
2.下載django包
? ??pip3 install django
3.創建一個項目叫mysite
? django-admin startproject mysite
4.創建一個應用叫blog
? ?django-admin manage.py startapp blog
5.可以查看一下mysite的目錄
//blog也是一個文件,查看blog 目錄cd blogadmin.pytests.pyviews.py 盛放實現具體功能的函數的models.py 操作數據庫的,放與操作數據庫相關的內容admin.py 強大的后臺管理系統,通過web頁面管理后臺settings.py 全局的與django相關的所有的配置信息進入mysite/mysite目錄:setting.pyINSTALLED_APP = {存放所有的應用名稱,比如blog"blog",}urls.pyurlpatterns = [ #把路徑分發到不同的路徑函數里去path('admin/', admin.site.urls), #自帶的,提供一個管理數據庫的頁面,model是創建數據庫path('login',login/) #前面login是路徑,后面是函數]__init__.py二、在pycharm中創建一個基本功能,顯示頁面的
1.打開pycharm
2.File --> New? Project--> Django--> 路徑后面名字就叫django_lesson --> Create
?
?
4.左邊的目錄
k
?
5.目錄展開
6.編輯: urls.py
from django.contrib import admin from django.urls import path #from django.conf.urls import url from blog import viewsurlpatterns = [path('admin/', admin.site.urls),path('show_time/',views.show_time),]備注:? 第一個show_time/前面不能跟^ 符號,否則找不到此路徑
?
7.編輯? blog-->migrations-->views
?
from django.shortcuts import render,HttpResponse import time# Create your views here.#瀏覽器發起請求后,客戶端的請求信息全部打包在req里面,return一個http response對象 def show_time(req):#return HttpResponse("hello")t=time.ctime()#return render(req,"index.html",locals())return render(req,"index.html",{"time":t}) #render做渲染,只要是返回的,都是返回的HttpResponse對象?
在右側的templates中創建一個名為index.html的頁面
編輯index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>hello yuan</h1> <h1>hello {{ time }}</h1> </body> </html>8.在pycharm的右下角的Terminal中啟動該項目
python? manage.py? ?runserver? 8889
9.在瀏覽器訪問8889端口的show_time路徑,輸入? http://127.0.0.1:8889/show_time
?
?
三、在django中引入js文件,js文件要放在static目錄中,這個目錄跟app目錄平行,同一級
#setting.py""" Django settings for django_lesson project.Generated by 'django-admin startproject' using Django 2.2.2.For more information on this file, see https://docs.djangoproject.com/en/2.2/topics/settings/For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/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/2.2/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 't9l6%u!6pwjsdnufk9bmvq6khd1p)b(z_e6@9iuj^wc8ado_yp'# SECURITY WARNING: don't run with debug turned on in production! DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','blog.apps.BlogConfig', ]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 = 'django_lesson.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',],},}, ]WSGI_APPLICATION = 'django_lesson.wsgi.application'# Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'db.sqlite3'),} }# Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validatorsAUTH_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/2.2/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.2/howto/static-files/STATIC_URL = '/static/' #別名,不是絕對路徑 STATICFILES_DIRS=(os.path.join(BASE_DIR,"static"), )?
#index.tml<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>hello yuan</h1> <h1>hello {{ time }}</h1> #<script src="/static/jquery-xx.js"></script> #方法一 <script src="{% static 'jquery-xxx.js' %}"></script> <script>$("h1").css("color","red") </script> </body> </html>?
總結
以上是生活随笔為你收集整理的python 使用django基本步骤和示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win10下配置IIS并调试PHP程序
- 下一篇: c语言成绩管理系统报告书,C语言学生成绩