Django urls page not found - django

In my urls.py I include my urls for each app, for example;
from app.insight import urls as insight_urls
urlpatterns = [
path('insight/', include(insight_urls), name='insight')
]
In insight.urls I include the view and the url like this:
urlpatterns = [
path('create/', InsightAddView.as_view(), name='create-insight')
]
The error I get says:
Page not found. No insight found matching the query
However, when I change the create-insight url from create/ to create/new/ it works.
Why doesn't the create/ url work?

Thanks to #cagrias I solved the problem. I had another url matching the create/ url. This one:
path('<slug:slug>/', InsightView.as_view(), name='read-insight-slug')
I solved it by changing that one to:
path('read/<slug:slug>/', InsightView.as_view(), name='read-insight-slug'),

Related

404 error issue with Path() pattern

path('add', views.TopicCreate.as_view(), name='add-topic')
]
When i try to run it (by entering http://127.0.0.1:8000/home/add), it gives me 404 error saying it didn't match any of the patterns. even though it says that there's matching url pattern right there.
home/ [name='index']
home/ <int:topic_id> [name='details']
home/ add [name='topic-add']
admin/
The current path, home/add/, didn't match any of these.
The weird thing is, the link works when i try it in Mozilla and Google Incognito. Doesn't work on IE (private window or not) and normal Chrome.
full urls.py :
app_name = "home"
urlpatterns = [
path('', views.index, name="index"),
path('<int:topic_id>/', views.detail, name='detail'),
path('add', views.TopicCreate.as_view(), name='topic-add')
]
I think you have APPEND_SLASH set to False in your settings or you don't have CommonMiddleware in your middlewares.
The reason behind the ERROR 404 is that your url does not end in a slash home/add but you are visiting the path with a slash home/add/. This will not raise an HTTP_ERROR if you have included CommomMiddleware, or set APPEND_SLASH to True in your settings.
Bear in mind that this will have some effects like it will issue a redirect, etc. Please see the documentation in the link above for more information.
If you don't want to set APPEND_SLASH, you can update your urls and add a slash at the end:
path('add/', views.TopicCreate.as_view(), name='add-topic')

Basic URL Django Issue

This basic url issue is giving me a headache. I have two links I wish to take a fictitious user to: localhost/prediction and localhost/prediction/. The first works, the second doesn't with an error saying that the site doesn't exist. Any clue as to why?
URl Patterns:
urlpatterns = [
# ex: /prediction/
url(r'^', views.post_list),
# ex: /prediction/<number>/
url(r'^(?P<number>[0-9]+)/$', views.show_body)
]
View:
def show_body(request, number):
return HttpResponse(number)
You should change your pattern from
url(r'^', views.post_list),
to
url(r'^$', views.post_list),
There's no need to add a leading slash, because every URL has that. See the example in the Django docs.

Build-in views?

I installed a module to Django and this module has ceertain views within itself. Also it comes with a demo to see the features. I am looking at the code from the demo for a while now but I cant figure out how a template is rendered over the build in views.
The module i'm working with is swingtime:
Here is an example:
Excerpt from the template:
<a class="plain" href="{% url 'swingtime-daily-view' prev_day.year prev_day.month prev_day.day %}">
URL Directory
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='intro.html'), name='demo-home'),
url(r'^karate/', include('karate.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^docs/?$', RedirectView.as_view(url='/docs/index.html', permanent=True)),
url(r'^docs/(?P<path>.*)$', serve, dict(document_root=doc_root, show_indexes=False))
]
So the url must somehow link to a built-in template i suppose?
When I try to implement similar code in my project the url can not be reversed?
And also: How would one edit the template for such a build in view?
Thank you!
Check the urls.py file in the karate module. you will find the url there.
It will be something like
urlpatterns = [
...
url(r'daily-view', view='view_name' name='swingtime-daily-view'),
...
]
Now the name argument of the url is the important thing you're looking for as I don't know the actual path or view set there.
So when you have something like this
<a class="plain" href="{% url 'swingtime-daily-view' prev_day.year prev_day.month prev_day.day %}">
The url dispatcher will look through the app urls for the url with this view....this is called named url patterns.
Read more on how url patterns work URL DISPATCHER IN DJANGO

How to expand error in Django dev server?

I have trouble figuring out what trailing slash that causing an error in Django. This is the error:
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'.
Remove this slash as it is unnecessary.
My urls.py looks like this:
urlpatterns += [
#url('^$', direct_to_template, {'template': 'index.html'}, name='home'),
url('^$', mezzanine.blog.views.blog_post_list, name='home'),
url('^', include('main.urls')),
url('^', include('mezzanine.urls')),
]
And in my main.urls it doesn't exist any '/' before. How can I troubleshoot where the problem exists? Can I expand the error when I run the dev server with manage.py?
Edit:
This is my main/urls.py:
from django.conf.urls import patterns, include, url
from . import views
app_name = 'main'
urlpatterns = [
# Create Blog
url(r'^create-blog/$', views.create_blog, name='create_blog'),
# Lists
url(r'^blogs/$', views.blog_list, name='blog_list'),
url(r'^blogs/new/$', views.blogs_new, name='blogs_new'),
url(r'^top/$', views.top_lists, name='top_lists'),
url(r'^latest/$', views.latest_posts, name='latest_posts'),
# Tags
url(r'^tags/(?P<tag>[\w-]+)/$', views.tag, name='tag'),
url(r'^tags/$', views.tags, name='tags'),
# Avatars
url(r'^avatar/', include('avatar.urls')),
# Various
url(r'^welcome/$', views.welcome, name='welcome'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^contact-us/$', views.contact, name='contact'),
]
Looking over the question again, perhaps you have literally defined a pattern as ^/ somewhere.
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
This system check by django is printing out the pattern that is causing problems. I made a quick django example, and when I put in a url such as...
urlpatterns = [
url('^test1/$', views.test1, name='test1'),
url('^/test2/$', views.test2, name='test2'),
]
?: (urls.W002) Your URL pattern '^/test2/$' [name='test2'] has a regex beginning with a '/'. Remove this slash as it is unnecessary.
As you can see it is giving me my URL pattern exactly as it is defined, ^/test2/$. I think your problem is likely in some other part of your urls.py file or one of the other urls.py files, most likely an include because you do not have the trailing $ in the pattern nor a name for the view.
Try searching through your project for a url defined as '^/' is my recommendation.

Django doesn't see view

In my urls.py I have the followwing:
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'welcome_page.home', name='home'),
#url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^welcome_page/$', include('OnePK_Web.views.welcome_page')),
)
Where views is a package with module welcome_page
When I am trying to run I have the following : The included urlconf doesn't have any patterns in it.
I don't understand how to fix it. Any Ideas?
If you are referring to an app view through your "PROJECT URLs" refer to following syntax:
url(r'^welcome_page/$','appname.views.viewnameforthisurl',name='viewnameforthisurl'),
If you are reffering to views corresponding to your "APP URLs" through your "PROJECT URLs" refer to following syntax:
url(r'^welcome_page/$',include('appname.urls')),
else if your struck in the app urls, Try reading djangoproject documentation. For every django related problem, you will find an appropriate and logical way out
Remove $ from the following pattern if OnePK_Web.views.welcome_page is app name:
url(r'^welcome_page/$', include('OnePK_Web.views.welcome_page')),
# ^
If OnePK_Web.views.welcome_page is a view name, remove include(..):
url(r'^welcome_page/$', 'OnePK_Web.views.welcome_page')