NoReverseMatch Django Postman - django

Why does this return a NoReverseMatch?
From django-postman:
url(r'^write/(?:(?P<recipients>[\w.#+-:]+)/)?$', 'write', name='postman_write'),
Template:
<a href='{% url postman_write recipients=object.user %}'>Send Message</a>
This does not work either..
<a href='{% url postman_write object.user %}'>Send Message</a>
Returns: Reverse for 'postman_write' with arguments '(<User: admin>,)' and keyword arguments '{}' not found. What am I missing to construct this url properly? Thanks!

you are passing a user object in url tag. You need to pass the username.
{% url postman_write recipients=object.user.username %}
Also make sure you have included the postman urls in your main urls.py file.

Try:
{% url postman_write object.user.username %}

Related

Django SiteTree: menu renders but breadcrumbs doesn't when variable is in url

I am using django==1.11 and django-sitetree==1.9.0, all links without url variables render both on menu and breadcrumbs but urls with variables only render on menu(with correct links) and not on breadcrumbs.
Title: {{ object.title }}
URL: products:detail object.slug object.pk
URL as Pattern: Checked
url(r'^products/(?P<slug>[-\w\d]+)~(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
{% sitetree_breadcrumbs from "main-menu" template "sitetree/breadcrumbs_semantic.html" %}
Problem was with ~ sign in the url pattern, by changing the url pattern from
url(r'^products/(?P<slug>[-\w\d]+)~(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
to
url(r'^products/(?P<slug>[-\w\d]+)-(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
I could fix the problem.

How to properly call a url that came from an object ID in django

I want to make a checkout page and also want to call a url which came from an object ID in Django. The products app has its own urls.py, My question is How do you properly call an object ID? . This is the url http://localhost:8000/products/1. I attempt to call it but it return some error.
<button type="button" class="btn btn-primary"><i class="fa fa-shopping-
cart"> </i> Add To Cart
</button>
urls.py-products
url(r'^$', ProductListView.as_view(), name='products'),
url(r'^cbv/(?P<pk>\d+)', ProductDetailView.as_view(),
name='product_detail'),
url(r'^(?P<id>\d+)', 'products.views.product_detail_view_func',
name='product_detail_function'),
main urls.py
url(r'^products/', include('products.urls')),
this is the error
**Reverse for 'products/1' with arguments '()' and keyword arguments '{}'
not
found. 0 pattern(s) tried: []**
You are using url template tag incorrectly. Try
{% url 'product_detail_function' id=1 %}
or better pass the object id dynamically ie.
{% url 'product_detail_function' id=my_id %}
See Django documentation for details

django template replace url by template var

Is there the possibility to dinamically replace a url call from a template?
in the form I insert:
{% include "mytemplate.html" with soggetto='gigi' %}
in the template
url: '{% url 'go_to_this_{{soggetto}}' %}'
is there a way to replace the {{soggetto}} part dinamically or should I add a parameter to the url?
You can use the add template filter:
{% url 'go_to_this_'|add:soggetto %}
However, it might be cleaner to include a soggetto parameter in the url, then you could do:
{% url 'go_to_this' soggetto %}

Django url get request with parameter

I have following HTML file
<!DOCTYPE html>
<body>
{% for champion in champions %}
{{champion}}
{% endfor %}
</body>
</html>
And these are my URLS
urlpatterns = patterns('', url(r'^select_champion/$', views.select_champion, name='select_champion'),
url(r'^guide_form/(?P<champion>\w+)/$', views.guide_form, name='guide_form'),
url(r'^create_guide/$', views.create_guide, name='create_guide'),
url(r'^get_guide/(?P<id>\d+)/$', views.get_guide, name='get_guide'),
url(r'^guide_list/(?P<champion>\w+)/$', views.get_guide_list, name='get_guide_list'),
)
When I try to select a champion I get following Error:
Reverse for 'guide_form' with arguments '()' and keyword arguments
'{}' not found. 1 pattern(s) tried:
['guides/guide_form/(?P\w+)/$']
When I change this line
{{champion}}
To this
{{champion}}
I don't get an Error but the wrong URL is called of course. I want to select a champion and want the champ to be delivered per url so a guide can be written about him in the guide form.
Do you have any suggestion on how to solve my problem?
Almost there, you need:
{{ champion }}
Your url pattern has a keyword argument champion, and the value is the template variable {{ champion }}. The url tag understands the template context, so you don't need {{ }} around the variable; instead pass it in directly as an argument to the url tag.
It should be like this
<!DOCTYPE html>
<body>
{% for champion in champions %}
{{champion}}
{% endfor %}
</body>
</html>

How to include a link to reset password and register in Django views?

In my Django view, I use the following tags for logging in and logging out:
<href="{% url 'login' %}">
<href="{% url 'logout' %}">
What are the analogous tags for registering an account and resetting the password?
Currently I am doing this which feels rather ugly:
<href="/accounts/password/reset/">
<href="/accounts/register/">
What's the proper way to do this please?
Password Reset
As mentioned in the docs, it should be:
<a href="{% url 'password_reset' %}">
Registration
This is usually supported by third party libraries. It seems you might be using the django-registration default backend, in which case it is:
<a href="{% url 'registration_register' %}">
AFAIK you need to install django-registration to get these.
Then you can use:
{% url 'auth_password_reset' %}
{% url 'registration_register' %}
EDIT
For password reset you can use password_reset. See the docs.