I am starting a project in django, it loads fine when starting, the error arises when I create a url and a hyperlink to redirect to it and it remains in the start url, that is, it does not redirect.
Structure of my Project:
carshop
carshop
-settings.py
-urls.py
---
--
principal
-urls.py
-models.py
-views.py
...
...
static
templates
in the carshop urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from principal.views import Principal
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('principal.urls', namespace='principal')),
]
urls.py of main:
from django.conf.urls import url
from principal.views import Principal, MenuAdministrador
urlpatterns = [
url(r'', Principal, name='vista_principal'),
url(r'^controlpanel', MenuAdministrador, name='controlpanel'),
]
in views.py of main:
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from django.template import loader
def Principal(request):
template = loader.get_template('principal/index.html')
context = {
}
return HttpResponse(template.render(context, request))
def MenuAdministrador(request):
template = loader.get_template('administrador/index.html')
context = {
}
return HttpResponse(template.render(context, request))
In my template where I invoke I have something like this:
<a class="nav-link" href="{% url 'principal:controlpanel' %}" >Administrador</a>
By default I make it load a template with shopping cart type images, which it does well, the problem arises when selecting the link Administrator does not redirect to it, the initial page is kept static.
Maybe someone could tell me what the error is. Thanks in advance.
URLs in Django are regular expressions. For them to work, you need to add a start and end to the string that represents that URL. For this use
^
and$
respectively: