Why is views method not being invoked? - python

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.

Related

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

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.

The Django admin page is not opening. Where am I going wrong?

I've just started working with Django 3.0.1. I used the python manage.py runserver command and tried opening localhost:8000/admin page but I get hit with a "GET / HTTP/1.1" 200 3731 in the command prompt and the server stops running.
This is the urls configuration for the basic page I have been working on.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls), #navigation url for admin page
path('', include('blog.urls')),
]
Is there any error which I have to rectify?

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.

django-registration "reset" redirects to "login"?

django-registration 'reset' doesn't get processed
Using django-registration 1.0 under Django 1.6 . Everything (register, login, logoff, change) works as it should except "reset" which just redirects to the login form.
My urls.py looks like this :
urlpatterns = patterns('',
url(r'^spellweb/', include('spellweb.urls', namespace="spellweb")),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('registration.backends.default.urls')),
)
And the url associated with the 'Reset' link is :
http://localhost:8000/accounts/password/reset/
When that url is presented to the server a 302 is returned which sends the browser off to the login page like this :
[24/Feb/2014 22:55:00] "GET /accounts/password/reset/ HTTP/1.1" 302 0
[24/Feb/2014 22:55:01] "GET /accounts/login/?next=/accounts/password/reset/ HTTP/1.1" 200 1795
[24/Feb/2014 22:55:01] "GET /style.css HTTP/1.1" 302 0
[24/Feb/2014 22:55:01] "GET /accounts/login/?next=/style.css HTTP/1.1" 200 1780
[24/Feb/2014 22:55:01] "GET /accounts/login/?next=/favicon.ico HTTP/1.1" 200 1782
Just to make myself entirely clear the problem isn't just that the reset form isn't processed - the system won't even serve a 'reset' form .
So ... I'd be greatful if someone would suggest why this might be happening and/or confirm that they get some other result using Django 1.6/django-registration 1.0.
Thanks
More information pertinent to Mario Gudelj:
Yes I believe I am using the auth_urls you reference. The reason I say this is that the 'registration.backends.default.urls' referenced within my urls.py in turn references 'registration.auth_urls' . I don't know why that's not producing the effect I would expect. I would like to be sure that the url object within that auth_urls is being used but I can't find a way of establishing which django.conf.urls.patterns instance is active for any given request - that would be great if I could find a way of doing that.
More information pertinent to comment of Alasdair:
The ROOT_URLCONF points at the urls.py shown above which in its entirety is as follows:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
#url(r'^accounts/', include('auth_urls')),
urlpatterns = patterns('',
url(r'^spellweb/', include('spellweb.urls', namespace="spellweb")),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('registration.backends.default.urls')),
)
~
The spellweb.urls referenced there is as follows (nothing very much due to trying to get registration working before doing anything else) ...
from django.conf.urls import patterns, url
from spellweb import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
)
# url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
# url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
~
The registration.backends.default.urls refernenced there is as per :https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/backends/default/urls.py?at=default and that in turn references : https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/auth_urls.py?at=default
Are you using https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/auth_urls.py?at=default?
It seems for that auth_urls.py has that URL and it's using Django's generic password_reset view from django.contrib.auth.views.
Make sure that you haven't changed the URL structure in that file and that this code is still in there:
url(r'^password/reset/$',
auth_views.password_reset,
name='auth_password_reset'),
Seems like you have somehow managed to place login_required decorator around that view.

Testing URLs - URLs that don't exist return 200

I have a function to test my URLs which looks like this:
def test_URLs(self):
routes = [
'about/',
'archive/',
'index/',
'admin/',
''
'doesntExist/'
]
for route in routes:
response = self.client.get(route)
self.assertEqual(response.status_code, 200)
and my URL patterns which looks like this:
urlpatterns = patterns('',
#CMS url
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'core.views.index'),
url(r'^index/', 'core.views.index'),
url(r'about/', 'core.views.about'),
url(r'^archive/', 'core.views.archive'),
url(r'^talks/(?P<slug>[\w\-]+)/$', 'core.views.getTalk'),
In my test_URLs function, the route 'doesntExist/' doesn't exist, rather aptly. When I run my server and try to access doesntExist/ I get the log message
[04/Oct/2013 09:37:40] "GET /doesntExist/ HTTP/1.1" 404 2629
So doesntExist/ definitely doesn't exist yet when I run the above test I get:
Creating test database for alias 'default'...
..
----------------------------------------------------------------------
Ran 2 tests in 0.017s
OK
Why does my test think it exists?
My problem was I was using an app called Lockdown. Status codes where being returned as 200 because it was getting the login HTML form which is displayed no matter what the URL is if a user hasn't logged in before.
Simply don't use Lockdown whenever you're testing.

Categories

Resources