djang2.0文档-概述

地址:
https://docs.djangoproject.com/en/2.0/intro/overview/html

设计模型,能够经过模型作增删改查,而不用面向数据库python

列:数据库

from django.db import models

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)

    def __str__(self):
        return self.full_name

class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline
python manage.py migrate

migrate 能根据模型在数据中建立不存在的表。
因而就能愉快的使用api来操做数据库。django

1,获取全部对象:api

Reporter.objects.all()

2,建立对象 后会本身生成id号缓存

r = Reporter(full_name='John Smith')
r.save()
# Now it has an ID.
>>> r.id
1

3,查询单个对象,经过id或者匹配内容框架

>>> Reporter.objects.get(id=1)
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__startswith='John')
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__contains='mith')
<Reporter: John Smith>
>>> Reporter.objects.get(id=2)
Traceback (most recent call last):
    ...
DoesNotExist: Reporter matching query does not exist.

3,建立关联对象this

>>> from datetime import date
>>> a = Article(pub_date=date.today(), headline='Django is cool',
...     content='Yeah.', reporter=r)
>>> a.save()

4,查关联对象url

>>> r = a.reporter
>>> r.full_name
'John Smith'

5,经过类方法,查询设计

>>> Article.objects.filter(reporter__full_name__startswith='John')
<QuerySet [<Article: Django is cool>]>

6,删除对象

# Delete an object with delete().
>>> r.delete()

 

7,经过在admin内容管理注册你的模型,即可在admin页面里面管理模型对应数据的增删改查,方法以下。这里的设计理论是为了作简单管理内容。django 快速开发内容管理后台就是用到这里,很是快捷方便的实现用户,内容管理。

mysite/news/admin.py

from . import models

admin.site.register(models.Article)

8,url地址对应关系设置

mysite/news/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('articles/<int:year>/', views.year_archive),
    path('articles/<int:year>/<int:month>/', views.month_archive),
    path('articles/<int:year>/<int:month>/<int:pk>/', views.article_detail),
]

For example, if a user requested the URL “/articles/2005/05/39323/”, Django would call the functionnews.views.article_detail(request, year=2005, month=5, pk=39323).

9,编写view 对应方法,返回一个response或者http404,这里使用了模板

mysite/news/views.py

from django.shortcuts import render

from .models import Article

def year_archive(request, year):
    a_list = Article.objects.filter(pub_date__year=year)
    context = {'year': year, 'article_list': a_list}
    return render(request, 'news/year_archive.html', context)

 

10,设计模板,须要在setting列出模板的路径dirs的列表,一个没找到会在下一个里面寻找。

mysite/news/templates/news/year_archive.html

加载基础html,对象循环

mysite/news/templates/news/year_archive.html

{% extends "base.html" %}

{% block title %}Articles for {{ year }}{% endblock %}

{% block content %}
<h1>Articles for {{ year }}</h1>

{% for article in article_list %}
    <p>{{ article.headline }}</p>
    <p>By {{ article.reporter.full_name }}</p>
    <p>Published {{ article.pub_date|date:"F j, Y" }}</p>
{% endfor %}
{% endblock %}

{{ article.pub_date|date:"F j, Y" }} uses a Unix-style “pipe” (the “|” character). This is called a template filter, and it’s a way to filter the value of a variable. In this case, the date filter formats a Python datetime object in the given format,模组里面的filter

mysite/templates/base.html

{% load static %}
<html>
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <img src="{% static "images/sitelogo.png" %}" alt="Logo" />
    {% block content %}{% endblock %}
</body>
</html>

base.html里面里面是一些通用的元素

 

其余特征:

1,缓存框架的后台

2,一个联合框架,使得建立RSS和Atom提要和编写一个小Python类同样容易。

相关文章
相关标签/搜索