I am currently trying to complete the Django 2.0 tutorial Poll application. I am using Django version 2.0.7 and Python 3.7.0. I have set up a virtual environment for this project. I am very new to Django so apologies if my terminology is incorrect.
Basically, I cant get part 1 to work.
I have tried numerous times to run it and I cant seem to figure out why it wont work.
Before I create the Polls app the site runs correctly and I get the rocket ship, however after I create the app, update polls/views.py, create the polls/urls.py file and update the mysite/urls.py, 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 mysite.urls, Django tried these URL patterns, in this order:
polls/
admin/
The empty path didn't match any of these.
I thought my code was incorrect, so I copied and pasted the code from the website directly and tried to run it. It still wont work.
Below is the code I am trying and the directory layout. Any help would be greatly appreciated. I'm sure I am just missing something simple.
Thank you.
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
urlpatterns = [
path('', views.index, name='index'), ]
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
print.txt
settings.py
urls.py
wsgi.py
init.py
pycache
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls
admin.py
apps.py
migrations
models.py
tests.py
urls.py
views.py
init.py
pycache
have you added your app name inside settings.py make sure you have added the name in settings.py which resides under project folder
in your case it will be here:
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',] # <--- polls in your case
make sure you are using your routes you have defined to access the correct page
for example in your case:
your website's routes are:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
and your application 'polls' routes are:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
according to your views.py file
which is this:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
you must visit http://localhost:{port number}/polls/ or http://127.0.0.1:{port number}/polls/
default port number is 8000
after running server using python manage.py runserver
to get your page as an output on browser
Related
I am new to using Django and following a tutorial (https://video.cs50.io/w8q0C-C1js4?screen=gytX_rSwZRQ&start=1160).
So far this course has been great! However, now I am stuck at successfully creating a new app using Django.
Here is the result of my efforts: [Lecture3 Page not found
Here is what the result should be: Hello, World page when found tutorial
As far as I know I've done everything correctly, perhaps I am missing something?
Below is the code I am using, which results in Django returning the error in the title:
urls.py lecture3:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
urls.py hello:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index")
]
views.py:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
settings.py:
INSTALLED_APPS = [
'hello',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I am currently using: Python 3.9.4, and Django 3.2.3
Any and all help that can be provided will be greatly appreciated.
Thanks in advance, Richardson
The issue is coming from ‘attempttwo.urls’ so revise your settings file.
the path you provided is path('hello/', include("hello.urls")) you need to add "/hello" at the end of the url in the browser, when you run python3 manage.py runserver.
Hello I'm making a portfolio website with Django, it was going fine but in the last steps following a guide I got this error
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
projects/
The empty path didn't match any of these.
This is my project folder
Here is personal_portfolio - urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("projects/", include("projects.urls")),
]
And projects - urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.project_index, name="project_index"),
path("int:pk>/", views.project_detail, name="project_detail"),
]
Also the section in settings.py
> INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projects',
]
Here is the link of the repo of the guide I'm following https://github.com/realpython/materials/tree/master/rp-portfolio
I downloaded the repo and tried to run the server and got the same error, how?
Also I searched here people with the same error even someone who was following the same guide but their solution didn't help mine and it was the same project
A request to the http://localhost:8000/ endpoint will indeed not trigger a view, since the project_index view is located under the http://localhost:8000/projects/ endpoint.
If you want to change this, you should alter the path to the projects.url with:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('projects.urls')),
]
There is also a typo in the projects/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_index, name='project_index'),
path('<int:pk>/', views.project_detail, name='project_detail'),
]
I'm new to django so take me easy. I'm just following some youtube tutorials and try to make a simple app to print Hello on the browser with django. And I keep getting this error in the urls.py file
ModuleNotFoundError: No module named 'app'
I get this error in urls.py file of my project which is bellow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
]
I also made an app called app
app\urls.py
from django.urls import path
import views
urlpatterns = [
path('', views.home, name = 'home'),
]
app\views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello!')
I read I think all threads on this topic and nothing helped so far and I can't realise where's my mistake or what I did wrong.
In your settings.py, add 'app.apps.AppConfig', in INSTALLED APP. You have to register the newly made apps to settings.py.
INSTALLED_APPS = [
'app.apps.AppConfig', // Added the name of app
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
]
*Note:- Everytime you add an app, register it in settings.py
I was creating a basic website following this tutorial:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
When I tried to redirect the home page to my django app called unihub this error prompted:
Using the URLconf defined in TFGsWeb.urls, Django tried these URL patterns, in this order:
admin/
unihub/
^static\/(?P<path>.*)$
The current path, catalog/, didn't match any of these.
My files look like this:
/TFGsWeb/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'unihub.apps.UnihubConfig',
]
ROOT_URLCONF = 'TFGsWeb.urls'
STATIC_URL = '/static/'
/TFGsWeb/urls.py
from django.contrib import admin
from django.urls import path
# Use include() to add paths from the unihub application
from django.urls import include
# Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', RedirectView.as_view(url='/unihub/', permanent=True)),
]
# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
/TFGsWeb/unihub/urls.py
from django.urls import path
from . import views
urlpatterns = [
]
I don't understand why this catalog/ appears when I see no reference to any app or path named catalog/ in the files I did modify.
I'd also like to upload this project to my github so should I just do this for the keys or sensible settings info?
with open('./secret_key.txt') as f:
SECRET_KEY = f.read().strip()
Your unihub application on the mentioned url (/) might be calling (some ajax) or redirecting some catalog app url (specifically catalog/).
Include in your urls the urls from catalog app and/or add such app to your installed apps.
On the other hand, you don't need a RedirectView to point / to your view just write:
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', include('unihub.urls')),
]
Solution:
When I tried to reproduce the "error" in other browsers it didn't happen so my guess is that...
Was not the app itself but the web browser that stored http://127.0.0.1:8000/catalog/ as the link to go to if the web does not have a valid redirect.
Thanks everyone for the support, I changed the url patterns to the suggested ones.
I've set up Django on my Windows 10 PC, and was working through the first tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
I can't seem to do the first part, because of an import error.
Here's the views.py script:
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py in the polls app:
mysite/polls/urls.py
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And urls.py in mysite:
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
After troubleshooting and fixing issues with the first two files, this is the error I get when I run "python urls.py" on the mysite urls.py file:
I've seen a few stackoverflow posts here regarding this tutorial, and similar issues. Someone advocated that I add polls to the INSTALLED_APPS section of settings.py, but this did not work.
/mysite/mysite/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
I also read that someone got this to work by adding the 'polls' module to their pythonpath... But I'm not sure if this is the way to go.
A very similar post was made here: Django reusable apps tutorial, ImportError: No module named 'polls'
but no solution was provided.
Anyone with some insight or input, please let me know what I can do to fix this, and let me know if you need any more information.
You seem to be running urls.py. shouldn't you be running python manage.py runserver command to start the app. Are you trying to achieve something else here ?