Python 链接到 Django 页面的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22938101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way of linking to a page in Django
提问by qu4ntum
I managed to create a URL tag for my index. But right now I'm confused how to add links to other pages.
我设法为我的索引创建了一个 URL 标记。但是现在我很困惑如何添加指向其他页面的链接。
I put this on my urls.py
我把它放在我的urls.py 上
url(r'^$', 'index', name='index'),
The next thing I put this tag into the href:
接下来我将此标签放入href:
{% url 'index' %}
But what if I wanted to create a new page and would be linking to it. How would I do it the best way?
但是如果我想创建一个新页面并链接到它呢?我将如何做到最好?
采纳答案by aychedee
So next you would extend your urls.py
to look something like this:
所以接下来你会扩展你urls.py
的看起来像这样:
url(r'^$', 'index', name='index'),
url(r'^blog$', 'blog', name='blog'),
Then in your html you can use either one:
然后在您的 html 中,您可以使用其中之一:
<a href="{% url 'index' %}">Home</a>
<a href="{% url 'blog' %}">Blog</a>
You can of course use the template tage {% url 'index' %}
as many times as you need in any template.
您当然可以{% url 'index' %}
在任何模板中根据需要多次使用模板标签。
回答by Ajai
Create a new URL in the same format and give that name instead of index.
以相同的格式创建一个新 URL,并给出该名称而不是索引。
Eg:
例如:
url(r'^$', 'index', name='index'),
url(r'^new/page/$', 'new', name='new_page'),
{% url 'new_page' %}
回答by WeizhongTu
Just use the same label {% url 'index' %}
.
You may use each name
in urls.py to link to the url.
只需使用相同的标签{% url 'index' %}
。您可以name
在 urls.py 中使用 each来链接到 url。
urls.py
网址.py
url(r'^archive/$', 'mysite.views.archive',name='archive'),
url(r'^about/$', 'mysite.views.about',name='about'),
url(r'^contact/$', 'mysite.views.contact',name='contact'),
template
模板
<a href="{% url 'about' %}">About</a>
<a href="{% url 'contact' %}">Contact</a>
If you have many apps, use namespace
https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces-and-included-urlconfs
如果您有很多应用程序,请使用namespace
https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces-and-included-urlconfs
回答by Mihai Zamfir
Example:
例子:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about', name='about'),
)
Now, in the html template rendered by your views.index
you can have:
现在,在您呈现的 html 模板中,您views.index
可以拥有:
<a href ="{% url 'about' %}">about</a>
回答by uclaastro
Django has updated urlpatterns to take 'path' instead of using url so it's become much more efficient. You don't have to use regex anymore
Django 已更新 urlpatterns 以采用“路径”而不是使用 url,因此它变得更有效率。您不必再使用正则表达式
from django.urls import path
from . import views
urlpatterns=[
path('', views.index , name='index'),
path('blog/', views.blog , name='blog'),]
Then in templates, you can use template tagging
然后在模板中,可以使用模板标记
<a href="{% url 'index' %}">Index</a>
<a href="{% url 'blog' %}">Blog</a>
If you have multiple apps, you can tag it as follows. For example, if this is under 'post' app:
如果您有多个应用程序,您可以按如下方式标记它。例如,如果这是在“发布”应用程序下:
In post app urls.py:
在后应用 urls.py 中:
from django.urls import path
from . import views
app_name = 'post'
urlpatterns=[
path('', views.index , name='index'),
path('blog/', views.blog , name='blog'),]
in the project urls.py:
在项目 urls.py 中:
from django.urls import path, include
urlpatterns=[
path('post/', include('post.urls'),]
In templates, you do as following:
在模板中,您可以执行以下操作:
<a href="{% url 'post:index' %}">Index</a>
<a href="{% url 'post:blog' %}">Blog</a>