Table of Contents
To learn Django step by step.
1: 目标
2:重申开发步骤
3:定义数据库文件
4:模板文件
Hello world!
5. Django常用指令
6. 编程感悟
Home Web Front-end HTML Tutorial Django:web框架的学习(3)_html/css_WEB-ITnose

Django:web框架的学习(3)_html/css_WEB-ITnose

Jun 24, 2016 am 11:16 AM


借我杀死庸碌的情怀


初学者,一切都是个开始和摸索


To learn Django step by step.

001:Django:web框架的学习002:Django:web框架的学习(2)


核心知识:

  • 再次强化开发步骤和知识
  • 简易的HTML和CSS语法
  • 数据库操作
  • 模板的使用

1: 目标

先显示最终效果图:这是个包含背景颜色的网页,不是水印。

1465024645455.png

  • Wechat : 显示微信账号
  • Weibo:直接跳转至微博平台
  • Github: 直接跳转至Github平台
  • 简书: 直接跳转至简书平台
  • 更多:跳转至抓取的豆瓣电影Top250,后页面显示的部分:

1465025610484.png


2:重申开发步骤

  • 创建APP
  • 编辑视图文件
  • 配置url
  • 编辑模型文件定义数据库
  • 编辑模板文件

3:定义数据库文件

  • 账号数据表:对应于:wechat, 简书,github 等
class Message(models.Model):    Id_name = models.CharField(max_length=10)    Id_user = models.CharField(max_length=10)    Id_url = models.CharField(max_length=60)    Id_des = models.TextField(max_length=60)    def __str__(self):        return self.Id_user# 包含名称,账号,链接,介绍几个字段
Copy after login
  • 电影数据:这里不讲述数据如何抓取:爬虫看这里
class Film(models.Model):    Film_name = models.CharField(max_length=20)    Film_director = models.CharField(max_length=10)    Film_rate = models.FloatField(max_length=5)    Film_number = models.CharField(max_length=10)    Film_url = models.CharField(max_length=30)    Film_describe = models.TextField(max_length=60)    def __str__(self):        return self.Film_name# 定义5个字段和相应数据类型## 确保上述数据表中存在数据
Copy after login

4:模板文件

模板文件都是些前端知识,好吧,我不太会...

大致的内容有:

  • HTML的元素,属性,结构之类的
  • CSS层叠样式:修饰HTML文件中的内容的形式

举例:

  • HTML
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>谢小路</title></head><div></div><body><h1 id="Hello-world">Hello world!</h1></body></html># HTML文件长这样.
Copy after login
  • CSS

    nav{  background-color: #000000;  height: 50px;}#banner{  background: #CCCCCC;  height: 600px;}# 定义了背景颜色和高度# HTML 和CSS配合使用提升开发效率...
    Copy after login
  • Django中的使用新建一个文件夹专门存放模板文件,命名为templates,文件夹下可以创建文件夹以APP名称存放模板文件这里我创建了两个APP,一个名为one,一个为two其中one/film.html文件下显示的是:上文中更多显示的电影数据two/index.html文件显示的是:首页信息,即上文第一张图显示

|---templates|---templates/one/film.html|---templates/two/index.html
Copy after login
  • 设置模板文件路径setting.py
    TEMPLATES_DIR = (  os.path.join(BASE_DIR, 'templates/one/'),  os.path.join(BASE_DIR, 'templates/two/'),)
    Copy after login
  • 为了显示电影数据:修改one下的视图文件:views.py , film.html模板文件
def film(request):    film = Film.objects.order_by("id")    return render(request, 'one/film.html', {"Film": film})    pass
Copy after login
  • 为了显示账户信息等数据:修改two下视图文件:views.py index.html模板文件
def des(request):    message = Message.objects.order_by("id")    return render(request, 'two/index.html' , {"Message": message})
Copy after login
  • 配置url
urlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^film', film), # 这个显示电影数据    url(r'', des),  # 这个显示首页信息]
Copy after login

5. Django常用指令

  • django-admin.py startproject [filename] # 创建新项目
  • python manage.py runserver # 启动web服务
  • python manage.py startapp [APPNAME] # 创建web APP
  • python manage.py makemigrations
  • python manage.py migrate
  • python manage.py shell
  • python manage.py createsuperuser # 创建后台超级用户

6. 编程感悟

  1. 如果不是你遇到的实际问题,看任何实际的文章都存在着知识的盲区。
  2. 想要把一个东西说出来和写出来,和你感觉会,不是一码事。
  3. 这是个需要不断更新知识的时代,每天都存在着接触或者被动接触新东西,需要抓住自己的知识体系不断构建,不然易陷入贪多还不会的地步。
  4. 学会一点技术,重复是个少不了的技能。
  5. 摸索需要极大的成本,假如身边有牛人,千万别放过学习的机会...

在平坦的路上曲折前行...致:美貌大王 --许岑

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

See all articles