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

Asked

Viewed 3,857 times

2

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

In the menu of 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 when clicking should direct to the respective pages.

To handle this redirect I added in the tags <a> from my base template an onclik function that redirects to the appropriate pages.

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>

That’s what I was able to do, because I don’t 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 redirect this in a correct way ?!

  • How’s your Javascript code and your urls.py? It is difficult to understand what is your question only with what is being asked, please add more details to your question.

  • In the menu of 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 when clicking should direct to the respective pages. It turns out that as I have not referenced these links in html nor have JS functions to handle. I wonder if in Django I can treat this event by clicking on the links so that by clicking it leads me to the pages!

  • Basically, you can put whatever you want in HTML/Javascript, which pro Django only matters is that the final URL has a corresponding entry in urls.py. If this is done through a direct link, from Ajax, via location.href, new Window, doesn’t matter... In the same way, what your function onclick does not concern Django, it is all on the client side even. If you are having problems, probably is a simple poorly constructed URL, show the code (the relevant section of the template) we may be able to help you better.

  • 1

    Ex.: I saw on your Github link that each link has one href the kind #cadLivros, #cadUsuarios etc. You could at first put a Javascript code that would transform these fragments into complete Urls. But that’s all on the client side, there’s nothing in Django I can help you with that.

  • 1

    rephrased question

  • 1

    Now, yes, I understand what your question is! I voted for the reopening, when that happens put an answer. For now, I leave this link on Soen showing how to do.

  • 1

    I still don’t understand what you’re trying to do, onclick inside a tag <a>? Why not put the direct address in the attribute href?

Show 2 more comments

2 answers

2


In principle, you could use absolute (or even relative) paths instead of complete Urls. In this way, the link would lead to a path within your domain, whatever it is:

/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 url to automatically mount the path from the desired view. In addition to respecting the principle DRY (by not repeating Urls on 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 file urls.py to serve your views in a different way. So, if your urls.py is like this (just an example, do not do so in practice because it is 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 do:

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

Django will replace with the full and correct URL.

This can be done anywhere in the template (whether it generates an HTML or something else, whether the tag is inside an attribute or outside). For example, the simplest way to do what you want (for the link to lead to another page) is simply to use the URL itself as href (as 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 don’t have any special reason to "complicate", use the simplest way (ideally, since you’re not using Javascript extensively in your project).

0

if in your urls file, Voce is using import path instead of url, Voce will need to configure the urls.py file in the directory of your application:

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

---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 cited

---inside 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 remember the importance of the 'Names', they will be referenced in the link

Browser other questions tagged

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