I am trying to build a web app using the Django Framework. When, I go into localhost:8000/admin
the browser says: Firefox can’t establish a connection to the server at 127.0.0.1:8000.
I am using PyCharm:
This is the error that comes up in the terminal:
Not Found: /
[21/Apr/2020 15:18:20] "GET / HTTP/1.1" 404 2031
Following which the server cuts off
my app/urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
And my project/urls:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('KMIO_page/', include('KMIO_app1.urls')),
]
In your logs, it says that you visited the home ("/") path, not the path for your admin ("/admin"). The urls.py files seem to be good. However, you could add in your app/urls.py file, outside of the urlpatterns variable, the variable app_name and assign it the value of KMIO_app1 or the name of your app. Last but not least, you could add in your app/urls.py file a slash ("/") for your home directory. For example:
app/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('/', views.index),
]
I hope that helps. Please let me know if that fixes your issue.
Related
My Django site isn't working properly. It's hard to explain but when I run my Django web server and go to http://127.0.0.1:8000/hello/ I see "Hello, World!" as expected. But when I go to http://127.0.0.1:8000/dashboard/ I see the same thing, when I should be seeing "Hello". There is to much code to put on stack overflow and it won't make sense so I made a GitHub repo https://github.com/Unidentified539/stackoverflow.
So to simplify your life
urlpatterns = [
path("",views.pomo,name="pomo"),
path("agenda/",views.agenda,name="agenda"),
path("notes/",views.notes,name="notes"),
]
this is your urls.py file in you app
just type this in your project urls.py so you don’t make a mess with paths and includes
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('pomofocus.urls'))
]
this is just an example you need to enter your own paths and views
What's your code problem?
According to your code, you have two same paths for the home page, and when you enter http://127.0.0.1:8000/hello/ Django look at the project-level urls.py (in your code djangoProject1/urls.py) and knows that must go in app-level urls.py (in your code practice/urls.py) and in this file, you have two same paths, and Django uses the first one so you get write page because first one is related to hello view and when you type http://127.0.0.1:8000/dashboard/ Django again look at the project-level urls.py and knows that must go in app-level urls.py and in this file again use the first path and you get incorrect HTML file (because both paths are the same and Django always use the first one)
How to fix this?
in project-level url.py set both paths to '' and in app-level set your project-level paths. (in other words, replace paths in app-level and project-level
djangoProject1/urls.py:
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('practice.urls')),
]
practice/urls.py:
from django.urls import path
from practice import views
urlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
path('dashboard/', views.dashboard, name='dashboard')
]
Your code should be like in
practice/urls.py
from django.urls import path
from practice import views
urlpatterns = [
path(" ", views.hello_world, name='hello_world'),
path("dashboard", views.dashboard, name='dashboard')
]
Project1 urls.py
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(" ", include('practice.urls')),
]
app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index, name='index')
]
project urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
I've re-watched the tutorial several times and still can't find the error.
Since your project's urls.py includes hello using the prefix 'hello/' in
path('hello/', include("hello.urls"))
and your app's urls.py also has hello as the path in
path('hello', views.index, name='index')
you will need to access /hello/hello in your browser to get to a view that doesn't 404.
Django's technical 404 page (the one with the yellow background) should show the available paths -- if you don't get the technical 404 page, ensure you have the DEBUG setting turned on.
I'm a newbie in web development and I'm learning to use Django. Unfortunately I have been stuck for more than 24 hours trying to figure out how to set the URL of a web page. I keep getting status 404 error displayed on the browser after running python server. I have checked python documentation and other documentations online but I still don't see anywhere I'm getting it wrong.
I have the following files in the main Django follow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank', include ('qbank.url')),
path ('admin/', admin.site.urls),
]
settings.py
INSTALLED APPS = [
'qbank'
.....
]
In my project folder(which I named qbank) I have the following files:
urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('qbank'), views.index, name = 'index'
]
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse ('Hello from Qbank')
The way you wrote it now, it will require two qbanks, one for the "root" urls.py, and one for the urls.py in the qbanks, hence localhost:8000/qbankqbank. If you only want to access it with qbank, then you remove the qbank for example from the urls.py of the qbanks app. So then the "root" urls.py looks like:
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank/', include('qbank.url')),
path ('admin/', admin.site.urls),
]
and the urls.py of your app:
# qbank/urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('', views.index, name='index')
]
I'm playing with Django python tutorial and notice a very odd url behavior.
My project name is 'mysite', and in its urls.py, it by default has
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
]
This shows that little rocket base page when I visit localhost:8000.
Then I editted like following,
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('student/', include('student.urls'))
]
to indicate a new url for my own app, this time the base page localhost:8000 returns a 404. I'm very curious why my new line will affect the base page? BTW my django version is 3.0.6.
I'm a beginner Django. So I read the tutorials and I'm following them.
But I'm receiving an Error page. 'What is the problem...?'
(I installed the python 3.6.5 version)
My error:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1.polls/
2.admin/
The empty path didn't match any of these.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
And Finally!!
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),
]
But running python manage.py runserver gives this error:
Page not found !!, The empty path didn't match any of these!!!
So I need your help, plz help me....!!
I assume your mysite is the "Django project name", so it contains the root urls.py. If we take a look at the file, we see:
# 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),
]
So that means that all the urlpatterns in the polls/urls.py are "sub URLs" of this path('polls/', ...), therefore you can say that the URLs in polls/urls.py are "prefixed" with 'polls/' implicitly.
In order to reach the index(..) view, we thus have to find a "path" from the root URLs to this specific view. The only way to reach this is first taking the 'polls/' path, and then selecting the '' path in polls/urls.py. The path is thus polls/.
So in order to "trigger" this view, you need to query a URL like:
https://localhost:8000/polls/
(of course if you have specified another port, or run the Django webserver on another server, you need to modify localhost, and 8000 accordingly).
I stumbled on this error and figured out that I misinterpreted the instruction at the top of page 18, section 2.3.4, listing 10. I modified that mysite/usrls.py file at the top level mysite directory, instead of the mysite/mysite/urls.py file. It worked fine after that.