When trying to click on a button to redirect to getonboard.html I get the following error:
django.urls.exceptions.NoReverseMatch: Reverse for 'getonboard' not found. 'getonboard' is not a valid view function or pattern name.
artdb/urls.py:
path('getonboard/',views.getonboard,name='getonboard'),
templates/artdb/base.html:
<p><a class="btn btn-primary btn-lg" href="{% url 'getonboard' %}" role="button">Get onboard »</a></p>
artdb/views.py:
def getonboard(request):
return render(request,'artdb/getonboard.html')
templates/artdb/getonboard.html:
{% extends "artdb/base.html" %}
{% block content %}
<h2>getonboard template</h2>
<p>this is the getonboard text</p>
{% endblock content %}
full main urls.py:
"""winmalist URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('artdb/', include('artdb.urls')),
path('admin/', admin.site.urls),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
full artdb/urls.py:
from django.urls import path
from django.views.generic import TemplateView
from artdb.views import PersonList
from . import views
app_name='artdb'
urlpatterns = [
# path('base/',views.base,name='base.html'),
path('getonboard/',views.getonboard,name='getonboard'),
path('index/',views.index,name='index'),
path('persons/',PersonList.as_view()),
path('',TemplateView.as_view(template_name='artdb/base.html')),
path('test2',TemplateView.as_view(template_name='artdb/test2.html')),
# path('', views.IndexView.as_view(), name='index'),
# path('contract', views.contract, name='contract'),
# path('<int:person_id>/test1/', views.test1, name='test1'),
]
any thoughts?
When you include urlpatterns from a different app, you'll usually namespace those URLs by adding the line app_name = 'artdb' to the urls.py file inside the app. So, if the main urls.py file has the line:
urlpatterns = [
path('artdb/', include('artdb.urls')),
...]
And the artdb/urls.py file has these lines:
app_name = 'artdb'
urlpatterns = [
path('getonboard/', views.getonboard, name='getonboard'),
...]
then you can reverse this URL by namespacing the name:
reverse('artdb:getonboard')
This allows you to use the same name in multiple apps (e.g. index would be common), having artdb:index and user:index for example.
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 %}
When I go to "http://127.0.0.1:8000/restaurant/sign-in/" I get page not found (404) error. But I can go to "http://127.0.0.1:8000/restaurant/$" to access the home page.
I also tried "http://127.0.0.1:8000/restaurant/sign-in/$" but this also gives me error (init() takes 1 positional argument but 2 were given).
My urls.py is
from django.contrib import admin
from django.urls import path
from foodtaskerapp import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('restaurant/sign-in/$', auth_views.LoginView,
{'template_name': 'restaurant/sign_in.html'},
name='restaurant-sign-in'),
path('restaurant/sign-out', auth_views.LogoutView,
{'next_page': '/'},
name='restaurant-sign-out'),
path('restaurant/$', views.restaurant_home, name='restaurant-
home'),
]
And my views.py is
from django.shortcuts import render, redirect
def home(request):
return redirect(restaurant_home)
def restaurant_home(request):
return render(request, 'restaurant/home.html', {})
here is the screenshot of the error
I also have
<body>
<form method="POST">
{% csrf_token %}
{{ form }}
<button type="submit">Sign In</button>
</form>
</body>
in sign_in.html but the form don't show up, only Sign In is shown.
only sign is shown but not the form
the syntax for the function you are using has changed in the latest version of django, which is why you got that error (the code you are sharing is in a tutorial made by code4startups which uses an older version of django).
You should modify your path command to:
path('restaurant/sign-in/', auth_views.LoginView.as_view(
template_name='restaurant/sign_in.html'),
name='restaurant-sign-in'),
You have no corresponding function in views.py:
auth_views.LoginView
Also, i guess, you neither have 'restaurant/sign_in.html', so it doesn't redirect to the page.
Add this in views.py:
def restaurant_signIn(request):
return render(request, 'restaurant/sign_in.html')
And corresponding HTML page name: 'sign_in.html',in restaurant directory:
<p>SigninWorks</p>
Your urls.py must look like:
from django.contrib import admin
from django.urls import path
from foodtaskerapp import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('restaurant/sign-in/', views.restaurant_signIn,
name='restaurant-sign-in'),
path('restaurant/$', views.restaurant_home, name='restaurant-
home'),
]
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
]