Django: index.html url cannot be found (Django polls tutorial) - python

I can't seem to get the tutorial at https://docs.djangoproject.com/en/1.6/intro/tutorial03/ to work. I get the following error when trying to access http://127.0.0.1:8000/index.html or http://127.0.0.1:8000/
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, index.html, didn't match any of these.
I have /mysite/urls.py set to:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And index.html is in /polls/templates/polls/index.html

The URLs in your urls.py are the ones you can go to; so either
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/polls/
Provided both those subapps (admin and polls) define a view for the empty string ("^$"), which I believe both of them do.
The fact that one of those views happens to use a template named "index.html" doesn't mean that that filename is also used in URLs.

Related

Django returns Page Not Found for mapped URL

I'm trying to render a basic html/css page in Django and I can't get it to work. It's set up seemingly the same as my index page which does work correctly and the debug explanation from the 404 response seems to show that it's pointing to the right url. Any thoughts on why this isn't working?
*I'm using django 2.1 so I'm using path instead of the old regex url mapping
*The html file exists and is located in templates\base_app\our-story.html
From views.py:
def OurStory(request):
return render(request, 'base_app/our-story.html')
From urls.py:
from base_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('our-story', views.OurStory, name='our-story')
]
From settings.py:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
INSTALLED_APPS = [
(standard django apps)
'base_app'
]
From debug message:
Request Method: GET
Request URL: http://127.0.0.1:8000/our-story.html
Using the URLconf defined in base_app.urls, Django tried these URL
patterns, in this order:
admin/
[name='index']
our-story [name='our-story']
The current path, our-story.html, didn't match any of these.
URL should be - http://127.0.0.1:8000/our-story
We cannot use our-story.html, As we are using framework and already have a route.
Use like this, You will definitely get rid of this error.
I guess path of html template would be only our-story.html If Your project name is base_app
In views.py you should return:
return render(request, template, context)
where
template = 'our-story.html'
and
context = { _your_context_dictionary }
also it is a good practice to use lower case names in the views:
def our_story

Can't see the django home page

I'm new to django, thus the question.
I've a django project set up with two apps both of which seem to work fine. Just that I can't reach the homepage - which should be the default django page. I get the following error,
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
^aggregator/
^bouncer/
The empty path 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.
This is my urls.py
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^aggregator/', include('aggregator.urls')),
url(r'^bouncer/', include('bouncer.urls')),
]
What am I missing here?
you are missing the path to your home directory in the urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^aggregator/', include('aggregator.urls')),
url(r'^bouncer/', include('bouncer.urls')),
url(r'^$', views.yourhomeview),
]

Django Python Page not found (404)

I just started my journey with Django and I can't figure out what I did wrong. Sorry for that simple question.
inz/urls.py
urlpatterns = [
url(r'', include('planner.urls')),
url(r'^admin/', include(admin.site.urls)),]
planner/urls.py
urlpatterns = [
url(r'^$', views.main_page),
url(r'^/student/$', views.student, name='student'),]
And my href in base.html:
Student
And my error:
Request URL: http://127.0.0.1:8000/student/
Using the URLconf defined in inz.urls, Django tried these URL patterns, in this order:
^$
^/student/$ [name='student']
^admin/
The current URL, student/, didn't match any of these.
Remove the leading slash from ^/student/$:
url(r'^student/$', views.student, name='student'),
FYI, in the URL dispatcher docs there is a related example:
There’s no need to add a leading slash, because every URL has that. For example, it’s ^articles, not ^/articles.

debug-toolbar spoiled my urls

I'm a newbie to Django and now I'm trying to make a simple webshop using it. Today I realized that I need some debug tool, so I've chosen django-debug-toolbar. I setup it just like it's explained here - How do I see the Django debug toolbar?. But when I set DEBUG to True I get a message -
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in urlconf, Django tried these URL patterns, in this order:
^__debug__/m/(.*)$
^__debug__/sql_select/$ [name='sql_select']
^__debug__/sql_explain/$ [name='sql_explain']
^__debug__/sql_profile/$ [name='sql_profile']
^__debug__/template_source/$ [name='template_source']
The current URL, , 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.
Here is my app's urls.py:
from django.conf.urls import patterns, url
urlpatterns = patterns('ecommerce.views',
url(r'^profile/$', 'profile'),
url(r'^login/$', 'login_user'),
url(r'^logout/$', 'logout_user'),
url(r'^registration/$', 'registration'),
)
As far as I can see now django looks for appropriate urls not in my app's urls.py, but somewhere else.

404 error in django when visiting / Runserver returns no errors though

When I syncdb and runserver everything works correctly in Django, but when I try to visit the webpage that it is on http://127.0.0.1:8000/ it returns a 404 error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in MyBlog.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, , didn't match any of these.
The strange part is that when I visit /admin on the page it works fine. I dont understand what is failing here. Any help would be awesome!
You need an URL route to the homepage. The urlpatterns variable in MyBlog.urls should have a tuple pair like (r'^$', app.views.show_homepage), where show_homepage is a function defined in views.py. For more info about the URL dispatcher, you can read about it here: https://docs.djangoproject.com/en/dev/topics/http/urls/
Check out chapter 3 of Django's Writing your first Django app tutorial.
In short, you need to specify (in urls.py) which code Django should run for particular URLs; there are no default URLs defined (you'll see a line including the admin URLs in urls.py).
Edit your urls.py so it looks something like
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="home.html"),
)
(you'll also need to create home.html in one of the directories specified in TEMPLATE_DIRS)

Categories