Django urls.py giving problems - python

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

Related

The current path, customer, didn't match any of these. even when all other path runs correctly

ERROR :Using the URLconf defined in crm1.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
products/ [name='products']
customer/<str:pk_test>/ [name='customer']
create_order/<str:pk>/ [name='create_order']
update_order/<str:pk>/ [name='update_order']
delete_order/<str:pk>/ [name='delete_order']
The current path, customer, didn't match any of these.
even when i run this.....accounts/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home, name='home'),
path('products/',views.products, name='products'),
path('customer/<str:pk_test>/',views.customer,name='customer'),
path('create_order/<str:pk>/',views.createOrder,name='create_order'),
path('update_order/<str:pk>/',views.updateOrder,name='update_order'),
path('delete_order/<str:pk>/',views.deleteOrder,name='delete_order'),
]
all path are running correctly and even when i run http://127.0.0.1:8000/customer/2
it runs properly....but when i run http://127.0.0.1:8000/customer/ error out
actually
/<str:pk_test>/
kind of path create problem i dont know
Well there is not route for customer only. So, add this line in your urls.py:
path('customer/',views.customer,name='customer-only'),
*Note:- Add the line above the another code. Like:-
path('customer/',views.customer,name='customer-only'),
path('customer/<str:pk_test>/',views.customer,name='customer'),
Okay, as your views.py needs argument, you have to do:
def customer(request, pk_test=None):
customer=Customer.objects.get(id=pk_test)
orders=customer.order_set.all()
order_count =orders.count()
context={'customer':customer,'orders':orders,'order_count':order_count}
return render(request,'accounts/customer.html',context)

RedirectView.as_view not working at the root

In a django 1.8 project, I am attempting to redirect http://myProject/ads.txt to an external url http://a.location.that.has.the.ads.txt.file and thus serve the ads.txt file without using ftp to simply place the ads.txt in the root.
Given this minimal directory structure:
django projects
myProject
myapp
urls.py
views.py
someotherapp
yetanotherapp
myProject
settings.py
urls.py (this is the top urls.py)
views.py
in myProject/myProject/urls.py, (the “top” urls.py) I have as the first entry in the urlpatterns list, the lines:
urlpatterns = patterns('',
(r'^ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
followed by many more pattern matching regex’s. This does not work and fails with a 404. What does work is
urlpatterns = patterns('',
(r'^foo/ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
and then calling http://myProject/foo/ads.txt
Unfortunately, ads.txt files must be placed at the site root. I have tried many different regex’s that test fine in a regex validator, but just don’t work (returns 404). How do I do this without the extra dir “foo”? Any thoughts appreciated. Thank you.
Turns out you cannot redirect with the top level urls.py "routing table" to above the Django project root. A nginx server level redirect did the trick.

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

regular expression issue in django urls.py

I am serving static files in Django locally through their simple server. I have the following line included in my urls.py:
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
(Bonus points to anyone who can tell me why this isn't automatically checked by the url resolver. I have django.contrib.staticfiles install and have DEBUG set to True).
If we take a peek at the implementation of static, we find a really simple function:
if not settings.DEBUG or (prefix and '://' in prefix):
return []
elif not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
return patterns('',
url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
)
the last line ends up getting added to my url patterns.
When I run the web server, it doesn't properly serve files from that STATIC_ROOT. Here is an example:
I type the following into url into my browser: localhost:8000/Users/nick/Documents/coding/sparestub/static_root/contact/js/contact.js
Here is the server response:
Request Method: GET
Request URL: http://localhost:8000/Users/nick/Documents/coding/sparestub/static_root/contact/js/contact.js
Using the URLconf defined in sparestub.urls, Django tried these URL patterns, in this order:
^$ [name='homepage']
^registration/
^Users\/nick\/Documents\/coding\/sparestub\/sparestub\/\.\.\/static_root\/(?P<path>.*)$
Notice that we are searching the STATIC_ROOT correctly. If I copy and paste the url into bash, it finds the directory:
Nick-MacBook-Pro:Documents nick$ cd /Users\/nick\/Documents\/coding\/sparestub\/sparestub\/\.\.\/static_root\/
Nick-MacBook-Pro:static_root nick$ pwd
/Users/nick/Documents/coding/sparestub/static_root
Nick-MacBook-Pro:static_root nick$ cd /Users\/nick\/Documents\/coding\/sparestub\/sparestub\/\.\.\/static_root\/
Nick-MacBook-Pro:static_root nick$ pwd
/Users/nick/Documents/coding/sparestub/static_root
Nick-MacBook-Pro:static_root nick$ cd contact/js
Nick-MacBook-Pro:js nick$ ls
contact.js
And notice that it contains /contact/js/contact.js!!
And seeing at contact/js/contact.js matches the RE pattern (?P.*), I expect this entire thing to work. Can anyone offer some guidance?

Can't find page in django

I'm currently learning Django and I'm trying to add form to register a User.
Now my problem starts really early because I get a 404 whenever I try to access my registration page.
My 3 files are as follows:
views.py : just a hello world to display basically
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
def registration(request):
return HttpResponse("Registration page")
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^$', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
and finally urls.py (in my app) :
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
url(r'^registration/$',views.registration, name='registration'),
)
When I go to my normal index (localhost:8000) It displays the hello world I wrote in the views.py file. However when I try to go the registration page it just returns me a 404 (localhost:8000/registration).
Now as I said I'm still learning this whole thing, but I don't get why It doesn't work properly. As far as i understand the r'^$' in the project file is pointing towards the localhost:8000 and the same regex in the app file tells it to load the index page in this location. Now as the program is still in localhost:8000, a r'^registration/' should be loading the other page in localhost:8000/registration right?
It would be really nice if you could explain to me why this doesn't work and where I did a mistake in my thoughts.
This is the error I get:
When i take
(r'^randomstringhere/', include('myapp.urls')), # index with login/registration
instead of
(r'^$', include('myapp.urls')), # index with login/registration
I can get to the registration page via localhost:8000/randomstringhere/registration. But Site is meant to have a selection where you can either Log in or register on localhost:8000 (or future domain www.randomdomainhere.com) and a registration/login form on localhost:8000/login, localhost:8000/registration (www.randomdomainhere.com/registration etc.)
At the end of each url you should add a $ symbol,
url(r'^registration/$',views.registration, name='registration'),
reffer this https://docs.djangoproject.com/en/1.4/topics/http/urls/
you are missing $ sign in the end of url.
It should be:
url(r'^registration/$',views.registration, name='registration'),
The $ symbol in django urls means end-of-string match character which is borrowed from regular expression. So, $ should be added at the end of your url on app's urls.
url(r'^registration/$',views.registration, name='registration'),
However we can write urls with the regular expressions that don’t have a $ (end-of-string match character) but we do have to include a trailing slash(/). For urls without $, you must enter the trailing slash while opening the url in your browser to match the regular expression.
Your app urls in project's urls.py is
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
So, to access the registration page, you need to browse by
localhost:8000/grappelli/registration/
i.e. append the path in your app's url to the path in project's url for the app.
Learn more about urls here.
You need to remove $ from the first url as given below
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
A url starts with ^ and ends with $. so if $ is encountered django understands that the url has ended and there's nothing more infront of that. so in your case it will no more look for "registration". or if your url with "registration" doesnt matches to any urls specified at all.

Categories

Resources