<a class="nav-link active" aria-current="page" href="{% url 'tasks' %}">Tasks</a>
When I'm using this template tag I get an error no ReverseMatch.
but ,
<a class="nav-link active" aria-current="page" href="/tasks">Tasks</a>
When I'm doing this I get no error.
Can someone explain me?
urls.py of the project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('tracker.urls')),
path('tasks/', include('tasks.urls')),
]
urls.py of the tasks app
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name="tasklist"),
]
views.py of the tasks app
from django.shortcuts import render
def tasklist(request):
return render(request, 'tasks/task_list.html')
Related
I have a small Django web app, with multiple applications inside them. I have used the include in the urls.py files, but whenever I reference the URLs in the HTML files they don't load. Below are my 3 urls.py files. The one I'm having an issue will specifically is the nodes url pattern in the nodes urls.py
#main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
]
#account urls.py
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('dashboard/', views.dashboard, name='dashboard'),
path('nodes/', include('nodes.urls')),
]
#nodes urls.py
from django.urls import path
from . import views
app_name = 'nodes'
urlpatterns = [
path('', views.nodes, name='nodes'),
]
This is my HTML file where I am referencing the URL pattern:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'nodes' %}">Nodes</a>
</li>
Due to this pattern name you set in urls file if change HTML file to this it must be fixed:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'account:nodes:nodes' %}">Nodes</a>
</li>
just for tips :) if import urls file at the django shell and print list of urlspatterns you could see default name assign to your nodes path.
Guys i want to open rendered HTML file when i am clicking a image on my HTML page. How i should use it?
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>
main app urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view),
path('o_nas/', views.about_view),
path('', views.home_view),
]
pages app views.py
from django.shortcuts import render
def home_view(reqest):
return render(reqest, "index.html")
def about_view(reqest):
return render(reqest, "about.html")
def contact_view(reqest):
return render(reqest, "contact.html")
First things first: add a name to each route inside pages app urls.py.
pages app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('kontakt/', views.contact_view, name='contact'),
path('o_nas/', views.about_view, name='about'),
path('', views.home_view, name='home'),
]
Inside the HTML Template you can use the url tag in combination with the name of the route to assign the desired target to the href.
HTML code fragment
<header>
<nav class="headlist">
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>
I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
urls.py
from django.conf.urls import include, url
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.conf import settings
from .views import Search
app_name = 'base'
urlpatterns = [
url(r'^index/$', Search.as_view(), name='index'),
url(r'^newresume/$', views.newresume, name='newresume'),
url(r'^profile/(?P<pk>[0-9]+)/$', views.profile, name='profile'),
url(r'^update_info/(?P<pk>[0-9]+)/$', views.update_info, name='update_info'),
]
views.py
def profile(request, pk):
student = get_object_or_404(Students, pk=pk)
return render(request, 'base/profile.html', {'student': student})
_tweet_search.html
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
Console error
Hi all, help me with this error, please. I made a AJAX-request, after this i get an error, how can i fix that ?
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
as you have mentioned app_name in your urls.py file, you need to mention that in your url
I've tried about a thousand different fixes suggested on different questions about this, but nothing seems to be working.
urls.py (app):
from django.contrib.auth.decorators import login_required
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', login_required(views.index), name='index'),
url(r'^clock/', views.clock, name='clock'),
]
urls.py (project):
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from mqtt.views import auth, acl, superuser
urlpatterns = [
url('^accounts/login/', auth_views.login,
{'template_name': 'login.html'}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^auth$', auth),
url(r'^superuser$', superuser),
url(r'^acl$', acl),
url(r'^$', include('lamp.urls')),
]
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render
def index(request):
lamps = request.user.lamp_set.all()
context = {}
if lamps:
device_id = lamps[0].model
context = {'device_id': device_id}
return render(request, 'index.html', context)
def clock(request):
return render(request, 'clock.html')
index.html (snippet):
<div class="toggle-button" id="power"></div>
<div class="toggle-button" id="alarm-clock">
<a href="{% url 'clock' %}" id='clock-link'>Clock</a>
</div>
When I try to load the page I get:
NoReverseMatch at /
Reverse for 'clock' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
I'm pretty new to Django, so any help would be greatly appreciated.
$ means the end of urlconf search for django. you need to remove it to enable the inner-app urls search.
urlpatterns = [
url(r'^', include('lamp.urls')), # <----
#... rest of the urlconfs
]