Admin page couldn't get found - python

Here come my urls.py page code:
from django.contrib import admin
from django.urls import path
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
]
When I run Django I get 404 error:
Page not found (404) Request
Method: GET Request URL: http://localhost:51169/admin Using the
URLconf defined in DjangoWebProject3.urls, Django tried these URL
patterns, in this order:
admin/ The current path, admin, didn't match any of these.
I know this is a famous problem but couldn't find any answer for it.
What is wrong?

The admin url is http://localhost:51169/admin/. You are missing the trailing slash.
Normally, Django should redirect from /admin to /admin/. If it hasn't it suggests that you've changed something in the settings, or you've upgraded Django from an older version without switching from MIDDLEWARE_CLASSES to MIDDLEWARE in your settings.py.

As Alasdair commented I updated my Django but didn't update my settings before:
Know:
In the setting, I changed middleware_classes to middleware and commented one of the middlewares: #'django.contrib.auth.middleware.SessionAuthenticationMiddleware'
now login page works!!
thank you,

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.

django 2.2.3-Django 404 error ('Page not found') after runserver

My project is named 'tweetme', and when I runserver I get this error.
Using the URLconf defined in tweetme.urls, Django tried these URL patterns, in this order:
^admin/
^static/(?P<path>.*)$
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.
urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += (static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT))
I expect to shows: the install worked successfully! Congratulation
but its shows me: Page not found at/
You have defined only two url patterns in your config. So only those will successfully work as expected. For url / to work correctly, you will have to add additional url pattern as mentioned on Django documentation using " " or "/" in relative url
https://docs.djangoproject.com/en/2.2/topics/http/urls/#url-namespaces-and-included-urlconfs

Page not found 404 on Django site

I've started with the online tutorial on Django(1.9) from thenewboston channel on YouTube which is building a simple music app.
However I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/music
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, music, didn't match any of these.
Here are my files:
website/website/settings.py
...
INSTALLED_APPS = [
'music',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
ROOT_URLCONF = 'website.urls'
...
website/website/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^music/', include('music.urls')),
url(r'^admin/', admin.site.urls),
]
website/music/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
website/music/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>This is the Music app homepage</h1>")
P.S: I've made sure that I am editing the correct urls.py file and it is placed in the correct directory also I guess, unlike the other similar questions asked about this problem
EDIT: I renamed my root directory from "website" to "website1" just to resolve the ambiguity related to reference of "website" and music and admin sections are working fine now but the django server homepage(http://127.0.0.1:8000/) is displaying the following error now:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
^music/
^admin/
The current URL, , didn't match any of these.
EDIT 2: Issue fixed, renamed the root directory from "website" to "website1". Seems Django was confused by multiple directories named "website"
Everything you did is correct. Make sure your files are saved and restart the server. If it's still not working, you can instead import the music urls like so:
from music import urls as music_urls
And then change the music url line to this:
url(r'^music/', include(music_urls)),
Only a partial answer: The error message lists all the urls that are known. In your case, only
^admin/
is known (according to the error message you posted), it does not know about ^music/. I don't see why it does not know about ^music/, though.
Edit: Although the include(string) should work, you could try to replace it with include(music.urls) and add an import statement on music.urls.

Django media page not found 404

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.

Page not found (404) in django tututorial

I'm working through https://docs.djangoproject.com/en/1.4/intro/tutorial02/ .
after changing the urls.py to
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.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)),
)
I get the following when I start the runserver:
404 error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, , didn't match any of these.
Is there anything obvious that I'm doing wrong?
Thanks in advance,
Bill
You don't have a base url defined. You need something like -
urlpatterns = patterns('',
# ...
url(r'^$', HomeView.as_view())
)
You should be able to see your site at - localhost:8000/admin/ (assuming you're running your dev server with python manage.py runserver).
Django checks all the URL's you've defined in your url conf file and looks for one that matches the url you've entered in the browser. If it finds a URL that matches then it serves up the http response returned by the url's corresponding view (HomeView in the code above). The urls.py file is matching url's to views. Views return the http response.
Looking at the error message you've got (and the code you've included from the url.py file), you can see that there's only one url defined in your app - admin/. Trying to get a page at any other url will fail.
For more information have a look at the docs for django's URL Dispatcher.

Categories

Resources