eclipse创建一个django及管理界面排版和多语言实现

GreatZhou 2014-07-09

  1.  新建project及app:

            New-Other-Pydev Django project  -- 'django-utils'

            New-Django-Create Application --  'book'

   2.修改models.py和settings.py

            

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name
    

    class Meta:
        ordering = ['name']
        db_table = 'kt_publisher'

    在settings.py 的INSTALLED_APPS 元组中添加新建的App--‘book',修改数据库配置,首先在对应数据库中新建数据库。

   

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'book',  # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',  # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',  # Set to empty string for default.
    }
}

     数据库book是事先在mysql中建立的,django同步必须为已经存在的数据库才行。

    3.同步数据库

          django-sync DB(syncdb manage.py )

          同步成功后,可以在浏览器中访问了,http://192.168.0.1:8000/admin/

    4.管理自定义app

          django的后台管理界面默认现实为2个app--Groups  Users,如果想现实自定义的app如book,按如下配置:book-admin.py 

         

# django imports
from django.contrib import admin
from django import forms
from django.utils.translation import ugettext_lazy as _
from book.models import Publisher
class PublisherAdmin(admin.ModelAdmin):
    list_display = ('name', 'address', 'website')#列表现实字段
    search_fields = ('name',)#列表上面搜索框,搜索字段 (like匹配)
    list_filter = ('country',)#列表过滤字段
    fieldsets = (#详细界面,分类现实详细字段
        ('info', {'fields': ('name',)}),
        ('location',{'fields':('country','state_province','city','address')}),
        ('site',{'fields':('website',)})
    )
    
admin.site.register(Publisher, PublisherAdmin)#把上面配置注册到管理界面

    5.实现多语言

    在setting.py中添加设置:

    MIDDLEWARE_CLASSES增加'django.middleware.locale.LocaleMiddleware',该中间要出现在SessionMiddleware和CacheMiddleware后,其他所有中件间之前

    TEMPLATE_CONTEXT_PROCESSORS 增加 'django.core.context_processors.i18n'

     在app下新建文件夹locale/zh_CN(对应不同语言)/LG_MESSAGES/    新建文件django.po,内容如下:

    

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-01-07 12:56+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: templates/404.html:4 templates/404.html.py:8
msgid "Page not found"
msgstr "未找到对应页面"

#: templates/404.html:10
msgid "We're sorry, but the requested page could not be found."
msgstr "很抱歉,您所请求的页面未找到。"

#: templates/500.html:4
msgid "Home"
msgstr "主页"

#: templates/500.html:4
msgid "Server error"
msgstr "服务器错误"

#: templates/500.html:6
msgid "Server error (500)"
msgstr "服务器错误(500)"

#: templates/500.html:9
msgid "Server Error <em>(500)</em>"
msgstr "服务器错误 <em>(500)</em>"

#: templates/500.html:10
msgid ""
"There's been an error. It's been reported to the site administrators via e-"
"mail and should be fixed shortly. Thanks for your patience."
msgstr "系统发生错误。改错误已经通过邮件发给管理员处理。"

#: templates/admin/base_site.html:4
msgid "Site Admin"
msgstr "站点管理"

msgid "Change language"
msgstr "语言"

msgid "Simplified Chinese"
msgstr "简体中文"

msgid "Traditional Chinese"
msgstr "繁体中文"

msgid "Ignore"
msgstr "忽略"

#: settings.py:36
msgid "Inactive"
msgstr "无效"

#: settings.py:37
msgid "Active"
msgstr "有效"

#: settings.py:38
msgid "Suspended"
msgstr "暂停"


#: settings.py:91
msgid "Year"
msgstr "年"

#: settings.py:92
msgid "Month"
msgstr "月"

#: settings.py:93
msgid "Day"
msgstr "日"

 

    在terminal(终端)cd 到LG_MESSAGES目录下,运行命令:msgfmt django.po -o django.mo

    重启服务就会发现英文已经有所翻译,但是想要翻译制定的字段还要在django.po文件中配置,然后到目录下运行上面命令。

相关推荐

inspuryhq / 0评论 2020-07-28