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類同樣容易。

相關文章
相關標籤/搜索