为django编写正确的URL.py和包含超链接的模板
我将继续学习django教程(1.6 python 3.3)。然而,我一直在尝试在自己的测试应用程序中复制教程 我想在索引页上显示一个只有3个超链接的列表。现在的超链接只是列出的项目的名称 视图.py为django编写正确的URL.py和包含超链接的模板,django,Django,我将继续学习django教程(1.6 python 3.3)。然而,我一直在尝试在自己的测试应用程序中复制教程 我想在索引页上显示一个只有3个超链接的列表。现在的超链接只是列出的项目的名称 视图.py def Index(request): tables = [] tables.append('Country') tables.append('County') tables.append('Registration Body') return render
def Index(request):
tables = []
tables.append('Country')
tables.append('County')
tables.append('Registration Body')
return render(request, 'test_app/index.html', {'table_list': tables})
from django.conf.urls import patterns, url
from test_app import views
urlpatterns = patterns('test_app',
#url(r'^Country/', views.Index, name='index'),
url(r'^$', views.Index, name='index'),
)
url.py
def Index(request):
tables = []
tables.append('Country')
tables.append('County')
tables.append('Registration Body')
return render(request, 'test_app/index.html', {'table_list': tables})
from django.conf.urls import patterns, url
from test_app import views
urlpatterns = patterns('test_app',
#url(r'^Country/', views.Index, name='index'),
url(r'^$', views.Index, name='index'),
)
Index.html
<h1>List of Config Tables in TestApp</h1>
{% if table_list %}
<ul>
{% for tbl_name in table_list %}
<li><a href="{% url 'test_app:index' tbl_name%}">{{ tbl_name }}</a></li>
{% endfor %}
</ul>
{% else %}
<ul>
<p>No table found<p>
</ul>
{% endif %}
奇怪的是,当我在索引页面中去掉href中的
tbl_name
时,我可以得到我的列表,但列表中没有超链接 视图和URL中不允许使用参数。但是您将一个参数传递给视图函数。您应该添加它,或者只是从模板中的URL中删除tbl_名称。对不起,我不明白。在索引视图中,我传递强制的请求
对象。我应该递点别的吗?另外,如果我从模板中的URL中删除tbl\u名称
,我将如何获得类似于http://test_app/country/
或http://test_app/county/
?请详细说明。在URL中,您可以写^/(?P.+)/$。在视图中,定义索引(请求、名称)。名称将是tbl_uuuuu名称OK。你的建议很管用。但是,我没有在视图中添加额外的变量。。。。看来没必要了?我需要重新阅读文档或其他东西,因为即使它起作用了,我也不理解它的概念和它的问题,我的问题是,当Django加载索引页面时,或者当我实际单击链接时,是否正在评估超链接?我原以为只有url(r'^$',views.Index,name='Index'),
就足以只加载索引页面了?