32π
So next you would extend your urls.py
to look something like this:
url(r'^$', 'index', name='index'),
url(r'^blog$', 'blog', name='blog'),
Then in your html you can use either one:
<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.
33π
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
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:
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:
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>
4π
Just use the same label {% url 'index' %}
.
You may use each name
in urls.py to link to the url.
urls.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
- Django queryset filter GT, LT, GTE, LTE returns full object list
- Psycopg2 cannot connect to docker image
- {% load static %} and {% load staticfiles %}: which is preferred?
- How to give initial value in modelform
2π
Use the below syntax:
<a href="{% url 'cart' %}" > Cart page </a>
Where URL βcartβ is the name field in your app URL pattern.
urlpatterns = [
#Leave as empty string for base url
path('', views.shop, name="store"),
path('cart/', views.cart, name="cart"),--------------------->
path('checkout/', views.checkout, name="checkout"),
path('shop/',views.shop, name="shop"),
]
- Django admin.TabularInline auto insert three blank row
- Error while accessing sqlite3 shell from django application
- How to display "x days ago" type time using Humanize in Django template?
- Convert unicode to datetime proper strptime format
0π
Create a new URL in the same format and give that name instead of index.
Eg:
url(r'^$', 'index', name='index'),
url(r'^new/page/$', 'new', name='new_page'),
{% url 'new_page' %}
- How do you create a Django HttpRequest object with the META fields populated?
- How to get object from PK inside Django template?
- How does a python web server overcomes GIL
0π
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:
<a href ="{% url 'about' %}">about</a>