Django理解


manager.py中


if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cwBlog.settings")


    from django.core.management import execute_from_command_line


    execute_from_command_line(sys.argv)

加载settings配置文件

配置文件中初始化数据库,安装app匹配,模版路径。url根目录。中间件匹配。

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


    "cwBlog/html/template/"
)

ROOT_URLCONF = 'cwBlog.urls'

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


    'cwBlog',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

当1个http请求产生。

通过urls.py解析路径。并投递到对应的view视图函数中。


urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'cwBlog.views.home', name='home'),
    # url(r'^cwBlog/', include('cwBlog.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),
    url(r'^blog/$', blog),
)


在视图函数中,加载数据模型,和模版。得到需要显示的数据以后,重绘模版。返回响应.

from django.template import loader, Context
from django.http import HttpResponse
from cwBlog.models import BlogPost


def blog(request):
    posts = BlogPost.objects.all()
    t = loader.get_template('blog.html')
    c = Context({'posts' : posts})


    return HttpResponse(t.render(c))

具体解析模版

其中post.timestamp | date:""

|表示后面接上过滤器

date格式化

{% extends "base.html" %}

{% block content %}

    {% for post in posts %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.timestamp | date:"1, F jS" }}</p>
        <p>{{ post.body }}</p>
    {% endfor %}

{% endblock %}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值