Django 1.10: The current URL didn't match any of these - python

mysite/urls.py
from django.conf.urls import url,include
from django.contrib import admin
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('blog.urls')),
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout,
name='logout', kwargs={'next_page': '/'}),
]
blog/urls.py
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$',views.PostListView.as_view(),name='post_list'),
url(r'^about/$',views.AboutView.as_view(),name='about'),
url(r'^post/(?P<pk>\d+)/$', views.PostDetailView.as_view()
,name='post_detail'),
url(r'^post/new/$', views.CreatePostView.as_view(), name='post_new'),
url(r'^post/(?P<pk>\d+)/edit/$', views.PostUpdateView.as_view()
, name='post_edit'),
url(r'^drafts/$', views.DraftListView.as_view()
, name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/remove/$', views.PostDeleteView.as_view()
,name='post_remove'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish
, name='post_publish'),
url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post
, name='add_comment_to_post'),
url(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve
, name='comment_approve'),
url(r'^comment/(?P<pk>\d+)/remove/$', views.comment_remove
, name='comment_remove'),
]
The error occurs when I try to edit, remove the post, add a comment or remove comment from the post
I can't access any url from blog/urls.py
and I don't know where the error is coming from.
Error

In your template, you have a link where you have missed out the % in the URL tag. You have something like,
{ url 'post_detail' pk=post.pk }
but it should be:
{% url 'post_detail' pk=post.pk %}
When you click on the invalid link, it takes you to drafts/{url 'post_detail' pk=post.pk}, so you get the 404 error.

Related

Having difficulties getting navigation to work in Django

I am trying to get my navigation to work in Django
This is what my about.html looks like
<ul class="dropdown-menu">
<li class="nav-item"><a class="nav-link" href="{% url 'courses' %}">Courses</a></li>
urls.py
from django.contrib import admin
from catalog import views
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('', views.about, name='about'),
path('', views.courses, name='courses')
]
and views.py
from django.shortcuts import render
#home page view
def index(request):
return render(request, "index.html")
#about page
def about(request):
return render(request,"about-us.html")
#courses page
def courses(request):
return render(courses, "courses.html")
As suggested by one of the answers, I made these changes to my urls.py file
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('courses/', views.courses, name='courses')
]
I am now getting this error message
AttributeError at /courses/
'function' object has no attribute 'META'
Request Method:
GET
Request URL:
http://127.0.0.1:8000/courses/
Django Version:
2.2.7
Exception Type:
AttributeError
Exception Value:
'function' object has no attribute 'META'
Exception Location:
C:\Users\User\Anaconda3\lib\site-packages\django\template\context_processors.py in debug, line 40
Python Executable:
C:\Users\User\Anaconda3\python.exe
Python Version:
3.7.4
Just update your urls.py to the following:
from django.contrib import admin
from catalog import views
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('courses/', views.courses, name='courses')
]

Using multiple slugs in urls.py

I'm trying to use two slugs in my urls but I keep getting:
Reverse for 'tithe' with arguments '(2018, 'February')' not found.
1 pattern(s) tried: ['tithe/(?P<year>[0-9]{4})-(?P<month>[\\w-])/$']
urls.py
from django.contrib import admin
from django.urls import path,include,re_path
from django.conf.urls import url
from tithe import views
from django.views.generic import RedirectView
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name="account_login"), name="index"),
path('accounts/', include('allauth.urls')),
path('dashboard', views.Dashboard.as_view(), name='dashboard'),
url(r'^tithe/(?P<year>[0-9]{4})/(?P<month>[\w-]+)/$', views.TitheView.as_view(), name='tithe'),
]
dashboard.html
<i class="zmdi zmdi-format-underlined"></i> <span> Tithe </span>
you can change urlpatterns like this:
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name="account_login"), name="index"),
path('accounts/', include('allauth.urls')),
path('dashboard', views.Dashboard.as_view(), name='dashboard'),
path('tithe/2018/February/', views.TitheView.as_view()),
url(r'^tithe/(?P<year>[0-9]{4})/(?P<month>[\w-]+)/$', views.TitheView.as_view(), name='tithe'),

view function not found in django

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.

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.

Django NoReverseMatch error, 0 patterns tried

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
]

Categories