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')
Related
I've got a site I'm working on where I need to match a parameter at the start
of the url:
test.com/the-data-slug
What is odd is I can't figure out why the following code dosen't work.
It dosen't match at all.
path('<slug:data-slug>/', DataLandingView.as_view(), name="data_landing")
I did get the following to work, but I know path is prefered over re_path.
re_path(r'^(?P<data-slug>\w+)/$', DataLandingView.as_view(), name="data_landing")
I'm really curious what I'm doing wrong with the path call, or is this just something path can't do?
Update:
Requested from commenters.
From urls.py these are the only url patterns in use.
urlpatterns = [
# Special Project level routing. Specifically routes to handle login/logout admin
path('admin/', admin.site.urls),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
# We are handling all convention level routing here as it makes more
# sence here as a master control router than in the individual projects
# Global Stuff for buisness
path('', HomePageView.as_view(), name='home'),
path('about/', AboutPageView.as_view(), name='about'),
# If we don't recoginze from the above we step down to the next layer
path('<slug:data-slug>', CLandingView.as_view(), name="c_landing")
]
Going to http://127.0.0.1:8000/abc-2020
and http://127.0.0.1:8000/abc-2020/
returns a 'Page Not Found' Exception
But if I use the following route with re_path. It works.
re_path(r'^(?P<c_name>[-\w]+)/$', CLandingView.as_view(), name="c_landing")
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.
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.
Hi thanks for looking into this.
I have been following Django's tutorial on URLs and got a bit confused/stuck on this part:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs
what I do not understand is if, say, on page mypage.com I provide only 2 possible URLs for mypage.com/polls and mypage.com/admin, what happens if the user goes to mypage.com? Obviously, I thought, the user will need to see some sort of 'welcome' page so I decided to add another URL to that urls.py:
urlpatterns = patterns('',
url(r'^/', 'myapp.views.welcome'), #when it's just mysite.com
url(r'^myapp/', include('myapp.urls')), #includes everything with mysite.com/myapp/...
url(r'^admin/', include(admin.site.urls)),
)
But then I get redirected to that welcome view from whichever page, whether i go to /myapp or not. So, I decided to create a views.py file outside myapps folder and put that welcome page there, and it seems to have worked, apart from that I get a 404.
I am so confused! Could you explain in lamers' terms please
Thanks,
blargie-bla
It should be
url(r'^$', 'myapp.views.welcome')
otherwise any URL will match the pattern. Django will call the view for the first pattern in urlpatterns that matches, so you need to be specific and include the end-of-the-line character ($) into the pattern.
I'm working with Django, admittedly for the first time doing anything real.
The URL config looks like the following:
urlpatterns = patterns('my_site.core_prototype.views',
(r'^newpost/$', 'newPost'),
(r'^$', 'NewPostAndDisplayList'), # capture nothing...
#more here... - perhaps the perma-links?
)
This is in an app's url.py which is loaded from the project's url.py via:
urlpatterns = patterns('',
# only app for now.
(r'^$', include('my_site.core_prototype.urls')),
)
The problem is, when I receive a 404 attempting to utilize newpost, the error page only shows the ^$ -- it seems to ignore the newpost pattern...
I'm sure the solution is probably stupid-simple but right now I'm missing it. Can someone help get me on the right track...
Your pattern for the include will only match an empty URL string, change it to a prefix which should be mapped to the included urls, or remove the $ from that pattern.