How to mount a url in Django?

3

To learn Django, but one thing that I have not understood very well is how to set up a url, I know that it uses regular expression, but someone there has a way to explain it to me, p>     

asked by anonymous 28.01.2015 / 20:00

1 answer

2

Basically,

You need to define a url, a 'view' and a url name.

# 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 that the user is accessing corresponds to one of the urls, Django calls the corresponding view.

To facilitate 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 in the urls section.

Django Documentation - Urls

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

    
28.01.2015 / 21:58