Page not found after trying to add a new path to the urlpatterns of main urls.py file? (Mosh Python Tutorial) - django

I'm following Mosh's tutorial video on Python. He begins the django section (https://youtu.be/_uQrJ0TkZlc?t=18085) by installing django 2.1. I am able to open a development server the first time as he does:
pip install django==2.1
django-admin startproject pyshop .
python manage.py runserver #server works
Here are the steps he goes through to add a "products" app/path:
python manage.py startapp products
Opens views.py from this new products folder and modifies code to this:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello World')
Creates urls.py inside the products app/folder and adds this code:
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.index) # Here should have been '' and now fixed
]
Opens the main urls.py in the pyshop folder and adds/modifies the end of the file like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
Mosh goes back to the server and adds /python to the url to get a page with "Hello World"
(https://youtu.be/_uQrJ0TkZlc?t=19220)
Upon trying to run the server again, I get Page not found error. Is there something I'm missing? I didn't figure it'd be a version issue since I made sure and installed the same 2.1 version.
As Jeffery and Daniel mentioned, I had an incorrect space and it needed to be an empty string in the products urls.py path. However, I'm still getting page not found error. Here's the exact error message if that helps:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.

Modify your url path in the products app/folder like this
urlpatterns = [
path('', views.index)]
Remove the space in between

Related

First view in DJANGO tutorial -> Page not found (404)

I was doing tutorial "Writing your first Django app, part 1" and I got stuck in "Write your first view". I have fulfil all instructions regardings files. When I go to http://127.0.0.1:8000/ then I can see "The install worked successfully! Congratulations!" but if I go to http://localhost:8000/polls/ then I see:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
In this case according instructions I should see "Hello, world. You're at the polls index". I attached 2 files content and 2 interesting screens (why does it emphasize?).
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
I was looking answer in Stack Overflow (also in hints) or YouTube but unfortunately without success. Interesting but in another computer it works, but on my laptop not.
So you are doing the URLS incorrectly, or partially at least.
You need to create another app in django called polls:
python manage.py startapp polls
Then in the polls/urls.py file, include an extension for /polls, that references your view in polls/view.py:
urlpatterns = [
path('/polls', views.<view-name>, name='Polls URL')
]
Then it should work, otherwise you are doing something wrong and need to elaborate on our problem.
Note that Django's DEBUG should be True when developing at all times, so you can see the full error log. DEBUG=FALSE is used if you have a webapp public (aka deployed/in production.
Subject closed. Solution -> Bad configuration urls.py file in mysite and polls. Check content both files in tutorial context.

Page not found error - Writing your first Django app, part 1

I have installed Django 2.2.5 in my conda environment and I'm following the Django tutorial for Writing your first Django app, part 1
I followed the steps exactly but I'm getting a Page Not Found error when I try to access the polls/ url.
My root directory structure is like this
mysite/
manage.py
db.sqlite3
urls.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
And my view is like this
D:\TEMP\djangotest\mysite\polls\views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World. You're at the polls index")
My two urls files are like this
D:\TEMP\djangotest\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
D:\TEMP\djangotest\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),
]
And I run the app from the environment like this
(py3.6) D:\TEMP\djangotest\mysite>python manage.py runserver
But when I go to the url indicated in the tutorial it give the 404 error - page not found from the console
October 21, 2019 - 16:23:13
Django version 2.2.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /polls
[21/Oct/2019 16:23:19] "GET /polls HTTP/1.1" 404 1954
From the browser it look like
Page not found (404)
Request Method:
GET
Request URL:
http://localhost:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls, 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.
Your main urls file is in the wrong directory. It looks like you've created a separate urls.py in the base "mysite" directory; instead you should have edited the existing one in the "mysite/mysite" directory. The one you've created isn't being used at all.

Django polls app is not working as expctected in the tutorial for me

I am getting this error every time restart my server
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 path, polls/, didn't
match any of these.
I have tried restarting the server but the same error is popping up every time.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = 'index'),
]
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)
]
views.py/polls
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world. You're at the polls index.")
The expected result as per the Django tutorial is the text "Hello world. You're at the polls index." after you start your server
You probably didn't add your app polls to installed apps in your settings.py.
Open your file settings.py and write:
INSTALLED_APPS = [
....,
'polls',
]

Django URLs management

I'm a full beginner on Django. Therefore, I'm sorry if my question is not making so much sense.
I'm studying Django tutorial - step 1. I installed properly Django and created my first Django project named 'vanilla'. The urls.py script of the vanilla project is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I start Django with this 'vanilla' project and start the development server at http://127.0.0.1:8000/, I correctly see the start page (with a rocket).
In a second project that I named 'djtutorial', I created as requested in Django tutorial - step 1 a 'polls' app. As requested, I modified urls.py file in djtutorial\polls which now has following content:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If I start the server the url http://127.0.0.1:8000/polls/ is working properly. However, if I launch the root url of the site (i.e. http://127.0.0.1:8000/), I now have a 404 error.
Why is the 'root url' of the site now hidden?
There wasn't any root URL at all. Main page informing you that your project is created properly shows up only if you don't have any view of your own added to urlpatterns. When you create any page, this welcome page stops showing up and that is expected.
If you want to show your own page on root of your website, use this in your urlpatterns:
path('', >>VIEW OR INCLUDE HERE<<),

django urls: "Django tried these URL patterns"

I am trying a tutorial on Django called blog. I have the following structure:
FirstBlog|FirstBlog
settings
urls
__init__
etc
blog
templates | index.html
migrations
views.py
manage.py
The view.py has
from django.shortcuts import render
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
return render('index.html')
The urls.py has
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^blog', 'FirstBlog.blog.views.home',name='home'),
]
and I get this error:
Using the URLconf defined in FirstBlog.urls, Django tried these URL patterns, in this order: ^blog [name='home']
The current URL, , didn't match any of these.
I can't seem to get it right..
Any help would be appreciated.
Thank you,
You are requesting for / url and you have not saved any such mapping. Current mapping is for /blog . So it will work for the same url.
i.e goto the browser and request /blog
If you need it to work for / then change the urls appropriately.
within your blog app, create a urls.py file and add the following code which calls the home view.
blog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#url(r'^',views.home, name='home'),
]
then in your root urls file which can be found at FirstBlog/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^blog/',include('blog.urls')), #when you visit this url, it will go into the blog application and visit the internal urls in the app
]
PS:
your templates should be in blog/templates/blog/index.html
Read this docs on templates to understand how django locates templates.
This one is to understand how urls work Urls dispatcher
You are doing this in the wrong way! Try doing that using TemplateView from class-based views, which are the current standard from django views.
Check the django documentation: https://docs.djangoproject.com/en/1.9/topics/class-based-views/
Use this at the urls.py:
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^blog/', TemplateView.as_view(template_name="index.html")),
]
And then, create a folder called templates in the project root ( like this: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something ) named index.html
Simply go to file then select Save all your project instead of save. Or use shortcut Ctrl +k s on windows. Project should be able to sync and display the code on Django interface