创建一个基于Python的Django应用程序

安装Django:

下载:https://www.djangoproject.com/download/

解压后运行:python setup.py install


参考资料:http://www.cnblogs.com/icyfire/archive/2011/10/10/writing_your_first_django_app.html

1. 创建demo工程

python django-admin.py startproject demo

2. 创建app

python manage.py blog

3. 新建model

在/demo/blog下修改models.py文件如下:

from django.db import models

# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=32)
    def __unicode__(self):
        return self.name
    class Admin:
        pass

class Article(models.Model):
    title = models.CharField(max_length=64)
    published_at = models.DateTimeField('date published')
    content = models.TextField()
    category = models.ForeignKey(Category)
    def __unicode__(self):
        return self.title
    class Admin:
        pass

4. 修改demo下settings.py文件:

a) INSTALLED_APPS 中添加 'blog',例如:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    
    'blog',
)
b) TEMPLATE_DIRS 中添加工程所在路径:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    
    "C:/Users/shenbin/My Documents/Aptana Studio 3 Workspace/demo/demo"
)

5. 建立数据库和表:注意,首先需要检查settings.py文件中关于数据库的设置是否正确。

python manage.py syncdb

6. 新建view页面:

article_list.html

{% if object_list %}
	{% for article in object_list %}
		<div class="article">
			<div class="title">
				<a href="/blog/{{ article.id }}">{{ article.title }}</a>
			</div>
		</div>
	{% endfor %}
{% else %}
	<p>
		Sorry, No Article here.
	</p>
{% endif %}
article_detail.html
<div class="article">
	<div class="title">
		Title: {{ object.title }}
	</div>
	<div class="pub_date">
		{{ object.published_at }}
	</div>
	<div class="content">
		{{ object.content }}
	</div>
	<div class="category">
		Published at: {{ object.category.name }}
	</div>
</div>
<p>
	<a href="/admin/blog/article/{{ object.id }}">Edit</a>
</p>
<p>
	<a href="/blog">BACK</a>
</p>


7. 修改urls.py文件如下:

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()

from demo.blog.models import Article
#admin.site.register(Article)

#from demo.blog.models import Category
#admin.site.register(Category)

show_list = {
    'queryset' : Article.objects.all(),
}

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'demo.views.home', name='home'),
    # url(r'^demo/', include('demo.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'^blog/', 'django.views.generic.list_detail.object_list', show_list),
    url(r'^blog/(?P<id>\d+)/$', 'django.views.generic.list_detail.object_detail', show_list),
)

启动django:

python manage.py runserver 0.0.0.0:8000

访问: http://localhost:8000/blog 即可。


其他资料:

http://www.tingcheba.com/

http://simple-is-better.com/news/275


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值