Django - How to redirect the user to the other pages of the application?

2

In the template menu I have a menu Dropdown with the links to the other pages application!

In the home page of the application I have the links to the other pages of the application! Ex: Home, Reports, About ... All are link's that should click on the respective pages.

To address this redirect I have added an onclik function that redirects to the appropriate pages in the <a> tag of my base template.

The dropdown menu code looks like this:

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Serviços <b class="caret"></b></a>
  <ul class="dropdown-menu">
    <li><a href="#" onclick="window.location='http://localhost:8000/cadLivro';">Cadastrar Livros</a></li>
    <li><a href="#" onclick="window.location='http://localhost:8000/cadUsuario';">Cadastrar Usuários</a></li>
    <li><a href="#" onclick="window.location='http://localhost:8000/cadFuncionario';">Cadastrar Funcionários</a></li>
    <li class="divider"></li>
    <li class="dropdown-header">Pesquisa</li>
    <li><a href="#" onclick="window.location='http://localhost:8000/pesqLivro';">Livro</a></li>
    <li><a href="#" onclick="window.location='http://localhost:8000/pesqUsuario';">Usuário</a></li>
    <li><a href="#" onclick="window.location='http://localhost:8000/pesqFuncionario';">Funcionário</a></li>
  </ul>
</li>

This is what I was able to do, since I do not have much knowledge of javascript (much less jQuery), what I did is a "gambiarra" and would only work if I were to run the application locally and at port 8000. How do I do this redirect in a correct way?!

    
asked by anonymous 16.05.2014 / 20:10

2 answers

2

In principle, you could use absolute (or even relative) paths rather than full URLs. That way, the link would lead to a path within your domain, whatever it may be:

/cadLivro/
cadLivro/

If you are in http://localhost:8000/ , these paths would lead to:

http://localhost:8000/cadLivro/
http://localhost:8000/cadLivro/

If you are in http://example.com/projeto/django/ , these paths would lead to:

http://example.com/cadLivro/
http://example.com/projeto/django/cadLivro/

However, a better solution is to use the template tag url to automatically mount the path from the desired view. In addition to complying with the DRY principle (by not repeating URLs in various parts of the system), it ensures that the link will always send to the correct URL, even if in the future you change the urls.py file to serve your views in a different path. So, if your urls.py is like this (just an example, do not do this in practice because it's super inconsistent):

(r'^cadLivro/', view_cadastro_livros, name='cadLivro'),
(r'^cadastro/usuario/', usuarios.views.cadastro_usuarios, name='cadUsuario'),
(r'^funcionario/', include('funcionarios.urls')), # Aqui dentro tem uma view cadFuncionario

In your template you just need to do:

{% url 'cadLivro' %}
{% url 'cadUsuario' %}
{% url 'cadFuncionario' %}

That Django will replace the complete and correct URL.

This can be done anywhere in the template (whether it is generating an HTML or something else, whether the tag is inside an attribute or outside). For example, the simplest way to do what you want (link to another page) is simply to use the URL itself as href (like suggested by Orion in the comments):

<li><a href="{% url 'cadLivro' %}">Cadastrar Livros</a></li>
<li><a href="{% url 'cadUsuario' %}">Cadastrar Usuários</a></li>
<li><a href="{% url 'cadFuncionario' %}">Cadastrar Funcionários</a></li>

But you could also do this in other ways:

<li><a href="#" onclick="window.location='{% url 'cadLivro' %}';">Cadastrar Livros</a></li>

<li><a href="#" onclick="new Window('{% url 'cadLivro' %}', '_blank');">Cadastrar Livros</a></li>

<li><a href="#" onclick="irPara('{% url 'cadLivro' %}');">Cadastrar Livros</a></li>

<li><a href="#" data-url="{% url 'cadLivro' %}">Cadastrar Livros</a></li>
<!-- Usando jQuery por exemplo para fazer algo com o data-url -->

Etc. But if you have no special reason to "complicate", use the simplest form even (ideally, since you are not using JavaScript extensively in your project).

    
28.05.2014 / 02:00
0

If in your urls file, you are using the import path instead of the url, you will need to configure the urls.py file in the directory of your application:

Example I have in the templates folder the \ home.html and contact.html files

--- project_base / urls.py

from django.contrib import admin from django.urls import path from core import views

urlpatterns = [     path ('', views.home, name = 'home'),     path ('contact /', views.contact, name = 'contact'),     path ('admin /', admin.site.urls), ]

--- I have an application called core, where are the templates mentioned

--- within this directory in the urls.py file

from django.contrib import admin

from django.urls import path

from. import views

urlpatterns = [     #path ('', views.index, name = 'index'),     path ('', views.home, name = 'home'),     path ('', views.contact, name = 'contact'), ]

- it is important to highlight the importance of 'names', they will be referenced in the link     
06.09.2018 / 20:02