美文网首页
Hello Django

Hello Django

作者: 葛木瓜 | 来源:发表于2018-04-26 00:10 被阅读0次

主题

Django学习笔记之输出Hello Django

方法一

调用HttpResponse类向浏览器返回‘Hello Django’字符串

实现:

  1. 准备工作(cmd.exe)
# 创建一个Django项目
>> django-admin startproject helloDj
>> cd helloDj/helloDj/ # 进入helloDj/helloDj
# 创建django视图文件,控制前段显示内容
>> touch views.py 
  1. 配置url(urls.py)
from django.contrib import admin
from django.urls import path
from . import views # 导入views模块

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.hello), # 配置inde路由
]
  1. 定义index属性(views.py)
from django.http import HttpResponse

def hello(requeset):
    return HttpResponse('Hello Django !')
  1. 运行(cmd.exe)
>> python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 25, 2018 - 22:53:31
Django version 2.0.4, using settings 'helloDj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
  1. 结果,ok!


    浏览器截图

方法二

使用HTML模板
在helloDj/目录下创建templates/index.html文件

文件路径
  1. index.html
<html>
<h1>Hello World !</h1>
</html>
  1. views.py
from django.shortcuts import render
def hello(request):
    return render(request, "hello.html")
  1. urls.py保持不变
  2. 配置setting.py
...
'DIRS': [BASE_DIR+"/templates", ], # 很重要,否则报TemplateDoesNotExist
...   
  1. 结果,ok!


    浏览器截图

相关文章

  • Django学习1 hello world

    用Django写第一个页面hello与hello xxx 首先创建Django项目 _init_.py初始化文件 ...

  • Hello,Django!

    Django的安装 Option 1: Get the latest official version The l...

  • Hello Django

    主题 Django学习笔记之输出Hello Django 方法一 调用HttpResponse类向浏览器返回‘He...

  • Django 系列篇(二):配置篇(上)

    系列导读 01. Django 系列篇(一):Hello World! ​1. Django 有哪些配置 创建一个...

  • 1、hello django

    1、install pythonconda create -n envsource activate envcon...

  • Django - Hello World!

    基本Django使用命令行: 1. 创建一个Django项目 $ python-admin startprojec...

  • Django Hello,world

    使用Django,让浏览器显示Hello,world.制作你自己的第一个响应页面 设置好时区与语言 前提是你已经创...

  • 4.Django使用入门

    使用入门 1. 期望 访问 基于django框架开发的web网址, 页面输出 hello django 通过资源定...

  • django | 02-处理HTTP请求

    Django 处理 HTTP 请求 Hello 视图函数 我们先以一个最简单的 Hello World 为例来看看...

  • Web开发(五)Django模板应用

    在《初次使用》章节,使用 django.http.HttpResponse() 来输出 "Hello World!...

网友评论

      本文标题:Hello Django

      本文链接:https://www.haomeiwen.com/subject/calelftx.html