NoReverseMatch when trying to access view from template - django

I am getting a NoReverseMatch error for the following:
Hello
Where I passed a string fileids from my view to my template when rendering.
Reverse for 'generateCSV' with arguments '()' and keyword arguments
'{'file_ids': '11111111_22222222'}' not found. 1 pattern(s) tried:
['mainapp/secondaryapp/generateCSV/(?P[\d{8,20}]+\_*)/$']
My mainapp/urls.py is:
from django.conf.urls import url, include
from mainapp import views
urlpatterns = [
...
url(r'^secondaryapp/', include('secondaryapp.urls', namespace="secondaryapp")),
]
and secondaryapp/urls.py is:
from django.conf.urls import url
from search import views
urlpatterns = [
url(r'^$', views.secondaryapp.as_view(), name='secondaryapp'),
url(r'^generateCSV/(?P<file_ids>[\d{8,20}]+\_*)/$', views.generateCSV, name='generateCSV'),
]
generateCSV is a functional view, and secondaryapp is the name of the class-based view. I am rendering the template within the class based view, and attempting to call the functional view with the parameter fileids.
If I simply hard-code the url as follows:
<a href = "generateCSV/{{fileids}}/">
Everything works as expected.
Thanks very much for your time reading and for any help you can give me!

A friend of mine pointed out that my regex was wrong; if you use the following in secondaryapp/urls.py
url(r'^generateCSV/(?P<fileids>(?:\d{8,20}_)*\d{8,20}?)/$', views.generateCSV, name='generateCSV'),
with the following in secondaryapp/templates/secondaryapp/template.html
<a href = "{% url 'mainapp:secondaryapp:generateCSV' fileids %}">
then things work as expected.
Apparently, it was the square brackets that did me in when attempting to use them for grouping with special characters; according to him, they turn what are normally operators into regular characters, and potentially caused me to accept commas and braces as well.
According to this:
"Within square brackets, most characters are interpreted literally."

Related

Django URL with parameter not working with include

I am trying to pass a parameter to a url before my include statement. I think my problem should be similar to this one, but I can't figure out why mine isn't working. I get the error NoReverseMatch at /womens/ Reverse for 'shampoo' with arguments '('womens',)' not found. 1 pattern(s) tried: ['(?P<gender>womens|mens)/items/shampoo/$']
*config.urls*
urlpatterns = [
re_path(r'(?P<gender>womens|mens)/items/$, include('items.urls')),
...
]
*items.urls*
urlpatterns = [
path('shampoo', views.shampoo, name='shampoo'),
path('soap', views.soap, name='soap'),
...
]
{% url 'items:shampoo' gender='male' %}
I expect to get male/items/shampoo, but it doesn't work. Despite this, I can still manually go to male/items/shampoo.
This might work when you do it manually like male/items/shampoo, but as it is giving a NoReverseMatch error, Django is not able to find a matching URL pattern that you provided which included the parameters.
After looking into this for some time, something like this might be happening when you use {% url 'items:shampoo' gender='male' %}
Assuming you are running this on your localhost, you expect the URL to look like male/items/shampoo
This url says, look into male section, among items show shampoo.
But you are passing the parameter in the wrong place. {% url 'items:shampoo' gender='male' %} means that the parameter male is passed to a url which has a namespace of shampoo. The URL with the namespace of shampoo does not accept any parameters.
Maybe try changing your code as follows.
*config.urls*
urlpatterns = [
re_path(r'^$', include('items.urls')),
...
]
from django.conf.urls import url, path
*items.urls*
urlpatterns = [
url(r'^(?P<gender>\w+)/items/shampoo/$', views.shampoo, name='shampoo'),
path('soap', views.soap, name='soap'),
...
]

the current path ,About.html didn't match any of these in django

from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About/', views.About,name='About'),
url(r'^checkout/', views.checkout,name='checkout'),
url(r'^contact', views.contact,name='contact'),
url(r'^faqs', views.faqs,name='faqs'),
url(r'^help', views.help,name='help'),
url(r'^icons', views.icons,name='icons'),
url(r'^payment', views.payment,name='payment'),
url(r'^privacy', views.privacy,name='privacy'),
]
The error message:
Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/About.html
Using the URLconf defined in shop.urls, Django tried these URL patterns,
in this order:
admin/
^$ [name='index']
^about/$ [name='about']
^checkout/$ [name='checkout']
^contact/$ [name='contact']
^static\/(?P<path>.*)$
The current path, About.html, didn't match any of these.
This kind of an error could occur from 2 or 3 different scenarios.
In your case, you seem to put the wrong URL in the browser address bar.
Your correct URL should be http://127.0.0.1:8000/About (as you've written in the URL patterns).
Remember, About.html - is the HTML template you create inside the templates folder. Even though you route to the html page (with a string like: app_name/About.html) - the actual URL in the address bar will be according to what you write in the regex path r'^url_name'. If you write r'^About.html' in url patterns, then http://127.0.0.1:8000/About.html should work perfectly.
The second scenario (based on my experience) which could produce this type of an error is when you forget to pass the 'request' argument inside the method that defines view of the URL - in the respective views.py file.
You should be having a method named About which would look like this in views.py
def About(request):
return render(request,'app_name/About.html')
If you forget to pass argument in the paranthesis of About, this kind of an error could occur.
Finally, if you are using django 2, please start using re_path method to serve regex url patterns. The url method is likely to be depracated in future release.
Refer re_path documentation.
your URL will not be http://127.0.0.1:8000/About.html it will just be http://127.0.0.1:8000/about (remember urls are case insensitive), this will take you to your view which is named About, in your view you should reference your template in its render (about.html)
have you read the my first Django app https://docs.djangoproject.com/en/2.0/intro/tutorial01/ its a great place to start if you are unfamiliar with how django operates
What you are trying to hit is not a valid url, you have to hit http://127.0.0.1:8000/About as written in urls.py.
You have to understand the difference between urls and html templates, this About.html would be used in views while rendering like:
return render(request, 'your_app/about.html')
And for sure you can write a url if you want like this:
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About.html/', views.About,name='About'),
.
.
]
Check the documentation
The url which you provide in url ( ) method doesn't contain any suffix of .html
You can goto the about page directly by /About

How do I reference Django 1.5 URLs that are "named" in an included URL file?

I have my project's main urls.py file with the following:
url(r'^accounts/$', include('accounts.urls'),
In accounts.urls.py, I have the following:
urlpatterns = patterns('accounts.views',
url(r'^profile/$', 'accounts_profile', name='accounts_profile'),
In my base.html template, I have:
<li>Profile</li>
This results in a ReverseNotFound error:
NoReverseMatch at /
Reverse for 'accounts_profile' with arguments '()' and keyword arguments '{}' not found.
If I move the accounts_profile url definition to the main urls.py, the URL works. I remember this style of URL organization working in a prior Django version; has something changed, or am I doing something wrong?
Get rid of the $ in your url(r'^accounts/$', include('accounts.urls'), call.
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash.
https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs

Use Django's url template tag with special characters

I'm using django's url tag to move from one view to another:
<a href = {% url wiki_view item %}>Read more here</a>
For most "items" this works perfectly. But I have an "item" with a / character: Sci-Fi/Fantasy. In this instance, I get an error
Caught NoReverseMatch while rendering: Reverse for 'wiki_view' with arguments '(u'Sci-fi/Fantasy',)' and keyword arguments '{}' not found.
My urls.py is defined as such:
url(r'^wiki/page/(?P<page_title>[^/]*)/$', views.wiki_view, name = 'wiki_view'),
Is there a way for this to work with a "/" character in place like this?
Use the urlencode filter as Ignacio suggests, however you are still getting a probelm as by default that filter assumes a / to be left alone. You can solve this (if you are using the dev version currently) by using the filter in the following way:
item|urlencode:""
This is explained in the Django docs.
Pass the argument through urlencode first.
You should be using Djanog's slugify on anything which you are using in a URL. It replaces characters like that with URL friendly ones, and human readable alternatives.
from django.template.defaultfilters import slugify

django 'url' template tag error

My URLconf contains this pattern:
url(r'^accounts/logout/$','django.contrib.auth.views.logout', name="logout"),
And I've trying to reverse that in a template with the URL tag like this:
logout
But I keep getting the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{'next_page': u'/first-page/child/'}' not found
I thought django.contrib.auth.views.logout is supposed to take an option next_page parameter. I'm sure I'm missing something obvious, but I'm not sure what it is.
Yes you're right, django.contrib.auth.views.logout does accept an optional "next_page" parameter, but don't forget that the "url" tag matches to urlconf patterns, not views, so it's not aware of what is or isn't a parameter of a view. So this suggests that you need to make "next_page" a named group in the regexp for the above pattern, which you could do, but there's an easier way to handle redirects...
Looking at django.contrib.auth.views.logout, you can see that in the absence of a "next_page" parameter, the view redirects to whatever url is provided in either request.GET or request.POST with the key "redirect_field_name", a parameter which defaults to "REDIRECT_FIELD_NAME" which in turn defaults to the string "next". So leaving your urlconf the way it is, you can do something like this in your template:
<a href='{% url logout %}?next={{ request.path }}'>logout</a>
Basically Django's URL dispatcher is looking at the urlconf and that argument and saying "I don't know where to put this argument" because it doesn't look at the view functions the urls point to, only the urlconf and the patterns in it.
Right now there's no place in your url pattern for that argument.
i.e. you can call django.contrib.auth.views.logout with the extra arguments if you write your own pattern for it or if you call it from your own view, but not from its default url pattern.
One of these url patterns might work for you (not tested):
url(r'^accounts/logout/(?P<next_page>.*)?$','django.contrib.auth.views.logout', name="logout"),
url(r'^accounts/logout/$','django.contrib.auth.views.logout', kwargs={'next_page':None}, name="logout"),
Hope that helps!