just curious what's the proper/transparent way to get plain 404 error instead of view for the pre-defined url ?
The default install of Satchmo handles /quickorder url with a cart view. I would like to completely disable handling for this url. So far I did get is the following url replacement in url.py at the top of store tree:
from django.conf.urls.defaults import *
from satchmo_store.urls import urlpatterns
from satchmo_utils.urlhelper import replace_urlpattern
replace_urlpattern(
urlpatterns,
url(r'^quickorder/$', handler404)
)
The handler404 is from django.conf.urls.defaults but it returns Django's own content instead of the shop's default 404 page like if you would ask for a non-existent page like http://www.google.com/quickorder_somenonsenseintheurl.
Any idea how to override this while avoiding modifications of default Satchmo installation ?
Related
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')),
I would like to redirect all 404 errors to the index page of my django project. I am trying to do this by creating a view to handle all 404 requests and then adding the references to my urls.py.
This is what I have in my views.py. This is the classroom.py views in my views folder:
def view_404(request, exception=None):
return redirect('/')
and my urls.py:
handler404 = 'classroom.classroom.view_404'
However I get the error message
ERRORS:
?: (urls.E008) The custom handler404 view 'classroom.classroom.view_404' could not be imported.
HINT: Could not import 'classroom.classroom.view_404'. Parent module classroom.classroom does not exist.
Which I assume has more to do with how I am referencing the my views.py file.
Here is the structure of my project
multipleusers
-->django_project
---->templates
---->views
classroom.py
students.py
mentors.py
Then it should be :
<name_of_your_project>.<name_of_your_app>.views.classroom.view_404
if classroom is effectively the name of your file in which your views are.
There is a complex app (not possible to just paste the code). Going to try to explain.
Django
There is a urls.py file from the native Django app. The urlpatterns defines and register its urls. The ^foo/ defines a group of related urls and the foonamepsace.
urlpatterns = patterns('',
...
url(r'^foo/', include('foo.urls', namespace='foonamespace')),
...
Now there is a method generate_report which does some logic inside and then uses render_to_string to return the HTML:
def generate_report(..):
...
return render_to_string('foo/foo_report.html', args)
Everything works inside the app, the url get reversed successfully.
Django Rest Framework (DRF)
Now there is a DRF implementation and one of its resources is supposed to return a report in a binary format.
class PDFReportViewSet(APIView):
renderer_classes = (BinaryFileRenderer, )
def get(..):
...
pdf = generate_report() # <-- fails with NoReverseMatch
...
return response
Problem
The ViewSet calls the generate_report, however one gets an error when trying to parse the HTML:
NoReverseMatch: foonamespace' is not a registered namespace
Question
Any clues why DRF cannot reverse the namespcae/url from the the core of Django app? How to make sure DRF can reverse a namespace from the core urls.py urlpattern?
Added
After investigation, inside the foo_report.html any usage of the url, for example {% url 'foonamespace:123' %} or {% url 'barnamespace:123' %} produces the error - only if ran from the DRF (running the same page using native Django works fine).
URLS
foo.urls.py
from django.conf.urls import patterns, url
from foo.views import (FooListView, FooDetailView...)
urlpatterns = patterns('',
url(r'^$', FooListView.as_view(), name='foo_list'),
url(r'^(?P<pk>\d+)/$', FooDetailView.as_view(), name='foo_details'),
Important note. The app is served at some.domain.com/, while the REST is served from some.domain.com/rest. So may be this way /rest just don't include anything because it is a parent of the root (which includes the foo.urls.py)
I was managed to resolve my issue with the help from #dirkgroten. It was difficult to see the problem without looking at the source code.
Solution
Updated the routers.py file:
urlpatterns = router.urls
urlpatterns += patterns('',
url(r'^foo/', include('foo.urls', namespace='foonamespace')),
)
Explanation
Basically, the app was serve from the root url / while the rest was served from /rest. The DRF router simply didn't include any of the root routes. Adding them manually like it is shown in solution resolved the problem and made foonamespace visible for all DRF elements.
I'm using Django 1.9. Is there any way to redirect a URL with a parameter in my urls.py file?
I want to permanently redirect a URL like /org/123/ to the corresponding URL /neworg/123.
I know how to redirect within a view, but I'm wondering if there's any way to do it solely inside urls.py.
You can use RedirectView. As long as the old and new url patterns have the same args and kwargs, you can use pattern_name to specify the url pattern to redirect to.
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^neworg/(?P<pk>\d+)/$', new_view, name='new_view'),
url(r'^org/(?P<pk>\d+)/$', RedirectView.as_view(pattern_name='new_view'), name='old_view')
]
Since you know, we can't use from django.views.generic.simple import redirect_to in Django 1.5. However we were using this kind of functions in our views.py:
return redirect_to(request, '/auth/login/')
I want to migrate from 1.4 to 1.5 but I couldn't figure out how to use RedirectView in views.py with request and url argument.
You can use redirect instead
Now you can simply change the redirect_to to
return redirect('/auth/login')
You can use Class based views or RedirectView
RedirectView helps you to redirect your url which works as like redirect_to. Both are applied in urls.py. But I couldn't find any solution to redirect from views.py.
Source: "No module named simple” error in Django