Django url doesn't match even though it should - python

In browser I get:
Request URL: http://xxxxxx:8000/person/test/
Using the URLconf defined in person.urls, Django tried these URL patterns, in this order:
^person/ ^$
^person/ ^person/(?P<slug>[-\w]+)/$
^admin/
The current URL, person/test/, didn't match any of these.
In python shell I get:
import re
url = 'person/test/'
pattern = re.compile(r'^person/(?P<slug>[-\w]+)/$'
re.match(pattern,url)
<_sre.SRE_Match object at 0xb7716860>
So it obviously should match the regexp.
Using only url person/ (the ^$ regexp) does work.
I've tried restarting the development server of course. What could be wrong here?

It isn't matching against r'^person/(?P<slug>[-\w]+)/$', the 404 page shows that it's matching against r'^person/person/(?P<slug>[-\w]+)/$'
You've probably matched ^person/ in a urls.py, then imported another urls.py and put "person" in there also. Remove it from the second urls.py. After importing, a secondary urls.py only has to match the rest of the URL, not the whole URL.

Related

page not found 404 django

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/about/ Using the URLconf defined in
Hello.urls, Django tried these URL patterns, in this order:
admin/ admin/ [name='home'] service [name='service'] contact
[name='contact'] about [name='about'] The current path, about/, didn't
match any of these.[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/jZMW9.png
You are getting this warning because you haven't defined a pattern that will direct the user to that page. So if you are doing this in a new application or the root part? Inform us about that. Either way you should go to "urls" and add the path to that folder. You can use the default "admin/" to teach you. For examle delete admin and write "abc/" and when you write"/abc" to your browser it'll show up.

IDE showing me error on objects keyword in " latest_questions = Question.objects.order_by('-pub_date')[:10] "

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/'polls/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>[0-9]+)/$ [name='detail']
^polls/ ^(?P<question_id>[0-9]+)/results$ [name='results']he current path, polls/'polls/index.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Your problem is not about IDE, you need to properly configure your urls.py page. It seems like Django couldn't find 'polls/'polls/index.html' address. You need to go to urls.py file and add following line;
path('polls/'polls/index.html/', views.path_to_function, name='index'),

Passing parameters through django url error

I have been trying to load my localhost:8000/streamers/1234 however there is a bug in my urls that I cannot seem to fix. Iv tried both patterns below and I keep getting the error:
Django tried these URL patterns, in this order:
^streamers/(?P[0-9]+)/$ [name='streamer']
The current path, streamers/34/, didn't match any of these.
urlpatterns = [
#path(r'^streamers/<int:id>/', views.streamer, name='streamer'),
url(r'^streamers/(?P<id>[0-9]+)/$', views.streamer, name='streamer'),
]
if "views.streamer" is a class based view, use:
path(r'^streamers/<int:id>/', views.streamer.as_view(), name='streamer'),
Notice the "as_view()" after views.streamer.

Django how to define default app for home URL?

I'm learning Django, and so far I always had to use URL's like
projectname/appname/viewname
but what if I don't want appname to appear in the URLs for the "default" app, how can I configure my urls so that
projectname/viewname
will load the view viewname from my default app?
P.S. : Of course my primary goal is to be able to use the URL projectname/ to load the default view for the default app.
Details
Currently my ProjectName/urls.py has this:
urlpatterns = patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp1/', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
url(r'^myapp2/', include('myapp2.urls', namespace='myapp2', app_name='myapp2')),
)
so when I deploy my project to Heroku, and visit myproject.heroku.com, I get the error :
Page not found (404)
Request Method: GET
Request URL: https://myproject.herokuapp.com/
Using the URLconf defined in MyProject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^myapp1/
^myapp2/
I know this is supposed to be, but how do I fix (or hack) this to get myproject.heroku.com to work?
If not possible, how can I redirect the homepage to myproject/myapp1/defaultview ?
Thanks in advance !
my app's urls.py looks like this :
urlpatterns = patterns('myapp1.views',
url(r'^view1/$', 'view1', name='view1'), # the default view
url(r'^view2/(?P<oid>\d+)/(?P<pid>\d+)/$', 'view2', name='view2'),
)
Edit
After trying #Wallace 's suggestion url(r'^$', include('myapp1.urls', namespace='myapp1', app_name='myapp1')), and hitting the homepage, I now get the error:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^$ ^view1/$ [name='view1']
^$ ^view2/(?P<oid>\d+)/(?P<pid>\d+)/$ [name='view2']
^myapp2/
Tried changing your project urls.py with:
url(r'', include('myapp1.urls', ...)
This will include all urls from myapp1.urls where they all append to /.
The reason why r'^$' won't work is because the regex ends with $ which means there can only be 1 x url /, and because your app1.urls only has 2 urls defined and without a / or ^$ equivalent, the url resolver will then fail.
But be aware of url clashes, if your project has a ^view1/$ url it will clash to your app1's view1 for example.
Try not including your appname in the regular expression.
url(r'', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),

Django urls.py giving problems

I have an application that is running on Django. Our urls.py had following entries -
url(r'^$', 'web.views.index', name='index'),
url(r'^g$', 'web.views.getpost', name='getpost'),
url(r'^p$', 'web.views.postarticle', name='postarticle'),
It was working fine for thes url patterns. However, I've added two more url patterns -
url(r'^d$', 'web.views.delete', name='delete'),
url(r'^u$', 'web.views.update', name='update'),
And it when I hit
http://127.0.0.1:8000/d
It gives - The current URL, d, didn't match any of these.
I don't know why its not recognizing newly added url patterns. Any idea why its not working?
EDIT: As Wesley mentioned, it shows:
Request URL: http://127.0.0.1:8000/d Django tried these URL patterns, in this order:
^$ [name='index']
^g$ [name='getpost']
^p$ [name='postarticle']
It doesn't show ^d or ^u url patterns.
Here is my urls.py
from django.conf.urls.defaults import patterns, url
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', 'web.views.index', name='index'),
url(r'^g$', 'web.views.getpost', name='getpost'),
url(r'^p$', 'web.views.postarticle', name='postarticle'),
url(r'^d$', 'web.views.delete', name='delete'),
url(r'^u$', 'web.views.update', name='update'),
)
The problem is not in the information you gave us. Both the url pattern and the url you try to visit look ok.
-> What does the debug view exactly return? Does is show:
Request URL: http://127.0.0.1:8000/d
Django tried these URL patterns, in this order:
^ ^$ [name='index']
^ ^g$ [name='getpost']
^ ^p$ [name='postarticle']
^ ^d$ [name='delete']
^ ^u$ [name='update']
Probably the debug view either doesn't list the delete url, it show it with a typo, or the request url is a little different. Try to give us a little more information if you still can't figure out the problem!
As shown in the comments to the question, the problem was a stale .pyc file. This was fixed by deleting the .pyc files and restarting the server.
In order to prevent this issue in future, you should remove .pyc files before starting a dev server. You can use the following command to quickly delete all pyc files in the current directory and all subdirectories (make sure to check that find returns the right files before killing them).
find . -name "*.pyc" #Find all pyc files and list them to console
find . -name "*.pyc" -exec rm '{}' ';' #Run command rm on each file found

Categories

Resources