Django Page not found (404) - django

I'm designing a photo app .
I get this error every time I view an uploaded picture on my admin page.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/images/California_Poppy.jpg
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
^cool/
^forum/
^register/
The current URL, media/images/California_Poppy.jpg, didn't match any of these.
My current settings are :
MEDIA_ROOT = 'C:/djcode/mysite/photo'
MEDIA_URL = 'http://127.0.0.1:8000/media/'
I reckon the problem is with these settings. I'm using window btw

The Django docs have a solution for you to serve media in development. Usually in production you alias your media directory to be served directly from your web server to be more efficient. To serve in development, the docs show two different solutions. You can check out the provided link to read the docs and figure out which one would be better for you.
from django.conf import settings
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
OR
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

Page not found at/admin

I'm a very beginner.
When I tried to go visit this (http://127.0.0.1:8000/admin), I couldn't. Here have shown page not found. What can be the solution?
Problem that I faced:
Page not found (404)
“D:\1_WebDevelopment\Business_Website\admin” does not exist
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: django.views.static.serve
Using the URLconf defined in business_website.urls, Django tried these URL patterns, in this order:
admin/
admin/
[name='index']
singup [name='handle_singUp']
login [name='handle_login']
logout [name='handle_logout']
contact [name='handle_contact']
frontend_orders [name='frontend_orders']
hire_me [name='hire_me']
^(?P<path>.*)$
The current path, admin, matched the last one.
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.
Problem : open the picture
business_website 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 = [
path('admin/', admin.site.urls),
path('', include('business_app.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
business_website url.py : open the picture
business_app urls.py:
from os import name
from django.contrib import admin
from django.urls import path
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
path('singup', views.handle_singUp, name= "handle_singUp"),
path('login', views.handle_login, name="handle_login"),
path('logout', views.handle_logout, name="handle_logout"),
path('contact', views.handle_contact, name="handle_contact"),
path('frontend_orders', views.frontend_orders, name="frontend_orders"),
path('hire_me', views.hire_me, name="hire_me")
]
business_app url.py : open the picture
Delete from business_app urls.py this row:
path('admin/', admin.site.urls),
You should not call it twice.
You should set MEDIA_URL in settings to something. Like:
MEDIA_URL = '/media/'
Django urls need to have a trailing slash and the url you tried to access does not have it, check in your settings.py file if APPEND_SLASH is set to false
Among Django's many built-in features is APPEND_SLASH, which by default is set to True and automatically appends a slash / to URLs that would otherwise 404.
You can turn off this option by just setting APPEND_SLASH = False
You can read more here about why django uses trailing slashes
You have the same admin path defined in your root urls.py and in your app. It should probably be in just the root. Remove it from:
from os import name
from django.contrib import admin
from django.urls import path
from .import views
urlpatterns = [
# path('admin/', admin.site.urls), ##### REMOVE
path('', views.index, name="index"),
path('singup', views.handle_singUp, name= "handle_singUp"),
path('login', views.handle_login, name="handle_login"),
path('logout', views.handle_logout, name="handle_logout"),
path('contact', views.handle_contact, name="handle_contact"),
path('frontend_orders', views.frontend_orders, name="frontend_orders"),
path('hire_me', views.hire_me, name="hire_me")
]
Edit
After trying, and failing to reproduce the OP's error on my machine using all the answers given as of this writing, it turned out my original answer was not correct (not incorrect, but also not the solution), in fact the original answer given by Jaime Ortiz, was most likely the correct one.
But why was it so hard to come to this realization? Within the link he provided was this, which is why, when I initially tried his solution it did not work. Below is from the answer provided by
All Іѕ Vаиітy in that link. Note that within the [] is my insertion.
Since django observes both the urls [the one with the trailing slash
and the one without] as different, if you are caching your app, Django
will keep two copies for same page at ...
So either use admin/ instead of admin, or apply APPEND_SLASH = False to your settings.py, clear your browser cache and then you can use either, Django will append the slash automatically.

Django: Downloading uploaded files

I have form details in this question Django: Adding files to a form gives multiple argument error
How to download the uploaded file. When i go to edit view of the form, i can see uploaded file url, but its not downloading.
What setting to be changed for development and production mode?
Error upon clicking link:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/Certificate.docx
Using the URLconf defined in tiktant.urls, Django tried these URL patterns, in this order:
^ ^$ [name='home']
^ ^login/$ [name='login']
^ ^logout/$ [name='logout']
^ ^logout_then_login/$ [name='logout_then_login']
^ ^dashboard/$ [name='dashboard']
The current URL, media/Certificate.docx, didn't match any of these.
The media url is not showing in the root urls.py file. You need to add the below code in the urls.py file to enable the same.
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Note: Please don't forget to add the MEDIA_ROOT and MEDIA_URL in settings.py file. For downloading a file, you need to write some more code.
make sure you added the following lines in your project
settings.py
import os
def root(x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
TEMPLATE_CONTEXT_PROCESSORS = (
'-----------------------'
'django.core.context_processors.media',
)
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns += patterns('',url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)

URL Conf - Serving views at the root URL and non root URLs within one app

I've run into a problem configuring the url.py files in a new project. I have one app, which contains two views. The first view should appear at myurl.com, while the other should appear at myurl.com/foo. myurl.com appears without trouble but myurl.com/foo shows a 404 page not found error.
The url.py at the project level looks like this:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('myapp.urls', namespace="myapp")),
)
And the url.py at the app level looks like this:
from django.conf.urls import patterns, include, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.book_search, name='book_search'),
url(r'^foo/', views.myapp, name='myapp')
)
I understand that that django is taking the URL that is submitted and checks it against the url patterns defined at the project level, but I don't know how to direct it to myapp without hosting all of myapp at some url that is not at the root, i.e. myapp.com/bar and myapp.com/bar/foo.
url(r'^$', include('myapp.urls', namespace="myapp")),
Remove the ^$ here. This would force all included URLs to match only if starting with "the end", i.e. nothing.

URLConf that works on development server but not on GAE

I am using Django non-rel in Google App Engine, and my URLConf seems not to work and generate a 500 Server Error.
This is my urls.py file at the root of my app :
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^home/', include('appname.home.urls')),
)
And this is my urls.py in a subpackage home of appname:
from django.conf.urls import patterns, include, url
urlpatterns = patterns(r'appname.home.views',
url(r'^0/', 'home'),
)
It works great in development server but it does not work on Google App Engine.
I already read a related question and its answer but it did not solve my problem.
What does the error message say in the logs? You urlpatterns is malformed. The first r'' is incorrect. That should be a string. And, is the 0 in your url intentional? Change to:
urlpatterns = patterns('appname.home.views',
url(r'^$', 'home'), # matches mysite.com
url(r'^0/$', 'home'), # matches mysite.com/0/
)

Django URL Conf Returns Incorrect "Current URL"

I have a django app that is mostly done, and the URLs work perfectly when I run it with the manage.py runserver command. However, I've recently tried to get it running via lighttpd, and many links have stopped working.
For example: http://mysite.com/races/32 should work, but instead throws this error message.
Page not found (404)
Request Method: GET
Request URL: http://mysite.com/races/32
Using the URLconf defined in racetrack.urls, Django tried these URL patterns, in this order:
^admin/
^create/$
^races/$
^races/(?P<race_id>\d+)/$
^races/(?P<race_id>\d+)/manage/$
^races/(?P<text>\w+)/$
^user/(?P<kol_id>\d+)/$
^$
^login/$
^logout/$
The current URL, 32, didn't match any of these.
The request URL is accurate, but the last line (which displays the current URL) is giving 32 instead of races/32 as expected.
Here is my urlconf:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('racetrack.races.views',
(r'^admin/', include(admin.site.urls)),
(r'^create/$', 'create'),
(r'^races/$', 'index'),
(r'^races/(?P<race_id>\d+)/$', 'detail'),
(r'^races/(?P<race_id>\d+)/manage/$', 'manage'),
(r'^races/(?P<text>\w+)/$', 'index'),
(r'^user/(?P<kol_id>\d+)/$', 'user'),
# temporary for index page replace with welcome page
(r'^$', 'index'),
)
urlpatterns += patterns('django.contrib.auth.views',
(r'^login/$', 'login', {'template_name': 'races/login.html'}),
(r'^logout/$', 'logout', {'next_page': '/'}),
)
Thank you.
I would think that the problem lies in the configuration of lighttpd.
Django is capable of "translating" "request urls" to "current urls" for the url checking, for example via the django.root configuration for mod_python.