DJANGO_PART3

来源:互联网 发布:阿里云os 编辑:程序博客网 时间:2024/06/14 00:08
  • 创建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>