Django routing - The empty path did not match any of these - python

Using the URLconf defined in pccb_model.urls, Django tried these URL patterns, in this order:
predictive/
admin/
The empty path didn't match any of these.

That's correct, you need to define a view for a default path in your pccb_model.urls module. Make sure you have a corresponding method in the views.py. For example:
urls.py
path('', views.index)
views.py
def index(request):
return HttpResponse('test', status=200)

Related

Django unable to render my template because it is unable to find my url. This only happens with url patterns from one of my apps

Django is unable to load my template because "NoReverseMatch at /books/outlines/20
"
This issue lies within a link in the template:
New Blank Outline
Here is my outlines/urls.py
from django.urls import path
from .views import Dashboard
from . import views as outline_views
urlpatterns = [
path('<int:pk>/', outlines_views.outline, name='url-outline')
path('blank/<int:storyPk>', outline_views.newBlankOutline, name='url-blankOutline'),
path('test/', outline_views.testView, name = 'test')
]
urlpatterns += staticfiles_urlpatterns()
Here is the testView:
def testView(request):
return render(request, 'outlines/test.html')
Here is the outline view:
def outline(request, pk):
context = {
'storyPk': pk
}
return render(request, 'outlines/outline.html', context)
The django error tells me:
NoReverseMatch at /books/outlines/20
Reverse for 'test' not found. 'test' is not a valid view function or pattern name.
The weird thing is, if I change the url name in the template to a url name from another app's urls.py file, it renders the template no problem. Any idea why it can't find the url?
New Blank Outline
how about try this

The current path, customer, didn't match any of these. even when all other path runs correctly

ERROR :Using the URLconf defined in crm1.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
products/ [name='products']
customer/<str:pk_test>/ [name='customer']
create_order/<str:pk>/ [name='create_order']
update_order/<str:pk>/ [name='update_order']
delete_order/<str:pk>/ [name='delete_order']
The current path, customer, didn't match any of these.
even when i run this.....accounts/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home, name='home'),
path('products/',views.products, name='products'),
path('customer/<str:pk_test>/',views.customer,name='customer'),
path('create_order/<str:pk>/',views.createOrder,name='create_order'),
path('update_order/<str:pk>/',views.updateOrder,name='update_order'),
path('delete_order/<str:pk>/',views.deleteOrder,name='delete_order'),
]
all path are running correctly and even when i run http://127.0.0.1:8000/customer/2
it runs properly....but when i run http://127.0.0.1:8000/customer/ error out
actually
/<str:pk_test>/
kind of path create problem i dont know
Well there is not route for customer only. So, add this line in your urls.py:
path('customer/',views.customer,name='customer-only'),
*Note:- Add the line above the another code. Like:-
path('customer/',views.customer,name='customer-only'),
path('customer/<str:pk_test>/',views.customer,name='customer'),
Okay, as your views.py needs argument, you have to do:
def customer(request, pk_test=None):
customer=Customer.objects.get(id=pk_test)
orders=customer.order_set.all()
order_count =orders.count()
context={'customer':customer,'orders':orders,'order_count':order_count}
return render(request,'accounts/customer.html',context)

How to redirect url pattern with variables from urls.py in Django?

I'd like to redirect url pattern with variables from urls.py.
I refer other stackoverflow solution, but I don't know when url having a variable like following code.
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns(
url(
r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(url='/permalink/(?P<id>\d+)/')
),
)
With this code, django will redirect /permalink/1/foo/ to /permalink/(?P<id>\d+)/, not the /permalink/1/.
Is there any solution without using views.py?
Of course I know solution using controller, but I wonder is there any simpler solution with using url pattern.
Passing url='/permalink/(?P<id>\d+)/' to RedirectView will not work, because the view does not substitute the named arguments in the url.
However, RedirectView lets you provide the pattern_name instead of the url to redirect to. The url is reversed using the same args and kwargs that were passed for the original view.
This will work in your case, because both url patterns have one named argument, id.
urlpatterns = [
url(r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(pattern_name="target_view"),
name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]
If the target url pattern uses other arguments, then you can't use url or pattern_name. Instead, you can subclass RedirectView and override get_redirect_url.
from django.core.urlresolvers import reverse
from django.views.generic import RedirectView
class QuerystringRedirect(RedirectView):
"""
Used to redirect to remove GET parameters from url
e.g. /permalink/?id=10 to /permalink/10/
"""
def get_redirect_url(self):
if 'id' in self.request.GET:
return reverse('target_view', args=(self.request.GET['id'],))
else:
raise Http404()
It would be good practice to put QuerystringRedirect in your views module. You would then add the view to your url patterns with something like:
urlpatterns = [
url(r'^permalink/$', views.QuerystringRedirect.as_view(), name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]

Django Tutorial "Write Views That Actually Do Something"

I've been working on the Django tutorial. I'm on the part where it is "Write Views That Actually Do something." (Part 3)
I'm trying to use the index.html template that it gives you, but I keep getting a 404 error that says
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>\d+)/$ [name='detail']
^polls/ ^(?P<question_id>\d+)/results/$ [name='results']
^polls/ ^(?P<question_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/index.html, didn't match any of these.
I don't know if one of the regex are wrong? I've been messing around with it for a while now and I have had no luck getting it to work.
I can go to /polls just fine. But /polls/index.html does not work.
Any help would be appreciated.
The version of Django that I'm using is 1.7.4
Django view functions or classes use the template you define, so that you do not have to specify it in the URL. The urls.py file matches your defined regex to send requests to views.
If you truly wanted to use that URL, you would have to define ^polls/index.html$ in your urls.py and direct it to your view.
From what you're asking it sounds like you essentially want to output a static html file on a URL defined in your urlpatterns in urls.py.
I strongly suggest you take a look at Class Based Views.
https://docs.djangoproject.com/en/1.7/topics/class-based-views/#simple-usage-in-your-urlconf
The quickest way to go from what you've got, to rendering polls/index.html would be something like;
# some_app/urls.py
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^polls/index.html', TemplateView.as_view(template_name="index.html")),
)
But I'm sure you'll want to pass things to the template so class based views will be what you need. So the alternative to the above with added context would be;
# some_app/views.py
from django.views.generic import TemplateView
class Index(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(Index, self).get_context_data(**kwargs)
context['foo'] = 'bar'
return context
Then obviously adding {{ foo }} to your index.html would output bar to the user. And you'd update your urls.py to;
# some_app/urls.py
from django.conf.urls import patterns
from .views import Index
urlpatterns = patterns(
'',
(r'^polls/index.html', Index.as_view()),
)

Handling Multiple Views/ Multiple Urls in Django

I'm having the 404 error when trying to handle a view that is not related to the main page. For example, if I initially start at the main page home, and want to navigate to another page called, otherpage, I receive a 404 otherpage.html not found.
The way I'm doing is based off intuition. So if there's a better way to do this, please mention it:
in the file:
prd/
views.py
url.py
otherstuffthatshouldbehere.py..
I have views.py (this is where I think the error is):
from django.shortcuts import render
def home(request):
context = {}
template = "index.html"
return render(request, template, context)
def otherpage(request):
context = {}
template = "otherpage.html"
return render(request, template, context)
Then urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
url(r'^admin/', include(admin.site.urls)),
)
This returns a 404 for the otherpage.html. I have the template directory working fine. But how do I go about handling multiple views?
EDIT:
Upon adding:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
I received this error:
Using the URLconf defined in prd.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^about$ [name='about']
^projects$ [name='projects']
^admin/
The current URL, about.html, didn't match any of these.
The urlpatterns starts at the top and then goes down until it matches a URL based on the regex. In order for Django to serve up the page located at otherpage.html there has to be a URL defined in urlpatterns that matches otherpage.html otherwise you will get the 404.
In this case you have:
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
Note that nothing will ever get to otherpage here because home has the same pattern (regex) and will therefore always match first. You want to have a different URL for both those so that you can differentiate between them. Perhaps you could do something like:
url(r'^$', 'prd.views.home', name='home'),
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
After making this change you now have a match for otherpage and hopefully no more 404.
EDIT:
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
This matches www.example.com/otherpage.html but it will not match www.example.com/otherpage
To match www.example.com/otherpage you need to use:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
Note the difference in the regex, there's no .html here. The regex matches exactly what you put in it.

Categories

Resources