Django NoReverseMatch error, 0 patterns tried - python

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
]

Related

I get page not found (404) error when going to "http://127.0.0.1:8000/restaurant/sign-in/" page

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'),
]

Reverse for 'ipads_by_school' with keyword arguments '{'school_id': ''}' not found. 1 pattern(s) tried: ['(?P<school_id>[^/]+)$'] - Django

I'm relatively new to Django/Python.
The line of code from my index.html that generates the NoReverseMatch error in the title is this.
<a href="{% url 'ipads_by_school' school_id=LES %}" class="btn btn-outline-primary m-1" >LES</a>
This is my urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<str:school_id>', views.ipads_by_school, name='ipads_by_school')
]
This is my views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Ipad
def index(request):
return render(request, 'ipads/index.html')
def ipads_by_school(request, school_id):
school_ipads = Ipad.objects.filter(school_id__icontains=school_id)
context = {
'school_ipads': school_ipads
}
return render(request, 'ipads/ipads_by_school.html', context)

NoReverseMatch Reverse for 'my_url' not found

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

django redirects different links to same page

from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request):
return render(request, 'platoweb/index.html')
def about(request):
return render(request, 'platoweb/about.html')
This is the views.py file.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index.html'),
url(r'^$', views.about, name='about.html'),
]
This is the app urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^platoweb/index.html', include('platoweb.urls')),
url(r'^platoweb/about.html', include('platoweb.urls')),
url(r'^admin/', admin.site.urls),
# . url(r'^posts/', include("posts.urls", namespace='posts')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This is the global urls.py
Essentially, I'm trying to hit two different urls, index.html and about.html (and more later). However, when I run it, both index.html and about.html redirect to index.html.
I've tested both html files and they work just fine. Any ideas or thoughts?
First, you need change global urls.py like this:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^platoweb/', include('platoweb.urls')),
url(r'^admin/', admin.site.urls),
# . url(r'^posts/', include("posts.urls", namespace='posts')),
]
Then your app urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about$', views.about, name='about'),
]
Then url http://127.0.0.1:8000/platoweb will go to your index page, and http://127.0.0.1:8000/platoweb/about will goes to your about page.

Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried:

A Django/Python newbie here. I am using python 3.4 and Django 1.7 version.
I am trying to get get my index page with hyperlinks to load and I see this issue.
My index.html (removed unwanted lines)
<form action="{% url 'testapp:search' %}" method="get">
{% csrf_token %}
Morphological
</form>
urls.py
from django.conf.urls import patterns, url
from testapp import views
urlpatterns = patterns('',
#url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^$', 'testapp.views.index', name='index'),
#url(r'^search-form/$', views.IndexView.as_view(), name='index'),
url(r'^search-form/$', 'testapp.views.index', name='index'),
#url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#url(r'^option=(?P<option>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^detail/(?P<val>\w+)/?$', 'testapp.views.detail', name='detail'),
#url(r'^search/$', views.search, name='search'),
url(r'^search/$', views.search, name='search'),
)
views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render_to_response
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
#from polls.models import Question, Choice
from testapp.models import Molecular, Morphological
#from django.template import RequestContext, loader
from django.http import Http404
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.views import generic
def index(request):
return render_to_response('testapp/index.html', context_instance=RequestContext(request))
def search(request):
if 'q' in request.GET:
message = 'You searched for: %r' % request.GET['q']
else:
message = 'You submitted an empty form.'
return HttpResponse(message)
def detail(request, val):
message = 'hello form.'
return HttpResponse(message)
#class DetailView(generic.DetailView):
# model = Morphological
# template_name = 'testapp/detail.html'
I am trying to pass a parameter if the link is clicked and use that parameter to query the DB. I tried various things, & at this point I am stuck. I want a string and not the primary key, which I used in the standard polls application. Is it just my regular expression wrong?
Any help in fixing this would be of great help. Thanks.
Put single-quotes around the argument in the URL:
Morphological

Categories