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.
Related
I'm hosting my Django app on Heroku and my domain name is registered with Network Solutions. When I enter my domain name, i.e. www.example.com, Heroku displays:
"Not Found
The requested URL / was not found on this server."
I then have to navigate to the template's URL that displays my app's landing page, www.example.com/shipment/.
How can I get my root domain www.example.com to automatically redirect to www.example.com/shipment/, or alternatively, change the URL of /shipment/ to my root domain?
Here's the urls.py for my app:
from django.conf.urls import patterns, url
from shipment import views
urlpatterns = patterns('',
url(r'^$', views.landing, name='landing'),
url(r'^create-subscription/$', views.createSubscription, name='createSubscription'), #
url(r'^(?P<subscription_duration>\d+)/create-account/$', views.createAccount, name='createAccount'),
url(r'^create-account/pay/$', views.pay, name='pay'),
url(r'^create-account/confirm/$', views.confirm, name='confirm'),
)
Here's the urls.py for my project:
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'^$', 'subscription.views.home', name='home'),
# url(r'^subscription/', include('subscription.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'^shipment/', include('shipment.urls', namespace = "shipment")),
url(r'^admin/', include(admin.site.urls)),
)
I changed:
url(r'^shipment/', include('shipment.urls', namespace = "shipment")),
to
url(r'^', include('shipment.urls', namespace = "shipment")),
and that fixed it!
Your problem is this line:
url(r'^shipment/', include('shipment.urls', namespace = "shipment")),
It means "include all these URLs but they must start with "shipment/".
You could add a line to your site's urls.py like:
url(r'^$', generic.RedirectView(url='/shipment/', permanent=False)),
(and then also put a corresponding "from django.views import generic" at the top of your file).
This map your URL / to a redirect view that redirects to /shipment/.
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.
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')), ?
I had correctly configured a Django app and had it running smoothly. However, when I went to enable the administration panel, I encountered a 404 error upon uncommenting this line:
url(r'^admin/', include(admin.site.urls))
Upon trying to access my site, I received this:
Using the URLconf defined in cms.urls, Django tried these URL patterns, in this order:
1. ^admin/
The current URL, , didn't match any of these.
I can't figure out how to get around this error. Does anyone with some Django experience have a solution? Thanks!
EDIT: Here is the entire urls.py file:
from django.conf.urls.defaults 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'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.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)),
)
Have you tried, http://yourdomain:port/admin ? If you are using the default url, I would try
http://127.0.0.1:8000/admin
I'm a Django newbie who needs help: Even though I change some urls in my urls.py I keep on getting the same error message from Django. Here is the relevant line from my settings.py:
ROOT_URLCONF = 'mydjango.urls'
Here is my urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mydjango/', include('mydjango.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
#(r'^admin/doc/', include(django.contrib.admindocs.urls)),
# (r'^polls/', include('mydjango.polls.urls')),
(r'^$', 'mydjango.polls.views.homepage'),
(r'^polls/$', 'mydjango.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mydjango.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mydjango.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mydjango.polls.views.vote'),
(r'^polls/randomTest1/', 'mydjango.polls.views.randomTest1'),
(r'^admin/', include(admin.site.urls)),
)
So I expect that whenever I visit http://mydjango.yafz.org/polls/randomTest1/ the mydjango.polls.views.randomTest1 function should run because in my polls/views.py I have the relevant function:
def randomTest1(request):
# mainText = request.POST['mainText']
return HttpResponse("Default random test")
However I keep on getting the following error message:
Page not found (404)
Request Method: GET
Request URL: http://mydjango.yafz.org/polls/randomTest1
Using the URLconf defined in mydjango.urls, Django tried these URL patterns, in this order:
1. ^$
2. ^polls/$
3. ^polls/(?P<poll_id>\d+)/$
4. ^polls/(?P<poll_id>\d+)/results/$
5. ^polls/(?P<poll_id>\d+)/vote/$
6. ^admin/
7. ^polls/randomTest/$
The current URL, polls/randomTest1, didn't match any of these.
I'm surprised because again and again I check urls.py and there is no
^polls/randomTest/$
in it, but there is
^polls/randomTest1/'
It seems like Django is somehow storing the previous contents of urls.py and I just don't know how to make my latest changes effective.
Any ideas? Why do I keep on seeing some old version of regexes when I try to load that page even though I changed my urls.py?
Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly.