How to build a url in Django?

Asked

Viewed 694 times

3

I am learning Django, but one thing I have not understood so far is, how to build a url, I know it uses regular expression, but someone there has how to explain to me, or indicate me a material that is good.

1 answer

2


Basically,

it is necessary to define a url, a view and a name for url.

# urls.py

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^expressao_regular/$', 'app.views.minha_view', name="nome_da_url"),
    #  mais urls ...
)

When the page the user is accessing matches one of the urls, Django calls the corresponding view.

To make it easier you can call the url in the templates as follows.

Ex:

<a href="{% url 'nome_da_url' %}">link</a>

There are other interesting topics like nested urls and regular expression patterns that will make your life easier. For more information see the Django documentation on the urls part.

Django Documentation - Urls

I strongly advise you to do the tutorial that is in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.