Generic Views NoReverseMatch when using {% url %} in Django - python

I have a problem to do the reverse command 'url' from the template base.html.
URLS.conf my file looks like this:
dic_info_artigo = {
'queryset': Artigo.modificado.all(),
'date_field': 'data_pub',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$', 'archive_index', dic_info_artigo,'artigos'),
(r'^(?P<year>\d{4})/$','archive_year', dic_info_artigo,'artigos_ano'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
'archive_month', dic_info_artigo,'artigos_mes'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
'archive_day', dic_info_artigo,'artigos_dia'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
'object_detail', dic_info_artigo,'detalhe_artigo'),
)
base.html
<a href="{% url artigos %}"> Artigos </ a>
The error:
dictionary update sequence element # 0 has length 1; 2 is required
Already tried using the parameter 'name=', i change the value , but it did not work
url(r'^$', 'archive_index', dic_info_artigo, name='artigos'),
What am I doing wrong? Any tips?
Thanks.

The error message suggests that you have tried to name a view using something like:
(r'^my_url$', 'my_view', 'my_view')
However, the third argument should be a dictionary, not the name of a view.
To prevent errors like this, I recommend always using the url shortcut and naming the url pattern:
url(r'^my_url$', 'my_view', name='my_view')
However, you could pass an empty dictionary as the third argument if prefer:
(r'^my_url$', 'my_view', {}, 'my_view')
The urls.py you have posted looks ok, so the problem is probably in a different urls.py. If you're lucky, the full traceback might give you the exact line of the module where the error is occurring.

Use url() to name the urls and try the following in template file.
{% url 'artigos' %}

Related

Unable to pass a view function with two arguments in Django resulting in No Reverse Match Error

I am unable to understand why am I getting this error. If someone can please explain me why and how I can fix it, I am hoping that it will correct the underlying misconceptions that I probably have about Django views and urls.
django.urls.exceptions.NoReverseMatch: Reverse for 'download' with keyword arguments '{'name': 'Dr. XYZ', 'date': '14 July 2020'}' not found. 1 pattern(s) tried: ['download/$']
I have a function called download in my views.py which takes two arguments: name and date
Views.py
def download (request, name, date):
x = date.split(" ")
date = f"29 {x[3]} {x[4]}"
image = Image.open("certificates\static\certificates\Certificate_0001.jpg")
font_type = ImageFont.truetype('arial.ttf', 70)
font_type_2 = ImageFont.truetype('arial.ttf', 35)
draw = ImageDraw.Draw(image)
draw.text(xy=(810, 740), text=name, fill=(0,102,0), font=font_type)
draw.text (xy=(330, 1230), text=date, fill=(0,102,0), font=font_type_2)
image.save(f"certificates\static\certificates\{name}.pdf", "PDF", resolution=100.0)
return HttpResponse ("works")
I am calling this function upon a link click in my html file.
<a class="nav-link active" href="{% url 'certificates:download' name=name date=program %}" download>Download Certificate</a>
The url configuration is:
urls.py
from django.urls import path
from . import views
app_name = "certificates"
urlpatterns = [
path("", views.index, name="index"),
path("download", views.download, name="download")
]
It is confirmed that there is no syntax or other logical error in my function. If I remove the two arguments in my html file then there is no error i.e. if I say html href="{% url 'certificates:download' %} . However, if I bring the two arguments name and date I get this NoReverseMatch error.
Please explain what is wrong?
Link you have in <a ... > tag goes to 'certificates/download/somename/somedate'. i.e you passing arguments 'somename' and 'somedate'
So you should add that parametres in your url configurations
path("download/<str:name>/<str:date>", views.download, name="download")
And then you taking that parametres in your view function
def download (request, name, date):
let me know if it worked
You need to add these arguments to your urls.py file as well, i.e.:
path("download/<str:name>/<str:date>/", views.download, name="download"),
However, I'm not sure about the formatting of your date, so you might need to change that part a bit.
You need to specify the function parameters in your URL definition as such
path("download/<str:name>/<str:date>", name="download")
That way the reverse function can resolve your function with the correct parameters.

Rendering dynamic variable in a django template

I am trying to render dynamic Url in Django template as follows
<a href={{'jibambe_site_detail/'|add: site.id}}>{{ site }}</a>. This however is returning a TemplateSyntaxError at /jibambe_sites/
add requires 2 arguments, 1 provided. What am I missing or how should I render this dynamic URL, I want it to produce something like jibambe_site_detail/1
OP's strategy changed on this one but OP's original error seems to be because of the space after colon.
Use add:site.id instead of add: site.id
See a similar question about default here
From #schwobaseggl comment, I was able to add a dynamic url variable as follows <a href={% url 'site_details' pk=site.id %}>{{ site.name }}</a>
then in the urls.py urlpatterns, I gave the path to jibambe_site_details a name path('jibambe_site_detail/<slug:pk>', JibambeSitesDetails.as_view(), name='site_details'),

Django: Reverse for 'delete' with arguments '(49,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['tidbit/delete_tidbit/']

Can't figure out how to solve this error
Here is the urls.py snippet:
urlpatterns = [
...
url(r'^delete_tidbit/', views.delete_tidbit, name='delete'),
...
]
The view:
def delete_tidbit(request, pk):
tidbit = Tidbit.objects.get(pk=pk)
tidbit.delete()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
And the portion of the template that raises this error:
<a href="{% url 'delete' tidbit.pk %}">
The issue is here:
url(r'^delete_tidbit/', views.delete_tidbit, name='delete'),
This URL doesn't accept an argument, where as you are trying to give it one.
Try this instead:
url(r'^delete_tidbit/(?P<pk>.*)', views.delete_tidbit, name='delete'),
But beware: you are accepting GET requests to delete items in your database, any crawler coming across those links may try to follow them and inadvertently delete your data. Consider using a GET that delivers a form to be POSTed to ensure an actual user is doing the action.

URL trouble to pass parameter

This link can not pass parameter to view.py
profile
It gives an error page not found, 127.0.0.1:8000/profile/edit/
There is not parameter there, even {{costumer.slug}} returns a string
Rest of template has no porblem to pass a parameter like this:
{{j.title}}
What can be wrong here?
Your problem is that you are missing a leading slash, so the browser is concatenating the URL with the one you're already on (you're on '/profile', you click 'edit', you go to '/profile/edit').
But you shouldn't be building up URLs like that. You should use the url tag. Assuming your URLconf is this:
url(r'^edit/(?P<slug>\w+)/$', 'profile.views.edit_profile', name='edit_profile')
you would do this in the template:
<a href="{% url 'edit_profile' slug=costumer.slug %}">

Django url template confusion

Okay I am having a bit of an issue.
I want to create a button with a link, and right now I am using action={% url views.contest_overview %} in hopes that the reverse lookup by Django will match (r'^category/$', views.contest_overview), in my urls.py. However, this is not working and I can't figure out the proper nomenclature, despite numerous guesses.
The error I get (with my best guess above) is:
Caught NoReverseMatch while rendering: Reverse for
'views.contest_overview' with arguments '()' and keyword arguments
'{}' not found.
Thank you very much for your time!
Use the application name in the url tag, e.g. {% url myapp.views.contest_overview %}
This is what I usually do; I give names to my url. For example:
url(r'^account/register/$', 'someapp.views.register_view', name='account_register'),
Therefore in template, I can do this:
{% url account_register as url_acc_register %}
<html>
..
..
Some link

Categories

Resources