Django:如何在主页面上创建子页面链接

时间:2017-04-12 14:51:05

标签: python django hyperlink href

我已经成功部署了一些在urls.py中的页面,并且可以毫无问题地到达 还有默认页面应该包含指向 127.0.0.1/page1 127.0.0.1/page2 的链接。
怎么可能实现? sitename在settings.py中 我在index.html中尝试了以下内容:

<h3>This is the URL for "page1": <a href="{% url 'sitename:page1.html' %}"> Click here</a></h3>
> django.urls.exceptions.NoReverseMatch: 'sitename' is not a registered namespace

<h3>This is the URL for "page1": <a href="{% url 'page1.html' %}"> Click here</a></h3>
> Reverse for 'exp.html' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

2 个答案:

答案 0 :(得分:1)

我建议您先阅读此链接以了解alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name_txt = name.getText().toString(); String surname_txt = surname.getText().toString(); String email_txt = email.getText().toString(); String cellphone_txt = cellphone.getText().toString(); String postcode_txt = postcode.getText().toString(); if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) { if (debug_mode) {Log.i(TAG,"clause 1");} String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n"; // upload_to_github(data_to_upload); alert_dialog.dismiss(); } } }); } }); https://docs.djangoproject.com/en/1.10/topics/http/urls/#url-namespaces

解决方案就是这样:

django url structure

您必须在<h3>This is the URL for "page1": <a href="/page1"> Click here</a></h3> <h3>This is the URL for "page2": <a href="/page2"> Click here</a></h3>

中定义此网址

答案 1 :(得分:0)

您似乎正在尝试将模板名称传递给网址模板标记。你应该做的是在你的urls.py中定义url名称,所以看起来就像这样:

url(r'^page1/$', YourView.as_view(), name="page1")

然后在模板中:

<a href="{% url "page1" %}">Page 1</a>

你应该避免硬编码网址做这样的事情:

<a href="/page1">Page 1</a>

因为将来您想要更改网址时,您必须在两个地方执行此操作:在urls.py和模板中,而在使用{%url%}模板时urls.py将是唯一一个这样做的地方。