I am following this tutorial https://docs.djangoproject.com/en/2.2/intro/tutorial01/
and I am getting 404 errors when I try the first test saying it cannot find "polls/" below is some of my code
in 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),
]
in polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I tried searching but I can't find anything for django 2.2
Below is a screenshot of the error message
So the problem was my directory structure. it was mysite/urls.py when it should've been mysite/mysite/urls.py in the directory with settings.py not in the directory with manage.py
Related
I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
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.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I have a simple django project
structure is
examp-
exam
polls
templates
exam was the name of the project and polls is the name of the first app
I have the following code in exam/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.urls')),
]
and the following code in polls/urls.py
from django.urls import path
from views.polls import index
urlpatterns = [
path('', views.index, name='index'),
]
and i am getting the following error when i try to runserver
The included URLconf module 'polls.urls from
'/home/grr/Documents/examp/polls/urls.py'>' does not appear to have
any patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
in polls/urls.py
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And put index function in polls/views.py
with considering above answer code , and changing the first line using nameapp instead of "." without any error i could run my code successfully
from nameapp import views
I'm following this tutorial: https://www.youtube.com/watch?v=Z4D3M-NSN58
This is the point where I get the 404 error: https://www.youtube.com/watch?v=Z4D3M-NSN58&t=1044s
What I did so far:
Set up a new project,
Created a virtualenv,
Set up views.py,
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(response):
return HttpResponse("<h3>MySite</h3>")
Created urls.py inside my main folder,
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
Modified the urls.py inside project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Upon connecting to http://127.0.0.1:8000/ I receive the 404 error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
admin/
home/
The empty path didn't match any of these.
I am unable to proceed.
Solution:
urls.py inside the project folder has two paths, one for admin/ and one for home/.
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Trying to connect to http://127.0.0.1:8000/home or http://127.0.0.1:8000/admin works, but leaving an empty path does not.
This is because if you type /home/ it goes inside the main/urls.py and looks for a /home/ path. The path is there and it displays what's inside the views.py. In order to display the desired output without entering a path at the end of the URL I had to change the /home/ path to an 'empty' path inside of the project/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("main.urls")),
]
Now if no path is entered: http://127.0.0.1:8000/ It will go and look inside the main/urls.py for an empty path. It will find that an empty path leads to project/views.index and that is what it displays.
Add in your urls.py
urlpatterns = [
path("", views.index, name="index"),
]
and write a view to display message.
as you have put /home in your main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
so you will get a 404 error if you go to localhost:8000/
you have to go to localhost:8000/home to get the index page
I want to define URLs in Django but get an error:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]
the error is:
nameError(name'view' is not defind)
please inform me.
thanks,
Saeed
You need to import views. If you have an app named myapp and it has views.py inside the app directory, then import it like this:
from django.contrib import admin
from django.urls import path
from myapp import views # <--- Here
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]
I am trying to map the base URL to my "Learning Logs's" home page. here is the following code I have in my main urls.py file:
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('learning_logs.urls', namespace='learning_logs')),
]
I save the file and look at the terminal to see if there are any issues and it spits out the following error: ModuleNotFoundError: No module named 'learning_logs.urls'
I am no to python/django and following a book called the python crash course. not sure what I am doing wrong please help!
Best solution is check author's updates:
http://ehmatthes.github.io/pcc/chapter_18/README.html#updates
updates for urls.py
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]
Updates for learning_logs/urls.py
"""Defines url patterns for learning_logs."""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
]