Forums

Issue with path in mysite/urls.py (Django polls app)

Hello, I currently follow the Django polls app tutorial. I've come to the step for adding paths to the urlpatterns:

python_site/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
   path('polls/', include('polls.urls')),
   path('admin/', admin.site.urls),
]

However, I receive this error message when opening the site:

Using the URLconf defined in python_site.urls, Django tried these URL patterns, in this order: polls/ admin/ The empty path didn't match any of these.

In polls/urls.py I have:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

While "path" doesn't work, "url" does:

url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),

I'm a complete beginner in python so I appreciate your understanding and help!

What urls are you hitting?

In the first version, http://erlend.pythonanywhere.com/polls/ works as well as http://erlend.pythonanywhere.com/admin/ and there is nothing to serve just http://erlend.pythonanywhere.com/

In the second one (assuming that you put it into python_site/urls.py) http://erlend.pythonanywhere.com/ works as well as http://erlend.pythonanywhere.com/admin/, but not http://erlend.pythonanywhere.com/polls/

Hi fjl, thank you for the quick response.

I suppose the issue is that http://erlend.pythonanywhere.com/, as you said, serves nothing after I fill python_site/urls.py with the mentioned code ("python_site" is the name of the site):

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
   path('polls/', include('polls.urls')),
   path('admin/', admin.site.urls),
]

I'm just following the instructions on the django tutorial, and the proposed code gives a 404 page not found error for the main site. I refer to the page with a rocket.

Hm, but now I can't make it work with anything. Now, also "url(r'^$', views.index, name='index'), url(r'^admin/', admin.site.urls)," gives page not found.

Well, I got the main url to display the index page of polls:

from django.conf.urls import url
from django.contrib import admin
from polls import views
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
    url('', views.index, name='index'),
]

The content of the pages aren't important at this point, obviously, but I'm just wondering where the page with the rocket is at and what it means.

@fjl You can as well help me out..follow this link https://www.pythonanywhere.com/forums/topic/27485/

@erlend You can use path() to route that index view as well.

@fjl thanks, "path('', views.index, name='index')," worked too. Just curious though, what's the url to that rocket page?

I'm not sure what you mean the page with the rocket...

It says: "The install worked successfully! Congratulations! You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs." For instance this blog article has an image of it.

The rocket page is a special page that appears on / (and possibly all other paths) when you have not defined any URLs. It is generated by Django and I don't think it has a path when you have URLs defined.

I have the identical problem. How do I change the code in the first question above, to have Django tell me I have not defined any URLs per Glenn's May 12 above?

UPDATE: It's not supposed to show the rocket after you create the urls.py file. Dr. Chuck gets us to the rocket page, but then we create the urls.py file, and his video actually shows this debugger page and says that's fine, how it's supposed to be....

How does your urls.py file look like?

UPDATE: It's not supposed to show the rocket after you create the urls.py file. Dr. Chuck gets us to the rocket page, but then we create the urls.py file, and his video actually shows this debugger page and says that's fine, how it's supposed to be.... (This was a tutorial-specific misunderstanding some of us appear to have had.)

It's showing rocket only before you start making changes.

I got the solution for the problem that occurred in my case. The WSGI configuration file should have the code that was shown in the video as I did not change it the problem was there.

The code in video: " " " import os import sys

path = os.path.expanduser('~/django_projects/mysite') if path not in sys.path: sys.path.insert(0, path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.wsgi import get_wsgi_application from django.contrib.staticfiles.handlers import StaticFilesHandler application = StaticFilesHandler(get_wsgi_application()) " " "