《Django By Example》python那点技术

Django 妙用

2018-08-15  本文已影响2人  不爱去冒险的少年y

1.定义Model的时候,写一个unicode或者**str函数

from __future__ import unicode_literals
 
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
 
@python_2_unicode_compatible
class Article(models.Model):
    title = models.CharField('标题', max_length=256)
    content = models.TextField('内容')
 
    pub_date = models.DateTimeField('发表时间', auto_now_add=True, editable = True)
    update_time = models.DateTimeField('更新时间',auto_now=True, null=True)
 
    def __str__(self):
        return self.title

    def __unicode__(self):# 在Python3中用 __str__ 代替 __unicode__
        return self.title

python_2_unicode_compatible 会自动做一些处理去适应python不同的版本,本例中的 unicode_literals 可以让python2.x 也像 python3 那个处理 unicode 字符,以便有更好地兼容性。

作用图

2.配置需要在后台显示的字段,使用list_display

from django.contrib import admin
from .models import Article
 
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title','pub_date','update_time',)
 
admin.site.register(Article,ArticleAdmin)

list_display 就是来配置要显示的字段的,当然也可以显示非字段内容,或者字段相关的内容,比如:

#在model.py中
class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
 
    def my_property(self):
        return self.first_name + ' ' + self.last_name
    my_property.short_description = "Full name of the person"
 
    full_name = property(my_property)

在admin.py中
class PersonAdmin(admin.ModelAdmin):
    list_display = ('full_name',)
 
admin.site.register(Person, PersonAdmin)

3.在后台获取前台传来的数据是使用 request.GET.get('key',None),在没有数据时不会报错

4.django服务部署:

4.1:apache+wsgi+django部署:

  1. 安装 apache2 和 mod_wsgi
sudo apt-get install apache2
 
# Python 2
sudo apt-get install libapache2-mod-wsgi
 
# Python 3
sudo apt-get install libapache2-mod-wsgi-py3
  1. 确认安装的apache2版本号
apachectl -v
  1. 准备一个新网站
    ubuntu的apache2配置文件在 /etc/apache2/ 下
    备注:centos 用户 apache 名称为 httpd 在 /etc/httpd/ 中

新建一个网站配置文件
sudo vi /etc/apache2/sites-available/sitename.conf
示例内容如下:

<VirtualHost *:80>
    ServerName www.yourdomain.com
    ServerAlias otherdomain.com
    ServerAdmin tuweizhong@163.com
  
    Alias /media/ /home/tu/blog/media/
    Alias /static/ /home/tu/blog/static/
  
    <Directory /home/tu/blog/media>
        Require all granted
    </Directory>
  
    <Directory /home/tu/blog/static>
        Require all granted
    </Directory>
  
    WSGIScriptAlias / /home/tu/blog/blog/wsgi.py
    # WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
    # WSGIProcessGroup ziqiangxuetang.com
  
    <Directory /home/tu/blog/blog>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>

备注:把上面配置文件中这两句的备注去掉,可以使用 virtualenv 来部署网站,当然也可以只写一个 /home/tu/blog

 # WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
    # WSGIProcessGroup ziqiangxuetang.com

4.2:django+ngjnx+uwsgi socket部署:

5:发送邮件:

send_mail 每次发邮件都会建立一个连接,发多封邮件时建立多个连接。而 send_mass_mail 是建立单个连接发送多封邮件,所以一次性发送多封邮件时 send_mass_mail 要优于 send_mail

#一次发送一个邮件
from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)

#一次发送多个邮件
from django.core.mail import send_mass_mail
 
message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)
上一篇下一篇

猜你喜欢

热点阅读