Including a Django app's url.py is resulting in a 404 - python

I have the following code in the urls.py in mysite project.
/mysite/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/$', include('mysite.gallery.urls')),
)
This results in a 404 page when I try to access a url set in gallery/urls.py.
/mysite/gallery/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/browse/$', 'mysite.gallery.views.browse'),
(r'^gallery/photo/$', 'mysite.gallery.views.photo'),
)
404 error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^gallery/$
The current URL, gallery/browse/, didn't match any of these.
Also, the site is hosted on a media temple (dv) server and using mod_wsgi

Remove the $ from the regex of main urls.py
urlpatterns = patterns('',
(r'^gallery/', include('mysite.gallery.urls')),
)
You don't need gallery in the included Urlconf.
urlpatterns = patterns('',
(r'^browse/$', 'mysite.gallery.views.browse'),
(r'^photo/$', 'mysite.gallery.views.photo'),
)
Read the django docs for more information
Note that the regular expressions in this example don't have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

Related

Django Rest Framework SimpleRouter inclusion inserts ^ into url pattern

I have DRF application with urls defined using SimpleRouter.
# project/app/urls.py:
from rest_framework.routers import SimpleRouter
from .viewsets import ExampleViewset, TopViewset
router = SimpleRouter()
router.register(r"example/", ExampleViewSet, basename="example")
I imported this router to main project urls file.
# project/urls.py:
from project.app.urls import router as app_router
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("app/", include(app_router.urls)),
]
GET localhost:8000/app/example/ returns 404.
Opening localhost:8000/app/example/ in browser returns this error page:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/app/example/
Using the URLconf defined in backend.urls, Django tried these URL patterns, in this order:
admin/
app/ ^example//$ [name='example-list']
app/ ^example//(?P<pk>[^/.]+)/$ [name='example-detail']
The current path, app/example/, 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.
I expected app/example in URLconf, but instead there is app/ ^example. I think ^ means beginning of line. So my question is, why this happened and how to fix it?
I had to remove trailing slashes from registrations:
router.register(r"example", ExampleViewSet, basename="example")
Now correct url is generated.

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.

Django Admin Urls Not Working

Some of my django urls will not work. I can visit my index page and the django admin page just fine. I can log in and create objects no problem. For example, I can create a site, like below, but when I try to visit the collection of sites, I get hit with a 404 (even after creating one). I'm using Django allauth as well, and those pages are also having troubles with my urlconf. Can anyone spot the error?
For example, this url:
Page not found (404)
Request Method: GET
Request URL: http://myapp.com/admin/sites/site/
No reward found matching the query
And this one:
Page not found (404)
Request Method: GET
Request URL: http://shielded-island-1440.herokuapp.com/accounts/profile/
Using the URLconf defined in litherewards.urls, Django tried these URL patterns, in this order:
^ ^$ [name='index']
....
^ ^reward/$ [name='reward_list']
^ ^redeem/(?P<reward_code>)/$ [name='redeem_reward']
^ ^redeem/(?P<slug>\w+)/(?P<reward_code>\w+)/$ [name='reward_confirmation']
....
^account/
^sitemap\.xml$
^admin/
My urlconf is as follows:
from django.conf.urls import patterns, include, url
from myapp.views import RewardSitemap
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
sitemaps = {'rewards':RewardSitemap}
urlpatterns = patterns('',
url('^', include('myapp.urls', namespace="fluffy")),
url(r'^account/', include('allauth.urls')),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
url(r'^admin/', include(admin.site.urls)),
)
The myapp urls included in the first urlconf:
urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name="index"),
....
url(r'^reward/$', RewardsList.as_view(), name="reward_list"),
url(r'^redeem/(?P<reward_code>)/$', RedeemReward.as_view(), name="redeem_reward"),
url(r'^redeem/(?P<slug>\w+)/(?P<reward_code>\w+)/$', RedeemConfirmation.as_view(), name="reward_confirmation"),
)
And finally, my ROOT_URLCONF is set to myapp.urls. The index page works just fine.
From the limited knowledge that i have, i see that the URl pattern has "account/" and you are accessing "http://shielded-island-1440.herokuapp.com/accounts/profile/" which won't match any of the regex patterns in your URLConf.
Can you try modifying to url(r'^accounts/', include('allauth.urls')), ?

Django Not Recognizing URLs

I am getting a 404 error for a few of my URLs in my django project. For example:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Using the URLconf defined in some_project.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^admin/$
But I haven't changed anything to the file.
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'views.index',name='home',),
url(r'^admin/$', include(admin.site.urls)),
)
This problem seemed to come after I installed Django Evolve. Not sure if that has something to do with it.
Your pattern is not correct, it should be:
url(r'^admin/', include(admin.site.urls)),
Your request url is http://127.0.0.1:8000/admin, but your urlpattern is url(r'^admin/$', include(admin.site.urls)).
There is an extra / in the urlpattern.
If you want to include other URLConfs, here's an example to include other URLconfs:
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
# ... snip ...
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^community/', include('django_website.aggregator.urls')),
url(r'^contact/', include('django_website.contact.urls')),
# ... snip ...
)
As Burhan correctly pointed out, you need to remove the trailing $ so that include could work.
The reason is because in regex trailing $ matches right after the last character in the string.
It was a problem with the settings file. There was a merge problem with what I had and another developer had.
The /$ wasn't related to this problem. I added it when I was trouble shooting.

Page not found (404) in django tututorial

I'm working through https://docs.djangoproject.com/en/1.4/intro/tutorial02/ .
after changing the urls.py to
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
I get the following when I start the runserver:
404 error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, , didn't match any of these.
Is there anything obvious that I'm doing wrong?
Thanks in advance,
Bill
You don't have a base url defined. You need something like -
urlpatterns = patterns('',
# ...
url(r'^$', HomeView.as_view())
)
You should be able to see your site at - localhost:8000/admin/ (assuming you're running your dev server with python manage.py runserver).
Django checks all the URL's you've defined in your url conf file and looks for one that matches the url you've entered in the browser. If it finds a URL that matches then it serves up the http response returned by the url's corresponding view (HomeView in the code above). The urls.py file is matching url's to views. Views return the http response.
Looking at the error message you've got (and the code you've included from the url.py file), you can see that there's only one url defined in your app - admin/. Trying to get a page at any other url will fail.
For more information have a look at the docs for django's URL Dispatcher.

Categories

Resources