Use Django's Generic Date Views There's a 404 error - python

views.py
from django.urls import path, re_path
from blog import views
app_name = 'blog'
urlpatterns = [
path('archive/<int:year>/<str:month>/<int:day>/',
views.PostDAV.as_view(), name='post_day_archive'),
path('archive/today/', views.PostTAV.as_view(), name='post_today_archive'),
]
urls.py
from django.views.generic.dates import DayArchiveView, TodayArchiveView
from blog.models import Post
class PostDAV(DayArchiveView):
model = Post
date_field = 'modify_dt'
class PostTAV(TodayArchiveView):
model = Post
date_field = 'modify_dt'
error code
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/archive/today/
Raised by: blog.views.PostTAV
No posts available
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 using DayArchiveView, template is opened, but 404 error occurs when using TodayArchiveView.On the official website of the library (https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-date-based/), I'm sure TodayArchiveView and DayArchiveView have the same template name, but why does 404 error occur only when I use TodayArchiveView? Why does the TodayArchiveView alone have 404 errors when both use the same template? Just in case, I named the template for TodayArchiveView as something else and tried using another template, but there is still a 404 error. Is there anything I missed? I'm sorry for my poor English.
stack trace
System check identified no issues (0 silenced).
May 20, 2020 - 12:05:42
Django version 3.0.6, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[20/May/2020 12:05:45] "GET / HTTP/1.1" 404 2135
[20/May/2020 12:05:51] "GET /blog/archive/ HTTP/1.1" 200 771
Not Found: /blog/archive/today/
[20/May/2020 12:05:57] "GET /blog/archive/today/ HTTP/1.1" 404 1731
The first 404 error occurred because I haven't set up the homepage yet. Please ignore it.

Related

How to fix exception 404 in Django views?

I'm following this tutorial:https://www.w3schools.com/django/django_views.php
After copying all the code to create the members and admin views I get this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in myworld.urls, Django tried these URL patterns, in this order:
members/
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.
In your project urls.py you set the path to the members app to use 127.0.0.1:8000/members
In order to navigate to 127.0.0.1:8000/members using the default route, you need to change the path to look like this :
path('', include('members.urls')),

Django odd behaviour

Whenever I start a project in Django, independently of the name of the app or project, and irrespective of any urls or settings configuration, upon running python manage.py runserver and navigating to 127.0.0.1 I get the following error: Not Found: /ball/ [10/Jan/2022 11:32:32] "GET /ball/ HTTP/1.1" 404 2337 and this displays in th browser:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/ball/
Using the URLconf defined in ornaments.urls, Django tried these URL patterns, in this order:
admin/
^static/(?P<path>.*)$
^media/(?P<path>.*)$
^static/(?P<path>.*)$
The current path, ball/, 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.
Why is Django looking for a route called ball?

How to customize 404 page in django?

In my django app I want to show a custom not found 404 page. But after reading the documentation and coding I am still getting the default 404 Not found page.
I am working with Django 2.2.8
I also set DEBUG=False
Here is my code:
in setting.py I have defined:
handler404 = 'riesgo.views.views.error_404'
Definition in riesgo/views/views.py
def error_404(request, exception):
return render(request,'404.html')
In your main urls.py file
handler404 = 'mysite.views.my_custom_page_not_found_view'
you can read the docs for a more detailed answer.

How to fix "Page not found (404)" error ("Django tried these URL patterns... The empty path didn't match any of these.")

As a newbie to Django, I encountered the same problem as many before me. I would appreciate if you didn't mark my question as a double immediately because I checked the fixes those old posts suggested but to no avail.
I was following this tutorial and have finished with all up to the heading "Projects App: Templates". Now when I start the server, at http://localhost:8000/ I get:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
projects/
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 console output when I run server:
System check identified no issues (0 silenced).
April 05, 2019 - 15:31:54
Django version 2.2, using settings 'personal_portfolio.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[05/Apr/2019 15:32:01] "GET / HTTP/1.1" 404 2042
Not Found: /favicon.ico
[05/Apr/2019 15:32:01] "GET /favicon.ico HTTP/1.1" 404 2093
What I tried, but didn't help:
restarting the server,
checking my code inside the files against the tutorial's source code,
Made sure that 'projects' is inside the INSTALLED_APPS list in settings.py.
Here is urls.py that's inside rp-portfolio\personal_portfolio:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('projects/', include('projects.urls'))
]
Here is urls.py that's inside rp-portfolio\projects:
from django.urls import path
from . import views
urlpatterns = [
path("", views.project_index, name="project_index"),
path("<int:pk>/", views.project_detail, name="project_detail"),
]
In the urls.py
from django.urls import path
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('projects/', include('projects.urls')),
path('', RedirectView.as_view(url='/projects/')),
]
try this
in urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('projects.urls'))
]
hope it helps
as you are seeking a path inside project your requested path must be this http://localhost:8000/projects/
You need to add:
in polls/urls.py
add this: from django.conf.urls import url
so it shold look like:
from django.urls import path
from django.conf.urls import url
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Sometimes the URL picks up other words from the terminal.
Example 1.0
Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.
For instance in Example 1.0, instead of opening a link like http://127.0.0.1:8000/
The link will be http://127.0.0.1:8000/Quit since /Quit is not included as part of your code you will get the 404 error.
Example 2.0
Starting development server at http://127.0.0.1:8000/
Quit the server with
CTRL-BREAK.
Not Found: /Quit
[27/May/2021 22:36:07] "GET /Quit HTTP/1.1" 404 2066
[27/May/2021 22:36:59] "GET / HTTP/1.1" 200 5873
[27/May/2021 22:37:04] "GET / HTTP/1.1" 200 5873
Hope you find this helpful.

Why is views method not being invoked?

I have a web directory with urls.py in a directory (RazorWare_Web) as follows:
from RazorWare_Web.views import home
urlpatterns = patterns('',
url('/', home.index, name="index"),
url(r'^razorware/', include("RazorCRM_App.urls")),
url(r'^admin/', include(admin.site.urls)),
)
So in my browser, http://localhost:8000/razorware navigates to the home view's index() method. I'm a little confused by this. I would expect http://localhost:8000/ would navigate to home. Instead this produces a url not found error page.
Aside from that, the real problem (and I suspect it is related to the above) exists when I perform a $.getJSON call from my HTML page. First, the actual app exists under the sibling directory, RazorCRM_App, with the following:
from RazorCRM_App.views import queries
urlpatterns = [
url(r'query_locale', queries.query_locales, name="query_locales"),
]
When I execute the following script:
function query_locale_by_zip(){
var post_code = txt_postal.val();
$.getJSON("query_locale", {post_code: post_code}, function(result){
console.log("[APP] query locale for: " + post_code + " [returned: " + result.success + "]")
});
}
... the queries.query_locales method is not being called. However, I do get the following output from the console:
[25/Feb/2015 15:20:51] "GET /razorware/query_locale?post_code=9
HTTP/1.1" 200
[25/Feb/2015 15:20:51] "GET
/razorware/query_locale?post_code=92 HTTP/1.1" 200
[25/Feb/2015
15:20:51] "GET /razorware/query_locale?post_code=920 HTTP/1.1" 200
[25/Feb/2015 15:20:52] "GET
/razorware/query_locale?post_code=9205 HTTP/1.1" 200
[25/Feb/2015 15:20:52] "GET /razorware/query_locale?post_code=92058
HTTP/1.1" 200
I might understand if there was a message stating that the url could not be found. But this just seems odd.
Using Django version 1.7.1 and Python 3.4.2
Your root url should look like url(r'^$', home.index, name="index") and your query_locale url should look like url(r'^query_locale/$', queries.query_locales, name="query_locales").
url dispatcher has broad documentation about how urls work.

Categories

Resources