Django18初体验


1072 浏览 6 years, 1 month

2.6 第一个视图及路由

版权声明: 转载请注明出处 http://www.codingsoho.com/

添加最基本的view功能

首先添加url

trydjango18\urls.py

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'newsletter.views.home', name='home'),
)
Symbol / Expression Matched String
. (Dot) Any character.
^ (Caret) Start of string.
$ End of string.
* 0 or more repetitions.
+ 1 or more repetitions.
? 0 or 1 repetitions.
| A | B means A or B.
[a-z] Any lowercase character.
\w Any alphanumeric character or _.
\d Any digit.

这儿的name后面可以用在template里面的url引用,例如

templates\navbar.html

<li class="active"><a href="{% url 'home' %}">Home</a></li>

添加view里面的实现

newsletter\view.py

from django.shortcuts import render 

# Create your views here.
def home(request):
    context = {}
    return render(request, "home.html", context)