Django media page not found 404 - python

I'm trying to learn to work with Django and i'm making a image app.
Now I managed to upload images to my database, but now I want to retrieve them.
The images are stored in media/images/.
Now I made an admin page that looks like this:
AS you can see the thumbnails don't load.
When You click on one of those thumbnails, I get the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/images/june.png
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, media/images/june.png, didn't match any of these.
Here is my urls.py of my main thinghy (not the app, wich is called photo):
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Now I'm not sure what I exactly need to do to fix this.
I hope you can help me.
Please be clear ( small steps ) cause I'm not that experienced with django.
Thanks in advance
Oh BTW i'm on Win7
EDIT:
this is what I added to my settings/py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/http://127.0.0.1:8000/media/'

So right now, your urls.py doesn't have a pattern for /media/images. That pattern probably exists somewhere in your photo app. You need to link the urls for your photo app into your primary urls.py, something like:
url(r'^media/', include('photo.urls')),
This would tell the app that if it sees a url with media/ it should look for the next thing ('images/') in the photo app.

Related

404 error when looking for page on Django site

I'm trying to create a media page on my site through Django. I'm following a course and I'm pretty stumped. I'm following exactly what the guy is doing but I keep getting the following error:
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
^media/(?P<path>.*)$
My settings.py:
MEDIA_ROOT = BASE_DIR/'media'
MEDIA_URL = '/media/'
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
The problem arises as soon as I enter this line:
+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
I haven't written anything in the views.py folders yet because the course hasn't required me to on this project so far.
I'm just a newbie so I don't really have any clue what's happening when the code looks correct but I'm still running into the issue.
Any help is appreciated.
As the error message says, your site has these 2 address available for access.
admin/
^media/(?P<path>.*)$
However, you are requesting for http://127.0.0.1:8000/ instead.
Maybe try http://127.0.0.1:8000/media/image.jpg for exmaple.

Getting error when I am trying to fetch my main url patterns

I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error
Page not found (404)
Request Method: GET
URL: http://127.0.0.1:8000/
the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/
blog/
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.
when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/
here is my main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app
And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.
If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.
Create a view that will be your front page.
From there you should link to the other areas of your website.
Don't direct that to admin, that's ridiculous.
You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.
To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'),
name='home'),
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.
We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:
# app1/urls.py
from django.urls import path
from .views import AppContentView
urlpatterns = [
path('content/', AppContentView.as_view(),
name='app_content'),
]
This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.
If you think of urls configurations like a tree it might help too:
Project Level Url Configs.
|
|
|
___________________________
| |
| |
App 1 Url Configs. App 2 Url Configs.

Django how to define default app for home URL?

I'm learning Django, and so far I always had to use URL's like
projectname/appname/viewname
but what if I don't want appname to appear in the URLs for the "default" app, how can I configure my urls so that
projectname/viewname
will load the view viewname from my default app?
P.S. : Of course my primary goal is to be able to use the URL projectname/ to load the default view for the default app.
Details
Currently my ProjectName/urls.py has this:
urlpatterns = patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp1/', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
url(r'^myapp2/', include('myapp2.urls', namespace='myapp2', app_name='myapp2')),
)
so when I deploy my project to Heroku, and visit myproject.heroku.com, I get the error :
Page not found (404)
Request Method: GET
Request URL: https://myproject.herokuapp.com/
Using the URLconf defined in MyProject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^myapp1/
^myapp2/
I know this is supposed to be, but how do I fix (or hack) this to get myproject.heroku.com to work?
If not possible, how can I redirect the homepage to myproject/myapp1/defaultview ?
Thanks in advance !
my app's urls.py looks like this :
urlpatterns = patterns('myapp1.views',
url(r'^view1/$', 'view1', name='view1'), # the default view
url(r'^view2/(?P<oid>\d+)/(?P<pid>\d+)/$', 'view2', name='view2'),
)
Edit
After trying #Wallace 's suggestion url(r'^$', include('myapp1.urls', namespace='myapp1', app_name='myapp1')), and hitting the homepage, I now get the error:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^$ ^view1/$ [name='view1']
^$ ^view2/(?P<oid>\d+)/(?P<pid>\d+)/$ [name='view2']
^myapp2/
Tried changing your project urls.py with:
url(r'', include('myapp1.urls', ...)
This will include all urls from myapp1.urls where they all append to /.
The reason why r'^$' won't work is because the regex ends with $ which means there can only be 1 x url /, and because your app1.urls only has 2 urls defined and without a / or ^$ equivalent, the url resolver will then fail.
But be aware of url clashes, if your project has a ^view1/$ url it will clash to your app1's view1 for example.
Try not including your appname in the regular expression.
url(r'', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),

Django "homepage" app urls.py issue

I am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py

Can't find page in django

I'm currently learning Django and I'm trying to add form to register a User.
Now my problem starts really early because I get a 404 whenever I try to access my registration page.
My 3 files are as follows:
views.py : just a hello world to display basically
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
def registration(request):
return HttpResponse("Registration page")
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^$', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
and finally urls.py (in my app) :
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
url(r'^registration/$',views.registration, name='registration'),
)
When I go to my normal index (localhost:8000) It displays the hello world I wrote in the views.py file. However when I try to go the registration page it just returns me a 404 (localhost:8000/registration).
Now as I said I'm still learning this whole thing, but I don't get why It doesn't work properly. As far as i understand the r'^$' in the project file is pointing towards the localhost:8000 and the same regex in the app file tells it to load the index page in this location. Now as the program is still in localhost:8000, a r'^registration/' should be loading the other page in localhost:8000/registration right?
It would be really nice if you could explain to me why this doesn't work and where I did a mistake in my thoughts.
This is the error I get:
When i take
(r'^randomstringhere/', include('myapp.urls')), # index with login/registration
instead of
(r'^$', include('myapp.urls')), # index with login/registration
I can get to the registration page via localhost:8000/randomstringhere/registration. But Site is meant to have a selection where you can either Log in or register on localhost:8000 (or future domain www.randomdomainhere.com) and a registration/login form on localhost:8000/login, localhost:8000/registration (www.randomdomainhere.com/registration etc.)
At the end of each url you should add a $ symbol,
url(r'^registration/$',views.registration, name='registration'),
reffer this https://docs.djangoproject.com/en/1.4/topics/http/urls/
you are missing $ sign in the end of url.
It should be:
url(r'^registration/$',views.registration, name='registration'),
The $ symbol in django urls means end-of-string match character which is borrowed from regular expression. So, $ should be added at the end of your url on app's urls.
url(r'^registration/$',views.registration, name='registration'),
However we can write urls with the regular expressions that don’t have a $ (end-of-string match character) but we do have to include a trailing slash(/). For urls without $, you must enter the trailing slash while opening the url in your browser to match the regular expression.
Your app urls in project's urls.py is
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
So, to access the registration page, you need to browse by
localhost:8000/grappelli/registration/
i.e. append the path in your app's url to the path in project's url for the app.
Learn more about urls here.
You need to remove $ from the first url as given below
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
A url starts with ^ and ends with $. so if $ is encountered django understands that the url has ended and there's nothing more infront of that. so in your case it will no more look for "registration". or if your url with "registration" doesnt matches to any urls specified at all.

Categories

Resources