【脑图】Django URL调用关系

【脑图】Django URL调用关系_第1张图片

  1,topics.html 带参数,根据名称调用URL

  {{topic}}

   url 根据learning_logs的urls.py中名为topic的URL模式来生成合适的链接。

2,urls.py 定义URL-views内容-名称 对应关系

    #specific topic view

url(r'^details/(?P\d+)/$', views.topic, name='topic'),

特意url 改写成https://localhost:8000/details/1/ 而不是topics/1,完全没问题。

路由匹配(urls分发器),将一个个URL的页面请求分发给不同的View(视图)处理

views 定义操作(输入request和参数,输出内容到html)

def topic(request,topic_id):

    topic = Topic.objects.get(id=topic_id)

    entries = topic.entry_set.order_by('-date_added')

    context ={'topic':topic, 'entries': entries}

return render(request,'learning_logs/topic.html',context)

 

def new_entry(request,topic_id):

    topic =Topic.objects.get(id=topic_id)   

    if request.method !='POST':

        form = EntryForm()

    else:

        form = EntryForm(request.POST)

        if form.is_valid():

            new_entry = form.save(commit=False)

            new_entry.topic=topic

            new_entry.save()

            return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic_id]))

                                       

    context ={'topic':topic,'form':form}

    return render(request, 'learning_logs/new_entry.html',context)

views.py 是核心功能部分, 进行业务逻辑的处理,可能涉及到:

    1. html 通过urls.py,调用views.py 并传入参数,
    2. Model(从数据库中取数据)、
    3. 分辨get/post,展示form,存储form的数据, render 给html
    4. redirect 到其他url

3,topic.html 展现

   

            {% for entry in entries %}

               

  •                

    {{entry.date_added|date:'M d,Y H:i'}}

                   

    {{entry.text|linebreaks}}

               

  •         {% empty %}

               

  • There are no entries for this topic yet.
  •         {% endfor %}

       

你可能感兴趣的:(【脑图】Django URL调用关系)