搜尋

Python-Django

Feb 15, 2017 pm 03:30 PM
djangopython

準備工作

新建一個Django專案

# 新建一个django项目
$ django-admin startproject mysite
# 新建一个app
$ django-admin startapp blog

專案的結構

├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
# mysite/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog', 
    'markdown2'
]
$ python3 manage.py runserver

$ python manage.py collectstatic

一般在urls.py中配置url,在models.py中配置model,在views.py中配置View。

urls.py

Function views

1. Add an import:  from my_app import views
2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')

Class-based views

1. Add an import:  from other_app.views import Home
2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')

other例如(?Pd+),當造訪/blog/article/3時,將會將3捕獲給article_id,這個值會傳到views.ArticleDetailView。 <pre class="brush:php;toolbar:false">1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))</pre>其中namespace參數為我們指定了命名空間,這說明這個urls.py中的url是blog app下的,這樣即使不同的app下有相同url也不會衝突了。

假設使用者要存取某篇文章,它會自動解析blog:detail 這個視圖函數對應的url,並且把article.pk(文章的主鍵)傳遞給detail視圖函數,

details

就是我們在

blog/urls .py

中指定的name<pre class="brush:php;toolbar:false"># blog/urls.py from django.conf.urls import url from blog import views urlpatterns = [     url(r'^blog/$', views.IndexView.as_view(), name='index'),     url(r'^blog/article/(?P&lt;article_id&gt;\d+)$', views.ArticleDetailView.as_view(), name='detail'),     url(r'^blog/category/(?P&lt;cate_id&gt;\d+)$', views.CategoryView.as_view(), name='category'),     url(r'^blog/tag/(?P&lt;tag_id&gt;\d+)$', views.TagView.as_view(), name='tag'), ]&lt;/tag_id&gt;&lt;/cate_id&gt;&lt;/article_id&gt;</pre>如果要訪問某個目錄<pre class="brush:php;toolbar:false"># mysite/urls.py from django.conf.urls import url, include from django.contrib import admin from blog import views urlpatterns = [     url(r'^admin/', admin.site.urls),     url(r'', include('blog.urls', namespace='blog', app_name='blog')) ]</pre>models.py

django.db.models

是orm框架的基礎,在

blog/models.py

,Ar 三個model。

<a>{{ article.title }}</a>
Django給我們提供了很多有用的字段,例如上面提到的CharFiled, TestField, DateTimeFiled等等,詳情請參考官方文檔。 Django中的一對多是在一中進行設置,這裡對應於文章的分類,ForeignKey即資料庫中的外鍵。 on_delete=models.SET_NULL表示刪除某個分類(category)後該分類下所有的Article的外鍵設為null(空),所以我們同時設定了null=True。多對多就不同,兩邊都要進行配置。詳情請參考官方文件。

<a>{{ category.name }}</a>
class Article(models.Model):
    STATUS_CHOICES = (
        ('d', 'Draft'),
        ('p', 'Published'),
    )
    
    # 仍然使用默认的 objects 作为 manager 的名字
    objects = ArticleManager()

    title = models.CharField('标题', max_length=70)
    body = models.TextField('正文')
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    last_modified_time = models.DateTimeField('修改时间', auto_now=True)
    status = models.CharField('文章状态', max_length=1, choices=STATUS_CHOICES)
    # blank和null要同时设置为null,详情参考官方文档
    abstract = models.CharField('摘要', max_length=54, blank=True, null=True, 
                                help_text="可选,如若为空将摘取正文的前54个字符")
    views = models.PositiveIntegerField('浏览量', default=0)
    likes = models.PositiveIntegerField('点赞数', default=0)
    topped = models.BooleanField('置顶', default=False)
    
    category = models.ForeignKey('Category', verbose_name='分类', null=True, on_delete=models.SET_NULL)
    tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-last_modified_time']

    # 新增 get_absolute_url 方法
    def get_absolute_url(self):
        # 这里 reverse 解析 blog:detail 视图函数对应的 url
        return reverse('blog:detail', kwargs={'article_id': self.pk})
評論功能的實作
class Category(models.Model):
    name = models.CharField('类名', max_length=20)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    last_modified_time = models.DateTimeField('修改时间', auto_now=True)

    def __str__(self):
        return self.name
class Tag(models.Model):
    name = models.CharField('标签名', max_length=20)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    last_modified_time = models.DateTimeField('修改时间', auto_now=True)

    def __str__(self):
        return self.name
我們首先要在project_name/settings.py中配置好相應的設定檔

class BlogComment(models.Model):
    user_name = models.CharField('评论者名字', max_length=100)
    user_email = models.EmailField('评论者邮箱', max_length=255)
    body = models.TextField('评论内容')
    created_time = models.DateTimeField('评论发表时间', auto_now_add=True)
    article = models.ForeignKey('Article', verbose_name='评论所属文章', on_delete=models.CASCADE)

    def __str__(self):
        return self.body[:20]
定義完畢後,我們執行下面的命令就在對應的資料庫中產生完畢後,我們執行下面的命令就在對應的資料表:

class ArticleManage(models.Manager):
    """
    继承自默认的 Manager ,为其添加一个自定义的 archive 方法
    """
    def archive(self):
        date_list = Article.objects.datetimes('created_time', 'month', order='DESC')
        # 获取到降序排列的精确到月份且已去重的文章发表时间列表
        # 并把列表转为一个字典,字典的键为年份,值为该年份下对应的月份列表
        date_dict = defaultdict(list)
        for d in date_list:
            date_dict[d.year].append(d.month)
        # 模板不支持defaultdict,因此我们把它转换成一个二级列表,由于字典转换后无序,因此重新降序排序
        return sorted(date_dict.items(), reverse=True)
admins.py參考Mozila的教學以及結合官方文件。

views.py

下面要使用markdown2,所以在INSTALLED_APP裡面要加入markdown2,不過這個mardown解析非常的不好,並且弄完還要去下載對應的markdown的css文件,有個專門的網站。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}
上面因為我們要進行markdown處理,所以重新自訂了

get_queryset

,如果不要進行相應的處理,直接製定

model

就行了,

get_context_data

可以添加一些額外的字段,比如以後我們要在字段首頁的側邊欄顯示目錄和標籤,所以這裡要新增一個

category_list

tag_list

$ python manage.py makemigrations

$ python manage.py migrate
from blog.models import Article, Tag, Category
from django.views.generic import ListView, DetailView
import markdown2

class IndexView(ListView):
    # template_name属性用于指定使用哪个模板进行渲染
    template_name = "blog/index.html"

    # context_object_name属性用于给上下文变量取名(在模板中使用该名字)
    context_object_name = "article_list"

    def get_queryset(self):
        article_list = Article.objects.filter(status='p')
        for article in article_list:
            article.body = markdown2.markdown(article.body, )
        return article_list

    def get_context_data(self, **kwargs):
        kwargs['category_list'] = Category.objects.all().order_by('name')
        # 调用 archive 方法,把获取的时间列表插入到 context 上下文中以便在模板中渲染
        kwargs['date_archive'] = Article.objects.archive()
        kwargs['tag_list'] = Tag.objects.all().order_by('name')
        return super(IndexView, self).get_context_data(**kwargs)
class ArticleDetailView(DetailView):
    model = Article
    template_name = "blog/detail.html"
    context_object_name = "article"
    # pk_url_kwarg会自动和model中相应的主键对应,aritlce_id就是下面配置的URLCONF
    pk_url_kwarg = 'article_id'

    # 为了让文章以markdown形式展现,我们重写get_object()方法
    def get_object(self):
        obj = super(ArticleDetailView, self).get_object()
        obj.body = markdown2.markdown(obj.body)
        return obj
        
    # 新增 form 到 context
    def get_context_data(self, **kwargs):
        kwargs['comment_list'] = self.object.blogcomment_set.all()
        kwargs['form'] = BlogCommentForm()
        return super(ArticleDetailView, self).get_context_data(**kwargs)
class CategoryView(ListView):
    template_name = "blog/index.html"
    context_object_name = "article_list"
    
    def get_queryset(self):
        # url里的cate_id传递给CategoryView,传递的参数在kwargs属性中获取
        article_list = Article.objects.filter(category=self.kwargs['cate_id'],status='p')
        for article in article_list:
            article.body = markdown2.markdown(article.body, )
        return article_list

    def get_context_data(self, **kwargs):
        # 增加一个category_list,用于在页面显示所有分类,按照名字排序
        kwargs['category_list'] = Category.objects.all().order_by('name')
        return super(CategoryView, self).get_context_data(**kwargs)
template{% for %}循環標籤,{% if %}判斷標籤. {{ variable }是一些非常常用的標籤

views.py中已經指定了

context_object_name = "article_list"

,並且已經在get_queryset()中進行了markdown處理<pre class="brush:php;toolbar:false">class TagView(ListView):     template_name = &quot;blog/index.html&quot;     context_object_name = &quot;article_list&quot;     def get_queryset(self):         &quot;&quot;&quot;         根据指定的标签获取该标签下的全部文章         &quot;&quot;&quot;         article_list = Article.objects.filter(tags=self.kwargs['tag_id'], status='p')         for article in article_list:             article.body = markdown2.markdown(article.body, extras=['fenced-code-blocks'], )         return article_list     def get_context_data(self, **kwargs):         kwargs['tag_list'] = Tag.objects.all().order_by('name')         return super(TagView, self).get_context_data(**kwargs)</pre>通常都會設定一個通用的父模板:

from django.views.generic.edit import FormView

class CommentPostView(FormView):
    form_class = BlogCommentForm
    template_name = 'blog/detail.html' 

    def form_valid(self, form):
        target_article = get_object_or_404(Article, pk=self.kwargs['article_id'])
        # 调用ModelForm的save方法保存评论,设置commit=False则先不保存到数据库,
        # 而是返回生成的comment实例,直到真正调用save方法时才保存到数据库。
        comment = form.save(commit=False)
        # 把评论和文章关联
        comment.article = target_article
        comment.save()
        # 评论生成成功,重定向到被评论的文章页面,get_absolute_url 请看下面的讲解。
        self.success_url = target_article.get_absolute_url()
        return HttpResponseRedirect(self.success_url)

    def form_invalid(self, form):
        target_article = get_object_or_404(Article, pk=self.kwargs['article_id'])

        # 不保存评论,回到原来提交评论的文章详情页面
        return render(self.request, 'blog/detail.html', {
            'form': form,
            'article': target_article,
            'comment_list': target_article.blogcomment_set.all(),
        })
靜態檔案由於原始程式碼遺失,具體情況記得不太清晰,靜態檔案路徑要設定好,如果js檔案載入異常,可能是載入順序的問題。

base_generic.html大概就是下面這樣的格式:

{% for article in article_list %}
    {{article.title}}
下面這樣設定似乎有點問題:
{% extends "base_generic.html" %}

{% block content %}
...
{% endblock %}
具體參考官方文件

部署

使用uws .conf,blog是app名字,static檔案放在了下面,建議直接放在mysite下面,template也是一樣:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'blog/templates')]
        ,
        'APP_DIRS': True,
...
]

uwsgi -i uwsgi.ini

來啟動uwsgi進程,結合

nohup &

改進

目前文章是直接在admin.py中註冊model,然後去admin後台發布的,可以做成api接口,做一個在線的編輯器。增加基本的用戶認證功能。

零碎知識點

null和blank的區別

null 是針對資料庫而言,如果 null=True, 表示資料庫的該欄位可以為空。

blank 是針對表單的,如果 blank=True,表示你的表單填寫該欄位的時候可以不填,例如 admin 介面下增加 model 一筆記錄的時候。直觀的看到就是該字段不是粗體。

render與render_response

優先採用render。

get_absolute_url
  • model有一個get_absolute_url,它可以與reverse結合。

    更多Python-Django相關文章請關注PHP中文網!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器