Django HomePage Down

I completed all of the steps of the polls tutorial but I am running into one final issue. I tried running the python manage.py runserver command and when I did so the home page would not load. I read the error message and it said that it could not find a url pattern. The thing is, I have the /polls with the include statement in the first line and in the polls urls I see the empty path listed. I wonder why it is not connecting. Can anyone help me understand why this is happening and how to fix it?

I cannot have the python manage.py runserver command not have a page for if you just open it. I need some adjustment to be made.

github link

mysite urls.py

polls urls.py

what steps have I taken to solve the problem?

I have looked at the documentation, I have searched the github repository code for the final solution, and I have attempted to make an empty path for the mysite urls.py in my project root folder. (edited)

mysite/urls.py

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

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

polls/urls.py

from django.urls import path

from . import views

app_name = "polls"
urlpatterns = [
    path("/", views.IndexView.as_view(), name="index"),
    path("<int:pk>/", views.DetailView.as_view(), name="detail"), # this was changed 
    path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

Always remember to post the actual error message received.

What URL did you enter?

What is the content of your root urls.py file? (The urls.py file referred to by your settings.py file.)

Error message 1 upon running python manage.py runserver

Traceback (most recent call last):
File “/Users/andrewstribling/Desktop/mysite-main/manage.py”, line 22, in
main()
File “/Users/andrewstribling/Desktop/mysite-main/manage.py”, line 13, in main
raise ImportError(
ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(project_env) andrewstribling@Andrews-MacBook-Pro mysite-main %

this error is the first that I have ever seen of it. I ran python manage.py runserver and I had just copied and pasted the url listed in the terminal. The local address. no / or anytihng. I believe that the empty quotes string ‘’ is supposed to take the user to the home page.

for some reason my virtual enviornment folders were gone. I had recreate a folder run pip install django, pip install psycopg[binary] and so on.

Please do not post images of code here - copy paste the code into the body of your message.

I don’t see an “empty” path entry in the urlpatterns you posted.

I have an include ‘polls.urls’ which I belive links my url conf with the polls url conf.

Inside of polls.urls I have an empty path. But for some reason it does not link up.

Based on your prior answer is it possible I need to set up a url for an empty path? and if so do I need an html page or something for it?

We’ve had the discussion before about the include function and how it works. You may wish to review the complete discussion at Understanding Django include() to refresh your memory about what’s happening there.

Only if you want your project to respond to an empty url.

It’s a url just like any other. There’s nothing special, different, or unique about it.

Thank you for your response.

Yes I remember the prior conversation it says that one of the abilities of include is that it can link the urls together. ← Thank you for answering that question by the way.

Which leads me to my confusion. If I have an empty url in my polls app and the root folder has a include statement for the polls urls, than by my reasoning the root folder should see the include statement and than see the empty ‘’ url in my polls urls. but for some reason, it will not see it.
Why is this?

If that’s the interpretation that you’re coming up with from the previous discussion, then I think you need to reread it again.

Specifically, the last couple of messages where we go through the specifics of how a URL is processed.

basically a url is processed by taking in a request from a user who sent it through a browser. django will look at the url path match up the url to one of the urls in the url conf and than strip off matching part and continue down the selected path using the url information.

Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

A request comes in from the browser. The browser may be submitting a request for a url like /a/b/c/.

If your path statement is path('a/', include(b_app.urls)), then Django is going to match the submitted /a/b/c/ to this path. It’s then going to drop the a/ from the request path and continue processing by searching the urlpatterns in b_app.urls with b/c/.

→ so basically it chops off ‘a/’ and than it goes into b_app.urls → which I perceive is a urls file for an app.

perhaps link is not the correct word, perhaps it redirects the search to the ‘b_app.urls’ which is a urls.py file in an app and it searches through the urls listed in the url conf with the url information that the user sent from the browser to find out which view to use. Am I getting closer to the correct interpretation?

If so, It seems that the include did not link the paths together and I need to make an empty ’ ’ url path for my project where it will show something if the user submits an empty string because an error page would not be the first thing I want a recruiter to see when they run python manage.py runserver.

Yes. Which means in that example, that you must have a/ as part of the url before it will strip that off to check the rest of the url in b_app.urls.

I have one more question its this import error that I am getting.

ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

If your virtual environment isn’t active, activate it.

If your virtual environment is active, then pip list will show you the packages that are installed.

Check to see if Django is installed.

asgiref 3.7.2
Django 4.2.3
django-debug-toolbar 4.1.0 /Users/andrewstribling/mysite/project_env/src/django-debug-toolbar
pip 23.1.2
psycopg 3.1.9
psycopg-binary 3.1.9
python-dotenv 1.0.0
setuptools 65.5.0
sqlparse 0.4.4
typing_extensions 4.7.1
(project_env) Andrews-MBP:mysite andrewstribling$

I ran it and it works so from this point forwards I need to utilize pip list to see what my project environment has and does not have.

Thank you very much for taking the time out of the day to help a very frustrated rookie.

Err one more question, the solution code for the tutorials app does not appear to have a ‘’ in the mysite urls. IT does have an include polls.urls.

The solution code for the tutorials app

solution code:

# solution code from the polls app urls.py 

from django.urls import path

from . import views

app_name = "polls"
urlpatterns = [
    path('', views.IndexView.as_view(), name="index"),
    path("<int:pk>/", views.DetailView.as_view(), name="detail"), # this was changed 
    path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

I also have the same code. But when I try to load the empty ’ ’ path I get the error message:

Page not found (404)

Request Method: GET
Request URL: http://127.0.0.1:8000/

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

  1. polls/
  2. admin/
  3. debug/

The empty path didn’t match any of these.

I believe the reason for this error is because

  1. the include statement that is in my urls.py in my polls app is not telling it to render a page.

  2. there is no html file that supports this path. but the solution code has no such html file.

Correct. The “empty” url is not handled by the tutorial code.