I'm new to django, thus the question.
I've a django project set up with two apps both of which seem to work fine. Just that I can't reach the homepage - which should be the default django page. I get the following error,
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
^aggregator/
^bouncer/
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.
This is my urls.py
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^aggregator/', include('aggregator.urls')),
url(r'^bouncer/', include('bouncer.urls')),
]
What am I missing here?
you are missing the path to your home directory in the urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^aggregator/', include('aggregator.urls')),
url(r'^bouncer/', include('bouncer.urls')),
url(r'^$', views.yourhomeview),
]
Related
I'm trying to render a basic html/css page in Django and I can't get it to work. It's set up seemingly the same as my index page which does work correctly and the debug explanation from the 404 response seems to show that it's pointing to the right url. Any thoughts on why this isn't working?
*I'm using django 2.1 so I'm using path instead of the old regex url mapping
*The html file exists and is located in templates\base_app\our-story.html
From views.py:
def OurStory(request):
return render(request, 'base_app/our-story.html')
From urls.py:
from base_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('our-story', views.OurStory, name='our-story')
]
From settings.py:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
INSTALLED_APPS = [
(standard django apps)
'base_app'
]
From debug message:
Request Method: GET
Request URL: http://127.0.0.1:8000/our-story.html
Using the URLconf defined in base_app.urls, Django tried these URL
patterns, in this order:
admin/
[name='index']
our-story [name='our-story']
The current path, our-story.html, didn't match any of these.
URL should be - http://127.0.0.1:8000/our-story
We cannot use our-story.html, As we are using framework and already have a route.
Use like this, You will definitely get rid of this error.
I guess path of html template would be only our-story.html If Your project name is base_app
In views.py you should return:
return render(request, template, context)
where
template = 'our-story.html'
and
context = { _your_context_dictionary }
also it is a good practice to use lower case names in the views:
def our_story
I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.
I followed the instructions in the tutorial
I made one change for debugging, here are the files:
mysite1\urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^abc/', include('polls.urls')),
url(r'^polls/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]
File: mysite1\polls\urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^&', views.index, name='index'),
]
Now if I go to the site http://127.0.0.1:8000/polls/ then it shows the login page same as going to the site http://127.0.0.1:8000/admin/.
However, if I go to the site http://127.0.0.1:8000/abc/ it gives me the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/abc
Using the URLconf defined in mysite1.urls, Django tried these URL patterns, in this order:
^abc/
^polls/
^admin/
The current URL, abc, 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.
Can someone guide me what I am doing wrong?
In mysite1\polls\urls.py It should be
url(r'^$', views.index, name='index'),
Notice that your code had an & instead of $. $ indicates a end-of-string match character.
I had a few issues with this, maybe it'll help someone. My VS code was running python 2.7 so the first line on page urls.py in the web project folder (not the polls folder) I had to change to "django.conf.urls import include, url" from "from "django.urls import include, path". I had to include .conf. and urls, path was not working and it was not working without "conf".
Another issue I had on this same page is I had to correct it to "urlpatterns = [url('', include('hello.urls'))..." from "urlpatterns = [path('', include("hello.urls"))". Path was not working so I got this to work.
The third thing is, Notice that there is a '' in the code. It should be blank otherwise when you go to url http://127.0.0.1:8000/ it doesn't redirect to views.py in the polls folder.
I hope this helps someone. Thanks
I can't seem to get the tutorial at https://docs.djangoproject.com/en/1.6/intro/tutorial03/ to work. I get the following error when trying to access http://127.0.0.1:8000/index.html or http://127.0.0.1:8000/
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, index.html, didn't match any of these.
I have /mysite/urls.py set to:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And index.html is in /polls/templates/polls/index.html
The URLs in your urls.py are the ones you can go to; so either
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/polls/
Provided both those subapps (admin and polls) define a view for the empty string ("^$"), which I believe both of them do.
The fact that one of those views happens to use a template named "index.html" doesn't mean that that filename is also used in URLs.
I'm a newbie to Django and now I'm trying to make a simple webshop using it. Today I realized that I need some debug tool, so I've chosen django-debug-toolbar. I setup it just like it's explained here - How do I see the Django debug toolbar?. But when I set DEBUG to True I get a message -
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in urlconf, Django tried these URL patterns, in this order:
^__debug__/m/(.*)$
^__debug__/sql_select/$ [name='sql_select']
^__debug__/sql_explain/$ [name='sql_explain']
^__debug__/sql_profile/$ [name='sql_profile']
^__debug__/template_source/$ [name='template_source']
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.
Here is my app's urls.py:
from django.conf.urls import patterns, url
urlpatterns = patterns('ecommerce.views',
url(r'^profile/$', 'profile'),
url(r'^login/$', 'login_user'),
url(r'^logout/$', 'logout_user'),
url(r'^registration/$', 'registration'),
)
As far as I can see now django looks for appropriate urls not in my app's urls.py, but somewhere else.