在Eclipse中创建Django 简单例子 之二

参考了《Django Web开发指南.pdf》第2章 Django速成:构建一个Blog, 我在本机创建这个例子,步骤记录如下:


本机环境:Fedora 20, Eclipse 4.3.1,Python 2.7, Django 1.5.5 PyDev 3.1.0...


(如何查看Django Pydev版本?

运行Python  >>>import django >>>print(django.get_version())

如何查看Pydev版本?

Eclipse-->Help-->About Eclipse Platform-->Installation Details-->Plug-ins

)


1: 创建项目:new project-->Pydev Django Project .input project name

   书上推荐使用命令,我没试过:django-admin.py startproject mysite


2:创建Blog应用

  书上推荐使用命令:./manage.py startapp blog

   我使用的方式是修改manage.py运行参数: 右键manage.py--> run as-->Run configurations

在Arguments tab 输入startapp blog。

  选择顶层目录后 选Refresh 可以看到blog目录


3:修改Settings.py的Installed_apps

书上建议 添加 ‘mysite.blog’.  Actually I only add 'blog' while I got error by adding 'mysite.blog'

The whole installed_apps sections is like below:

INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.admin',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'blog',


4. Coding for Models:

from django.db import models
from django.contrib import admin

# Create your models here.
class BlogPost(models.Model):
   title = models.CharField(max_length=150)
   body = models.TextField()
   timestamp = models.DateTimeField()

class BlogPostAdmin(admin.ModelAdmin):
   list_display = ('title','timestamp')

admin.site.register(BlogPost,BlogPostAdmin)


5. Setting Database.

5.1 Config database

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


5.2 Change manage.py run configurations to 'syncdb' and run manage.py

     or run ./manage.py syncdb

     It will create the sqlite.db and tables.

     Besides this, you will be asked to define administrator of this blog in this process.


6.  set 'django.contrib.admin'  in Installed_apps section of Settings.py

   

7.  Configure urls.py

from django.conf.urls import patterns
from view import  hello
from django.contrib import admin

urlpatterns = patterns('',
                      (r'^hello/$', hello),
                      (r'^admin/',admin.site.urls),

     

你可能感兴趣的:(eclipse,django)