Page not found in Django - python

I'm following this tutorial to learn Django. I'm a total starter.
My urls.py file has following code
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
My views.py file is
from django.shortcuts import render
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
When i try to access th url http://127.0.0.1:8000/polls/ in my system it gives a page not found message. What am i doing wrong?
Is it a problem with the version difference?
Here is the screenshot of the error

This error comes if your urls.py file in polls directory does not have this pattern. Create a file urls.py your polls directory and add following url pattern
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
)

Ok, so if you want to use /polls/ with this urls,
your polls.urls should look similar to this ( for django1.8):
from django.conf.urls import url
from polls.views import BasePollView, AnotherPollView
urlpatterns = [
url(r'^$', BasePollView.as_view()),
url(r'^another-url/$', AnotherPollView.as_view())
]
/polls/ -> BasePollView
/polls/another-url/ -> AnotherPollView

Related

Django FirstApp

I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
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.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

i am trying to learn django from django documentation but i am getting an error message in the first part of the documentation

Using the URLconf defined in blogs.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
this is the error i am getting after i wrote the following code:
for urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
for polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
3.for polls/views.py
"""
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
"""
how i can i remove this error in vs code

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/catalog/

I got the follwoing error whenever I run the project and automatically catalog/ is added to the url:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
[name='index']
admin/
first_app/
The current path, catalog/, 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 project urls.py
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
]
Here is first_app urls.py code:
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
How to get the index page as a default and get rid of the catalog.
Here is views.py file:
from django.shortcuts import render
from django.http import HttpResponse
from first_app.models import Topic, AccessRecord, Webpage
# Create your views here.
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'first_app/index.html', context=date_dict)
Hey Mak in your code u didnt mention any url related to /catalog/ so you are getting that 404 error message. Error 404 means that the page couldnt found by the server add /catalog/ in your url patterns so that the server could know which page is to be shown if /catalog/ is requested . And as far as ur proble"and automatically catalog/ is added to the url:" may be its your browser autocompleting the address field so once plesase check it. if the request is only /catalog then ur myproject urls.py file should be something like this
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),
path('catalog',views.your_function_name)
]
if the request is first_app/catalog then ur first_app urls.py file should be something like this in url patterns
urlpatterns = [
path('/catalog', views.catalog, name='catalog'),
]
from django.contrib import admin
from django.urls import path, include
from first_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('first_app/', include('first_app.urls')),]
and
from django.urls import path
from first_app import views
urlpatterns = [
path('', views.index, name='index'),
]
>>>>> if your templates files :
templates/index.html
def index(request):
webpages_list = AccessRecord.objects.order_by('date')
date_dict = {'access_records': webpages_list}
return render(request, 'index.html', context=date_dict)

Page not found 404 on Django server?

I am doing a Django course and in my first tutorial only I am getting "Page on found 404 error" while mapping the URL will the view.
I referred to this link and did same steps I am still getting same error.
https://docs.djangoproject.com/en/3.0/intro/tutorial01/
polls/urls.py
-------------
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
polls/view.py
-------------
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
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),
]
Directory of my code
Help me with the error... I checked all sources and followed exactly the link... Still cannot run it properly

How to configure Django views and urls to render specific templates

When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.

Categories

Resources