Django: 404 error in urls.py - django

I have two urls.py files.
In project/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
In project/blog/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('blog.views',
(r'^$', 'index'),
(r'^(?P<slug>[a-z-]+)/$', 'detail'),
(r'^(?P<slug>[a-z-]+)/comment/$', 'comment'),
)
Then I tried to browse these URLs:
http://127.0.0.1:8888/ (404)
http://127.0.0.1:8888/hello-world/ (404)
http://127.0.0.1:8888/admin/ (It worked)
Django version: 1.4 pre-alpha SVN-16985.
Thanks!

To fix the http://127.0.0.1:8888/ error, change your regular expression to:
(r'^$', include('blog.urls')),
Regarding the http://127.0.0.1:8888/hello-world/ error: There could be many things causing this. First, check that you have a blog title that actually returns the slug 'hello-world'.

Related

Routing error: trying to route a defined route in urls.py

I encountered a strange behavior of Django urls. Although forecast/upload url is defined in the urls.py, Django is saying
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/forecast/upload/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
polls/
admin/
forecast/ [name='index']
forecast/ **forecast/upload/** [name='upload']
forecast/ help.html [name='help']
The current path, **forecast/upload/**, didn't match any of these.
Project url.py:
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('forecast/', include('forecast.urls')),
]
Application urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
#url(r'^$', views.home, name="home"),
path('forecast/upload/', views.upload, name="upload"),
path('help.html', views.help, name="help"),
]
You have specified "forecast" twice; once at project level and once in the app. So your URL would be "forecast/forecast/upload".
Presumably, you don't want that, in which case you should remove the "forecast" from the pattern in the app urls.

django tutorial 3 indexpage missing

i am learning django by using django 1.6 documents tutorial 1 - 6.
this round is my 4th try and, previous 3 try was successful and i understand more on every try.
i am in tutorial 3 now, to create views.
according to the documents, after i created a view, i need to map it to a URL.
so i follow the documents to add a urls.py in the polls directory.
and then i follow the document to add include() to mysite/urls.py
i am able to so called wired an index view into the polls view.
now if i go back to localhost:8000, i get an error page,
so my question is
1) WHY?
2) how to get back my localhost:8080 index page co-exist with the polls index page?
thank you very much everyone for your time. i am new and sorry for so simple question.
thank you.
updated:
this is my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
this is my polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
the error msg is:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , didn't match any of these.
http://localhost:8000 index page will be displayed when you created a project (mysite). After creating an application and including it in installed apps in settings.py, you will no longer view the index page of the project, rather your admin page of the application (polls) will be visible at http://localhost:8000/admin
And if you had created any views with the following patterns in polls/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^blog/$', views.blog, name='blog'),
)
You need to visit http://localhost:8000/polls for your index page and http://localhost:8000/polls/blogs for your poll blogs page
Editing my answer for step by step approach:
create index.html at polls/templates/polls directory.
in polls/views.py
from django.shortcuts import render_to_response
def pollindex(request):
return render_to_response('polls/index.html', context_instance=RequestContext(request))
in polls/urls.py
urlpatterns = patterns('',
url(r'^index/$', views.pollsindex, name='index'),
)
in your project/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And now you can view your template index.html at http://localhost:8000/polls/index

No module named django.contrib.auth when using things that redirect

I get the ImportError "No module named django.contrib.auth" both when I try to use the django.shortcuts redirect function and when I try to use:
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
I figure it can't be a coincidence that the only place I'm hitting this error is in places where the page is redirected, but maybe it is. I know that the user isn't actually getting logged out, so the error happens before you even get to any redirect code.
Below is my urls.py file.
import django.contrib.auth.views
from django.conf.urls.defaults import *
import django.contrib.auth
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('karma.views',
(r'^$', 'homepage'),
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r"^opportunities/nearby$", 'draw_map'),
(r'^admin/', include(admin.site.urls)),
url(r'', include('social_auth.urls')),
(r'^profile/', include('karmup.profile.urls')),
)
You are mixing up URL prefixes in your urlpatterns.
urlpatterns = patterns('karma.views',
(r'^$', 'homepage'),
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r"^opportunities/nearby$", 'draw_map'),
)
Django tries to find views relative to the given URL prefix, in your case 'karma.views'. Inside this module, there is no 'django.contrib.auth.views.logout', hence you get the ImportError.
Move the logout URL to a second block, e.g.:
urlpatterns += patterns('',
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
)
That should resolve your issue.

Django bug : ImproperlyConfigured at /databrowse/ . How to solve this bug?

My urls.py of the project looks like this
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib import databrowse
from world.views import welcome
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', welcome),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^databrowse/', include(databrowse.site.root)),
)
urlpatterns += patterns('',
(r'^media/$',include('django.views.static.serve'),{'document_root': settings.MEDIA_ROOT, 'show_indexes': True})
)
What changes have to be done for this to work ?
The only change you have to make is to follow the actual documentation. The documentation doesn't use include for databrowse.

Django cannot find admin.sites.url

I am developing a django site locally using rc1.3 and after making several adjustments to my urls.py file i am receiving error messages that django cannot import admin.site.urls. by urls.py file is below.
urlpatterns = patterns('',
# Example:
(r'^city/$', 'venue.views.city_index'),
(r'^accounts/', include('registration.urls')),
(r'^accountss/', include('registration.backends.simple.urls')),
(r'^profiles/', include('profiles.urls')),
(r'^admin/', include('admin.site.urls')),
(r'^city/(?P<city>[-\w]+)/$', 'venue.views.city_detail'),
(r'^city/(?P<city>[-\w]+)/venue/$', 'venue.views.venue_index'),
)
Any idea what might be cause this, thanks.
http://docs.djangoproject.com/en/dev/intro/tutorial02/#activate-the-admin-site
Don't include the string path, actually import the admin module and point to its urls.
from django.contrib import admin
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
)