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

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.

Related

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

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

Django 2 media returns error 404 (top level media folder)

Running Django 2.1.5 with Python 3.7.2 32-bit, Win10 localhost, no nginx etc. Cloned the repo https://github.com/axelpale/minimal-django-file-upload-example.git
Can upload files and they appear as a url link in the db and as a list item on the page but the images/files do not display on the page and return 404 in the console.
Error returned:
GET http://127.0.0.1:8001/media/documents/2019/04/07/image.jpg 404 (Not Found)
I can see the file at that location in explorer.
The code is exactly as per the repo with the addition of the following:
list.html line 14
<li><img src="{{ document.docfile.url }}" height="60px"></li>
and
urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [ path('', views.list, name='list')]
I have followed the documentation as far as I can and looked at other SO post that suggest the static pattern should go first.
It should be noted that the media folder is at the top level of the directory.
for_django_2-0
-myproject
--myproject
--media
--myapp
Also:
DEBUG = True
Have tried running with:
python manage.py runserver 8001 --insecure
and
python manage.py runserver 8001
Any help would be much appreciated.
Thanks, Jon
To answer my own question, after some more digging on another topic I found this useful video.
https://www.youtube.com/watch?v=PIvlcmnayOE&t=55s
The video explains something I missed in the other tutorials. Your static urls path needs to be in the root project urls.py not the app urls.py
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls'))
This now works.

how to overcome the following url issue?

Hello I am beginner learning Django I am triying to follow this tutorial:
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
I created an app called Polls to test my site:
Since I dont have idea where to put the file called urls.py
I put this file at the following directories:
Django/mysite/polls/urls.py
Django/mysite/polls/migrations/urls.py
This file contains:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And finally I added the following lines:
at this level:
Django/mysite/mysite/urls.py
this file contains:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
I dont know where is the issue but when I run:
Django/mysite$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 21, 2017 - 15:59:43
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I got in the page the following:
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:
^admin/
^polls/
The current URL, , 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.
So I really appreciate support to overcome this issue,
thanks for the support
the urls.py doesn't go into the migrations folder.
Did you register your Polls app in the settings.py file? you need to add the name of the Polls app to the INSTALLED_APPS list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example: add app name here',
'polls',]
you then need to create a url.py file within your polls app.
polls/
urls.py
In that file you would add:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
the url you want to use in your browser would be something like this:
http://127.0.0.1:8000/polls

Django App URL model not importing

Im certain that this is something simply that Im overlooking but Im too irritated to figure it out alone so thanks in advance.
Project Directory Structure
*UPDATED*
myproject/
manage.py
myproject/
apps/
geo/
urls.py
settings.py
urls.py
urls.py
from django.conf import settings
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib.gis import admin
admin.autodiscover()
from pinax.apps.account.openid_consumer import PinaxConsumer
handler500 = "pinax.views.server_error"
urlpatterns = patterns("",
url(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
url(r"^admin/invite_user/$", "pinax.apps.signup_codes.views.admin_invite_user", name="admin_invite_user"),
url(r"^admin/", include(admin.site.urls)),
url(r"^about/", include("apps.about.urls")),
url(r"^account/", include("pinax.apps.account.urls")),
url(r"^openid/", include(PinaxConsumer().urls)),
url(r"^profiles/", include("idios.urls")),
url(r"^notices/", include("notification.urls")),
url(r"^announcements/", include("announcements.urls")),
url(r"^products/", include("products.urls")),
url(r"^locate/", include("geo.urls")),
url(r"^sectors/", include("sectors.urls")),
)
if settings.SERVE_MEDIA:
urlpatterns += patterns("",
url(r"", include("staticfiles.urls")),
)
settings.py
INSTALLED_APPS = [
# project
"tulsa-site.apps.about",
"tulsa-site.apps.profiles",
"tulsa-site.apps.geo",
"tulsa-site.apps.sectors",
]
When I go to the url path "http://127.0.0.1:8000/locate/" is receive the error message: I recieve the exception value "No module named geo.urls." What am I missing?
include("geo.urls") tells Django to look for geo.urls relative to the manage.py file. So its essentially looking for this file:
myproject/
manage.py
myproject/
apps/
settings.py
urls.py
geo/
urls.py <- this file
That is sort of the new directory structure starting with Django 1.4 which encourages to have apps independent of the Django project. However if you still follow the old layout where the apps folders are within the project folder, then you have to change your imports to reflect that:
include("myproject.geo.urls")
EDIT
Following your updated layout:
include("myproject.apps.geo.urls")
url(r"^locate/", include("tulsa-site.apps.geo.urls"))

django cms apphook error

I am learning django-cms. I tried to make custom plugin which was quite successful but when I tried to hook my custom made plugin to apphook, its giving me an error, saying,
No Module named urls
.
I followed the tutorial which was given in django cms sites documentation, and created the cms_app.py file. Currently my application directory has all the files which is required to make a custom plugin for django cms, and an additional file of cms_app.py.
Is something wrong with setting of the url or do I need to create a new urls.py file inside my app directory?
My cms_app.py is exactly the same as given in the tutorial.
i have created a project called myproject using command -
python django-admin.py startproject
myproject
After referring to the tutorial given for cms I created a plugin called first, using the basic command
python manage.py startapp first
Now the plugin is working perfectly well, and the directory structure before making an attempt to the apphook was,
first/
__init__.py
cms_plugins.py
models.py
tests.py
views.py
Now after making an attempt to hook the app in apphook, the directory structure is:
first/
__init__.py
cms_app.py
cms_plugins.py
models.py
tests.py
views.py
My cms_app.py is as follows:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class FirstApp(CMSApp):
name = _("First App") # give your app a name, this is required
urls = ["first.urls"] # link your app to url configuration(s)
apphook_pool.register(FirstApp) # register your app
i have a urls.py file in myproject folder, and it is as follows:
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
I have restarted the server as was mentioned in the tutorial, but no success.
Any ideas as to what is wrong with my simple app?!
EDIT - 1
My views file is as follows:
from django.http import HttpResponse
def index(request):
“””Generate the context for the main summary page”””
return render_to_response(‘first/first.html’)
Edit - 2
I have changed my urls.py inside first app folder to this :
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
url(r'^first/$', include('first.views.index')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
But now I am getting this error:
SyntaxError at /
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.3
Exception Type: SyntaxError
Exception Value:
Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)
Exception Location: /usr/local/lib/python2.6/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 11:00:41 -0500
I have edited the urls and views but now I am getting this error.
NameError at /first/
global name 'render_to_response' is not defined
Request Method: GET
Request URL: http://localhost:8000/first/?preview
Django Version: 1.3
Exception Type: NameError
Exception Value:
global name 'render_to_response' is not defined
Exception Location: /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 14:50:32 -0500
You do not have a first.urls module with the URLs of your 'first' app. Next to your file first/models.py, create a file first/urls.py which contains the URL patterns for the 'first' app.
For the views you give in your questions, the urls.py should look something like this:
from django.conf.urls.defaults import *
from first.views import index
urlpatterns = patterns('',
url(r'^$', index),
)
Also note that in your views, you use non-standard quote characters, it should look like this:
from django.http import HttpResponse
def index(request):
"""Generate the context for the main summary page"""
return render_to_response("first/first.html")