DJANGO_PART3

  • 创建VIEWS(创建路由函数)
    Blog homepage – displays the latest few entries.
    Entry “detail” page – permalink page for a single entry.
    Year-based archive page – displays all months with entries in the given year.
    Month-based archive page – displays all days with entries in the given month.
    Day-based archive page – displays all entries in the given day.
    Comment action – handles posting comments to a given entry.
    In our poll application, we’ll have the following four views:
    Question “index” page – displays the latest few questions.
    Question “detail” page – displays a question text, with no results but with a form to vote.
    Question “results” page – displays results for a particular question.
    Vote action – handles voting for a particular choice in a particular question.
    // VIEWS 不是ROUTE处理函数就是基于类视图的方法

  • URL注册步骤
    mysite/urls里的urlpatterns注册新的APP的路由
    在APP内的urls.py的urlpatterns注册各个子路由
    路由和对应的操作函数的参数传递 url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail')#?P<question_id> 参数传递

  • 页面设计
    这里有一个问题:页面的设计在视图中是硬编码(写死的)的。如果你想改变页面的外观,你必须编辑这个Python代码。所以让我们使用Django的模板系统,通过创建视图可以使用的模板来将设计从Python中分离出来

  • 使用DJANGO模板体系
    项目的模板设置在mysite/settings/TEMPLATES
    在POLLS 文件夹下创建templates文件夹
    DJANGO从INSTALLED_APPS的每个文件夹寻找templates文件夹。所以为了避免重复 在templates文件夹下在创建个POLLS文件夹。整个路径是1Django-first-app\mysite\polls\templates\polls
    代码

#模板文件
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
#路由函数
def index(request):
    lastest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    content = {
        'latest_question_list':lastest_question_list,
    }
    return HttpResponse(template.render(content,request))
#或者
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)#render()第一个参数是请求对象。第二个参数是模板名称。第三个参数是可选的传递给模板的字典。
    #这里的render()跟上个函数的template.render()不是同一个方法,这个是单独导入的,并且返回HttpResponse

question = get_object_or_404(Question,pk=question_id)相当于

    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")

get_list_or_404()相当于filter()
Question.objects.get(pk=1)==Question.objects.get(id=1)

  • 移除模板里的硬编码
    这种硬编码,紧密耦合的方法存在的问题是,使用大量模板更改项目的URL会变得非常困难。但是,由于您在polls.urls模块的path()函数中定义了name参数,因此可以使用{%url%}模板标记删除对URL配置中定义的特定URL路径的依赖:
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
    url ‘detail’是从polls/urls中选择’detail’ 对应的内容,并传递question.id。这样如果要修改路由只要在polls/urls中修改就行了不用动模板

  • 确定app的命名空间
    实际项目中,一个项目有很多个APP,不同的的项目中的很多URL路由都会相同,比如POLLS APP有/detail,Blog APP也有/detail。
    在polls/urls 中定义app_name = 'polls'.前面的引用URL的函数也变成
    <li><a href="{% url 'polls:detail'#改变 question.id %}">{{ question.question_text }}</a></li>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值