3、安装Eclipse的Python插件PyDev
Eclipse下执行Help—Install New Software...,输入网址:http://update-production-pydev.s3.amazonaws.com/pydev/updates/site.xml
安装成功后在Windows—Preferences中进行配置,添加Python解释器
如果在新建工程中有PyDev这一项则表示安装成功:
二、用Python+Django在Eclipse环境下开发自己的网站
1.新建Django项目
选择sqlite数据库
2.创建网站模块app
3.测试新建的模块是否正常
服务器启动起来后,去浏览器输入网址:http://127.0.0.1:8000/admin
4.编辑代码
4.1修改 MyBlog.models.py
from django.db import models
from django.contrib import admin
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length = 150)
content = models.TextField()
timestamp = models.DateTimeField()
class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title', 'content', 'timestamp')
admin.site.register(BlogPost, BlogPostAdmin)
4.2修改 MyBlog.views.py
# Create your views here.
from django.template import loader,Context
from django.http import HttpResponse
from MyBlog.models import BlogPost
def archive(request):
posts = BlogPost.objects.all()
t = loader.get_template('archive.html')
c = Context({'posts': posts})
return HttpResponse(t.render(c))
4.3 修改MySiteWithPython.setting.py,找到下面部分进行修改
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'MyBlog',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
4.4 修改MySiteWithPython.urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from MyBlog.views import *
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'MySiteWithPython.views.home', name='home'),
# url(r'^MySiteWithPython/', include('MySiteWithPython.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^MyBlog/$', archive),
)