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 %}">
Related
How can I send a URL from a template to a view? I have noticed that a normal string works but a URL doesn't. Here is what I have tried.
path('imageviewer/<str:theimage>', mainviews.imageviewer, name="imageviewer"),
def imageviewer(request, theimage):
response = render(request, "imageviewer.html", {"theimage": theimage})
return response
How I attempt to pass it : (value.image) is a url
<a href="{% url 'imageviewer' theimage=value.image %}" class="effect-lily tm-post-link tm-pt-60">
Error I Get:
Reverse for 'imageviewer' with keyword arguments '{'theimage': 'https://storage.googleapis.com/katakata-cb1db.appspot.com/images/humours/1643758561'}' not found. 1 pattern(s) tried: ['imageviewer/(?P<theimage>[^/]+)\\Z']
Thank you.
You need to escape the image so that it can be used in a URL, use the built-in filter urlencode
{% url 'imageviewer' theimage=value.image|urlencode %}
Your urlpattern also needs to accept slashes and other chars, use the path converter instead of str as it accepts any non-empty string
path('imageviewer/<path:theimage>', mainviews.imageviewer, name="imageviewer"),
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'),
I'm a begginer grasping at straws with difficulty dealing with the django slug url system and these NoReverseMatch errors that make no sense to me even after reading the docs.
I have a django project. In one of the views, I pass a list of geoJSON features into a template, and show them on a map. I want to have each feature act as a clickable 'link' to a view that will show stuff about it. The following is part of the template that has those features that I want to click on:
//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
});
}
(I have confirmed that feature.properties.myslug does in fact contain the slug I want).
The url pattern I want to go to:
urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]
And the view it relates to:
def AreaDetail(request, areaslug):
area = get_object_or_404(Area, nameslug=areaslug)
return render(request, 'polls/areadetail.html', {'area':area})
The issue I get is, by doing what I show and placing that url reference inside that template I show above, that I want to be able click on, that template won't even work at all, giving me a 'Error during template rendering' full page error info that starts with:
NoReverseMatch at /polls/areas/
Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']
Any help would be immensely appreciated
EDIT part1: As I've said in response to falsetru, I'm sure feature.properties.myslug has in fact got a slug expression in it.
EDIT2: Based on something I found in a django ticket, I've made a slight change in the url regex at urls.py, from (?P<areaslug>[-\w]+)/$ to (?P<areaslug>[-\w]+)?/$ and now the error is:
Page not found (404)
Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail
Is it possible that because the "{% url 'polls:areadetail' feature.properties.myslug%}" bit is inside javascript, that feature.properties.myslug is not being inserted there correctly? Like some sort of brackets are needed here?
According to the error message, feature.properties.myslug is empty or has no value.
Make sure the feature.properties.myslug is passed correctly from view.
Comment out {% url .. %} temporarily.
Print {{ feature }}, {{ feature.properties }}, {{ feature.properties.myslug }} to see if which part is missing.
Fix view accordingly.
Uncomment {% url .. %}.
After some more digging around I've found the answer to why doesn't this work in another question at:
How to pass javascript variable to django custom filter
The answer to it by Ludwik Trammer says:
Django templates are build on the server side, while JavaScript is executed on the client side.
That means that template code is always executed before JavaScript (as
it is executed by the server, before the page is sent to the client).
As a consequence it is absolutely impossible to mix JavaScript and
Django code the way you want to.
Which clearly applies here. I was focused on problems with the URL template, regex on the urls.py file etc. when the problem was that no matter what I did, because it's in a javascript section, run client-side, that URL template will always be incomplete no matter what I do, therefore being an impossible solution to what I want.
My website has a list of pages that are under /record and available only with query parameters of type and id. Like so:
/record?type=poem&id=175
I am using the django next redirect to go from the login page to the previous page. I initially used href="{% url 'auth:login' %}?next={{ request.path }}" to redirect, but it didn't take the query parameters (i.e type and id). This takes the user to
/login/?next=/record
I then used href="'{% url 'auth:login' %}?next={{ request.path }}'+window.location.search". However, this doesn't work as well. This takes the user to
/login/?next=/record?type=poem&id=175
but it finally redirects to
/record
How do I redirect using next along with query parameters? Is this behavior not possible?
You need to escape special characters in the URL, namely '?', '&' and '='.
While there is django.utils.encoding.escape_uri_path, it doesn't escape the ampersand (&), which is a problem because it will be interpreted as the end of the next parameter and the beginning of another.
Instead, you can use urllib.parse.quote:
from urllib.parse import quote
current_url_escaped = quote(request.get_full_path())
and in the template:
href="{% url 'auth:login' %}?next={{ current_url_escaped }}
You can use HttpRequest.get_full_path() method along with urlencode template filter to get the current url along with the query string.
HttpRequest.get_full_path()
Returns the path, plus an appended query string, if applicable.
href="{% url 'auth:login' %}?next={{request.get_full_path|urlencode}}
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' %}