14

I'm a newbie in Django and just started looking at it before a day by installing Django 1.10 on my local.

I've followed all the instructions of this link https://docs.djangoproject.com/en/dev/intro/tutorial01/. However I'm continuously getting this error:

Page not found (404) Request Method:    GET Request URL:    http://127.0.0.1:8000/polls/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

    ^admin/

The current URL, polls/, didn't match any of these.

I've created polls app to getting started.

To make a start, I went with view.py, here is the code:

polls/views.py

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello World!")

Here is my code for urls.py:

polls/urls.py

from django.conf.urls import patterns, url

from . import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)

And here is the urls.py in root:

mysite/urls.py

from django.conf.urls import patterns,include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I've spent good piece of time to find out the solution, found that this question has been asked for many times,so tried with those solutions as well but none of them worked for me.

I can't find out what I'm missing here so please draw my attention to the gap.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Dhaval
  • 901
  • 3
  • 8
  • 26

10 Answers10

29

I think you have edited the wrong file when trying to change the root url config.

Make sure you are editing the root url config in mysite/mysite/urls.py (the directory containing settings.py) not mysite/urls.py (the directory containing manage.py).

As general advice, install the latest release, currently 1.9. Don't use 1.10, which is under development. Make sure that you are following the tutorial for 1.9, because the tutorial changes for different versions. For example, your mysite/urls.py doesn't match the tutorial for 1.9, as the urlpatterns should be:

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]
Alasdair
  • 298,606
  • 55
  • 578
  • 516
7

In settings.py you have a setting name INSTALLED_APPS-

Adds you app i.e. polls to it.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ....
    'polls',
]
utkbansal
  • 2,787
  • 2
  • 26
  • 47
  • 1
    [Installed apps](https://docs.djangoproject.com/en/1.9/ref/settings/#installed-apps) is now a list instead of a tuple :) – Sayse Dec 11 '15 at 10:20
1

It's working. Go to your url bar and type your app name:

http://127.0.0.1:8000/home

My app name is home. It will work.

If you want to set your app as your default page then import views from your app.

Like

from home import views

then write

url(r'^$', views.index),

inside.

It will set the views as your default page

http://127.0.0.1:8000/

When you type this it will redirect to your views.

Draken
  • 3,134
  • 13
  • 34
  • 54
1

I had the same problem as described. Running a Windows machine.

It turned out, that the virtual environment I was using had not been configured properly (or maybe I had forgot to activate it before installing Django) and the interpreter and django-admin were fetched from the wrong path by CMD.EXE.

If it appears as if Django is "ignoring" your urls.py - try deleting everything, re-creating the virtual environment, activating it and re-installing Django afterwards.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
ru13r
  • 194
  • 8
1

change the mysite/url

    from django.conf.urls import patterns,include, url
    from django.contrib import admin

    urlpatterns = patterns('',
    url(r'^&', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
    )

Then run your server and visit 127.0.0.1/8000. This should take you to the index of your website.

or you leave your code as it is and run 127.0.0.1/8000/polls on your browser

Bello Tomi
  • 513
  • 6
  • 9
1

Make sure you have path("admin/", admin.site.urls) in your main url settings.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 16 '22 at 21:29
0

I too had same problem going through the official docs tutorial. I was using cloud 9. What I realised before I was able to solve this problem was that while creating the workspace I already chose django(ie by the time my workspace was created, django had already been installed) And going through the tutorial, I again executed $ django-admin startproject mysite thereby having another layer of django with multiple directories and unavoidably the confusion. My solution was to delete everything and start all over.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
0

if @Alasdair answer does not work, and it seems your working on correct files, just restart your server

0

The path in Django 2.2 does not support regular expression , I was using, path('(?P<id>\d+)/share/', views.mail_send_view) , so getting error, now changed to this, path('<int:id>/share/', views.mail_send_view). Now not getting any error. For more info please follow django's official documentation.

-1

Checklist:

  1. Make sure to run the server
>>python manage.py runserver

Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 01, 2020 - 15:04:46
Django version 3.0.2, using settings 'corona.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

  1. Open the browser with cmd running, http://127.0.0.1:8000/polls should be added to the url.
Nimantha
  • 6,405
  • 6
  • 28
  • 69