Problems with {% URL %} tag - django

I've been trying to implement hyper links into my Django application, where a list of items are displayed, clicking on each item will take you to a page detailing more information about the item.
I've been wrestling with the {% URL %} tag and despite searching over here, the internet and books on the matter, I've yet to get it working.
In views.py:
def Link(request):
return render_to_response('Search_Page.html')
In Urls.py:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ParkManager.views.home', name='home'),
# url(r'^ParkManager/', include('ParkManager.foo.urls')),
url(r'^test/', Search_Page),
url(r'^search/', Search),
url(r'^details/', Details_Main),
url(r'^Link/(d+}/$', Link),
url(r'^$', 'Parks.views.Link', name="home"),
in my template:
test
Thanks for your time :)
EDIT
error:
The page loads however the link only takes you to 127 .0 .0 .1 /8000
when I add: test
I get:
NoReverseMatch at /search/
Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.
Request Method:
GET
Request URL:
http://127.0.0.1:8000/search/?search=a&type=parks&submit=Search
Django Version:
1.4.2
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.
Exception Location:
C:\Python27\lib\site-packages\django\template\defaulttags.py in render, line 424
Python Executable:
C:\Python27\python.exe
Python Version:
2.7.3
Python Path:
['C:\\Users\\User\\Documents\\Django\\ParkManager',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time:
Mon, 4 Feb 2013 16:05:30 +0000
Error during template rendering
In template C:\Users\User\Documents\Django\ParkManager\Templates\Details_Main.html, error at line 23
Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.

A clue:
Exception Location:
C:\Python27\lib\re.py in _compile, line 242
Your issue is not related to the url tag. It is a mal-formed regex in your urls.py.
urls.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ParkManager.views.home', name='home'),
# url(r'^ParkManager/', include('ParkManager.foo.urls')),
url(r'^test/', Search_Page),
url(r'^search/', Search),
url(r'^details/', Details_Main),
# LINE BELOW has an open parentheses and not a closed parentheses.
url(r'^Link/(d+}/$', Link),
#url(r'^Link/(d+)/$', Link), #line fixed
url(r'^$', 'Parks.views.Link', name="home"),

unbalanced parenthesis is problem at this line:
url(r'^Link/(d+}/$', Link),
You have forgotten to close the parenthesis.
If you are using {% url %} tag in Django < 1.5, use it this way:
{% load url from future %}
{% url 'namespace:viewname' arg1, arg2 %}
{% url 'namespace:viewname' kwarg1=val, kwarg2=val2 %}
If you are using Django 1.5, you don't have to load the special url tag. If you are not using namespaces (good if you are using general view names like list, detail etc. and you want to distinguish between apps, e.g.: author:list or book:list) use only the view name. Check the documentation, there is a good section about the url tag - https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

Related

NoReverseMatch at /auth/password/reset/

I am using dj-rest-auth package to do auth related functions, installed the package and added to installed apps and included it in main url as follows
path('auth/', include('dj_rest_auth.urls')),
it works for login, logout etc, but when I do password reset it throws the error
http://127.0.0.1:8000/auth/password/reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name
NoReverseMatch at /auth/password/reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/password/reset/
Django Version: 3.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Exception Location: /home/biju/Desktop/reporting-system/lib/python3.8/site-packages/django/urls/resolvers.py, line 694, in _reverse_with_prefix
Python Executable: /home/biju/Desktop/reporting-system/bin/python
Python Version: 3.8.10
Python Path:
['/home/biju/Desktop/reporting-system',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/biju/Desktop/reporting-system/lib/python3.8/site-packages']
Server time: Fri, 01 Apr 2022 16:01:18 +0000
the urlpattern in https://github.com/iMerica/dj-rest-auth/blob/master/dj_rest_auth/urls.py:
...
path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'),
...
so you need to change 'password_reset_confirm' to 'rest_password_reset_confirm' in your html url tag or in django function call like reverse()
update:
see https://dj-rest-auth.readthedocs.io/en/latest/faq.html
"You need to add password_reset_confirm url into your urls.py (at the top of any other included urls). Please check the urls.py module inside demo app example for more details."
the package expects something like:
path('password/reset/confirm/<uidb64>/<token>',TemplateView.as_view(template_name="password_reset_confirm.html"),
name='password_reset_confirm'),
path('auth/', include('dj_rest_auth.urls')),
see the demo urls.py: https://github.com/iMerica/dj-rest-auth/blob/24678437b3cdf3fa663880ab66a42aa0992b1d39/demo/demo/urls.py
strange for me that it is mentioned only in the FAQ - looks to me like a bug in the confirm email generation where they use the wrong urlpattern name without the "rest" prefix
.

Stuck two days on this NoReverseMatch thing

Coded in Pycharm
Django version 3.2.6
Project hw_v2
app
aftersales
...
Templates
Project URL
urlpatterns = [
path('', include(('index.urls', 'index'), namespace='index')),
path('admin/', admin.site.urls),
path('mkt/', include(('marketing.urls', 'marketing'), namespace='mkt')),
path('com/', include(('commercial.urls', 'comercial'), namespace='com')),
path('aft/', include(('aftersales.urls', 'aft'), namespace='aft')),
path('gen/', include(('general.urls', 'general'), namespace='gen')),
path('user/',include(('user.urls','user'),namespace='user')),
path('bootstrapTable/',include('bootstrapTable.urls')),]
App/aftersales URL:
urlpatterns = [
path('', views.aft,name='afti'),
path('edit/<int:id>',views.aft_edit,name='aft_edit'),
path('view/<int:id>',views.aft_view,name='aft_view'),]
Error Page HTML:
<td>
</td>
Error:
NoReverseMatch at /aft/view/1
Reverse for 'aft' not found. 'aft' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/aft/view/1
Django Version: 3.2.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'aft' not found. 'aft' is not a valid view function or pattern name.
Exception Location: D:\0 PROJECTS\14 django_website_ali\hw_v2\venv\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: D:\0 PROJECTS\14 django_website_ali\hw_v2\venv\Scripts\python.exe
Python Version: 3.9.2
Python Path: [WindowsPath('D:/0 PROJECTS/14 django_website_ali/hw_v2'),
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2\\app',
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2',
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2',
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2\\app',
'D:\\Program Files\\JetBrains\\PyCharm '
'2020.1\\plugins\\python\\helpers\\pycharm_display',
'D:\\Program Files\\Python39\\python39.zip',
'D:\\Program Files\\Python39\\DLLs',
'D:\\Program Files\\Python39\\lib',
'D:\\Program Files\\Python39',
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2\\venv',
'D:\\0 PROJECTS\\14 django_website_ali\\hw_v2\\venv\\lib\\site-packages',
'D:\\Program Files\\JetBrains\\PyCharm '
'2020.1\\plugins\\python\\helpers\\pycharm_matplotlib_backend']
Server time: Fri, 20 Aug 2021 09:01:56 +0800
app/aftersales/views:
def aft_edit(request, id):
item = FFREntry.objects.get(pk=id)
if request.method == "GET":
print(item)
return render(request, 'aft_edit.html', locals())
elif request.method == "POST":
keys = ['engineer', 'date', 'complaint', 'client', 'e_code', 'inv_type', 'inv_model', 'sn', 'region', 'state',
'f_type', 'f_type_d', 'solution', 'is_warr', 'compl', 'remark', 'img', 'video'
]
for key in keys:
if request.POST.get(key):
dict={key:request.POST.get(key)}
FFREntry.objects.filter(id=id).update(**dict)
FFREntry.save
return redirect('aft_view',id=id)
def aft_view(request, id):
item = FFREntry.objects.get(pk=id)
return render(request, 'aft_view.html', locals())
I've been stuck here for two days.
The error came out of nowhere.
One sec ago, it was all ok. Then, the error jumped out.
I am not a programmer. Coding is just a hobby. I have no one I can consult with.
So please please please, save me.
LATEST UPDATES
The project is working now. It is totally creepy and strange. Below is what I did:
In #app/aftersales/url.py
path('', views.aft,name='aft'),
path('aft/edit/<int:id>',views.aft_edit,name='aft_edit'),
path('view/<int:id>',views.aft_view,name='aft_view'),
]
I added 'aft/' in the second path(). Totally out of desperation.
obviously, it's wrong. And error popped.
I deleted the 'aft/'.
Then everything goes back to normal.
I have no idea what happened.
Thanks to everyone who gave me advice! Deeply!
Without seeing your views I can't be sure, but the issue is likely in your template due to the way arguments are being passed into your template tag.
<td>
</td>
Remove the key / equals sign and simply pass the argument value. Assuming the context you're passing the template includes itm this should render the links correctly and the reverse match error will go away.
Before: id=itm.id
After: itm.id
Note that I tested this on one of my own templates by adding the arguments as key value pairs like in your example and got the same error.

Reverse for 'filer_folder_changelist' not found

I am deploying site to divio server with django and django-cms. I do R&D but not found much information about this.
Facing Error: Reverse for 'filer_folder_changelist' not found. 'filer_folder_changelist' is not a valid view function or pattern name.
Exception Type: NoReverseMatch Exception Value: Reverse for
'filer_folder_changelist' not found. 'filer_folder_changelist' is not
a valid view function or pattern name. Exception
Location: /virtualenv/lib/python3.5/site-packages/django/urls/resolvers.py
in _reverse_with_prefix, line 497 Python
Executable: /virtualenv/bin/uwsgi
{% cms_toolbar %}
**urls.py:**
] + aldryn_addons.urls.patterns() + i18n_patterns(
# test urls
url(r'^admin-cms/', include(admin.site.urls)), # NOQA
url(r'^django-cms/', include('cms.urls')),
url(r'^django-cms1/',include('aldryn_django_cms.urls')),
url(r'^test4/$',views.test4,),
# ends here ~ test urls
# add your own i18n patterns here
*aldryn_addons.urls.i18n_patterns() # MUST be the last entry!
)
cms_toolbar.py
def populate(self):
media_library = _('Media library')
admin_menu = self.toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER)
admin_menu.add_sideframe_item(
media_library,
url=reverse('admin:filer_folder_changelist'),
position=self.get_insert_position(admin_menu, media_library)
)
Did some research and found this:
Need to install django-admin-shortcuts
Add shortcut to your settings.py:
ADMIN_SHORTCUTS = [
{
'shortcuts': [
{
'url_name': 'admin:filer_folder_changelist',
'title': _('Files'),
},
]
},
]
You can check out similar implementation in here. Hope it helps!!
I fixed this error by change settings.py.. Add below things in installed app in settings.py:
INSTALLED_APPS.extend([
.....
'djangocms_text_ckeditor',
'filer',
'easy_thumbnails',
'djangocms_column',
'djangocms_file',
'djangocms_link',
'djangocms_picture',
'djangocms_style',
'djangocms_snippet',
'djangocms_googlemap',
'djangocms_video',
.....
)]

Django include url causes rendering error?

I have a working django project. I wrote a small app - pm - and I tried to include its urls.py in the active project:
urlpatterns = patterns('',
# ... some urls here
url(r'^$', views.home, name='vw_home'),
# I added the following line:
(r'^pm/', include('pm.urls')),
Once I access the main web page, I receive the following error:
TemplateSyntaxError at /
Caught error while rendering: syntax error
and the debug shows the problem in the following line:
Home
If I remove the last url pattern (the include()), the page renders without any problem.
How can this be fixed?
EDIT:
Adding the urls.py of the pm app:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns("pm.views",
url(r'^inbox/$', 'inbox', {'folder': 'inbox'}, name='vw_inbox'),
url(r'^sent/$', 'inbox', {'folder': 'sent'}, name='vw_sent'),
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
url(r'^compose/(?P<profile_id>\w+)/$', 'compose_message', name='vw_compose_message'),
url(r'^reply/(?P<message_id>\w+)/$', 'compose_message', name='vw_reply_message'),
)
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
You missed ?P

Django-grappelli admin: No reverse match error

I've been working on a django project for a while now that uses grappelli for the admin and all of a sudden today my change_form.html template is throwing the following error:
Caught NoReverseMatch while rendering: Reverse for "grp_related_lookup" with arguments '()' and keyword arguments '{}' not found.
The offending line of code is line 38:
37 $.each(related_lookup_fields_fk, function() {
38 $("#id_" + this).grp_related_fk({lookup_url:"{% url grp_related_lookup %}"});
39 });
which is preceded by this bit of code:
var related_lookup_fields_fk = {% get_related_lookup_fields_fk adminform.model_admin %};
Obviously it's the {% url grp_related_lookup %} bit that's causing the problem.
I don't understand how the template is resolving grp_related_lookup to grappelli.views.related.related_lookup. I have tried replacing grp_related_lookup with grappelli.views.related.related_lookup and that didn't work either. Also, in the template the offending line looks like this:
$("#id_" + this).grp_related_fk({lookup_url:"{% url grp_related_lookup %}"});
but in the error message it looks like this:
$("#id_" + this).grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
I don't know if the single quotes surrounding grp_related_lookup might have something to do with the problem or not. Is that how django rendered the function call? Is it passing the string 'grp_related_lookup' to the url template tag? If so, what might have caused this to break suddenly?
Some additional info:
The value of related_lookup_fields is an empty list []. I am not defining any related_lookup_fields in my admin.py.
I threw a couple debug statements into the grappelli.views.related.related_lookup view function and it doesn't appear to be getting called.
I have not touched any of the templates recently.
Hopefully someone can point me in the right direction... Thanks!
Do you still have 'grappelli.urls' included in your URLconf? That the only reason I see that would cause this error. You can try using python manage.py shell:
from django.core.urlresolvers import reverse
print reverse('grp_related_lookup')
If this line returns the correct URL, you shouldn't get a NoReverseMatch in your template.
The quotes around grp_related_lookup shouldn't be a concern. The {% url %} tag accepts both quoted and unquoted strings as first argument, so django normalizes it to quoted strings. This behaviour is going to change in the future: you'll be able to pass template variables to {% url %} using unquoted strings. {% url foo %} and {% url "foo" %} won't give the same result, see the 1.3 release notes for details about this.
I encountered the same behavior with Django 1.5 and Grappelli 2.4.4.
To fix the problem I had to add
url(r'^grappelli/', include('grappelli.urls')),
to urlpatterns.
I faced with this problem today, when I tried to delete data in admin.Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': ''}' not found.
I have put the url(r'^grappelli/', include('grappelli.urls')) in urls.py
The solution is pretty strange: just update the grappelli to the latest version. (I updated it from 2.5.6 to 2.6.3)
I faced this problem yesterday. The Django-grapelli I used was the one that was included in the FileBrowser installation. I solved the problem by upgrading Django-grapelli. Just type:
pip install --upgrade django-grappelli
I had a similar issue with urls and noticed that I need
{% load url from future %}
in the template if I want to have quoted url tags. That's also mentioned in the official django documentation: https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url
I seem to be encountering this same issue, but when I run the suggested console test I get this:
Python 2.7.9 (default, Apr 7 2015, 07:58:25)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> print reverse('grp_related_lookup')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/tsantor/.virtualenvs/project_env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 579, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/tsantor/.virtualenvs/project_env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 496, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'grp_related_lookup' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
And my urls.py looks like this:
urlpatterns = patterns(
# Admin
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls), name="admin"),
# main views
#url(r'^$', RedirectView.as_view(url='/admin'), name='home'),
# API
url(r'^api/', include('api.urls', namespace='api')),
)
I also have the latest Grappelli (2.6.4) running on Django (1.8.2). By the way, it seems it only occurs when I try to access and add or edit view. The control panel and list views work.
I added
path('grappelli/', include('grappelli.urls')),
and fixed the problem.