标签:style blog http color io 使用 ar for 文件
#mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns(‘‘,
url(r‘^polls/‘, include(‘polls.urls‘)),
url(r‘^admin/‘, include(admin.site.urls)),
)
#polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns(‘‘,
url(r‘^$‘, views.index, name=‘index‘),
#name是url的名字,可以在template中使用
url(r‘^(?P<question_id>\d+)/$‘, views.detail, name=‘detail‘),
)
#polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
#使用这种方式更好,可以去掉hardcode, ‘detail’是在urls.py中定义的url的名字
<li><a href="{% url ‘detail‘ question.id %}">{{ question.question_text }}</a></li>
#mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns(‘‘,
url(r‘^polls/‘, include(‘polls.urls‘, namespace="polls")),
url(r‘^admin/‘, include(admin.site.urls)),
)
#引用url时要加名字空间
<li><a href="{% url ‘polls:detail‘ question.id %}">{{ question.question_text }}</a></li>
#效果等同
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404
question = get_object_or_404(Question, pk=question_id)
标签:style blog http color io 使用 ar for 文件
原文地址:http://www.cnblogs.com/phenixyu/p/3990501.html