I am using this plugin
There was an error with the url so I changed it from
<a class="liker" href="{% url like content_type content_obj.id 1 %}" rel="nofollow">I Like</a>`
to
<a class="liker" href="{% url 'like' content_type content_obj.id 1 %}" rel="nofollow">I Like</a>
as recommended in this fix but I am still getting this error
Reverse for 'like' with arguments '(u'snippets-snippets', None, 1)' and keyword arguments '{}' not found.
EDIT:
This is the urls.py from the app
urlpatterns = patterns(
'likes.views',
url(r'^like/(?P<content_type>[\w-]+)/(?P<id>\d+)/(?P<vote>-?\d+)$', 'like',
name='like'),
)
My urls.py simply includes it
urlpatterns = patterns('snippets.views',
(r'^likes/', include('likes.urls')),
)
Looking at your error, it seems like your content_obj.id is evaluating to None.
You might want to see if that object indeed exists. If not, you might have to do a sanity check. Something like
{% if content_obj.id %}
<a class="liker" href="{% url 'like' content_type content_obj.id 1 %}" rel="nofollow">I Like</a>
{% endif %}
Or pass the content_obj in the context appropriately.
Related
As I said in the title, what would be the correct syntax to change the href of a link according to the current url the user is on?
I tried this, but it doesn't work.
{% if url == '' %} href='/cookies' {% else %} href='' {% endif %}
What would be the correct syntax to do this?
At Django 1.9 and above can use something like this
href='{% if not request.path %}/cookies{% endif %}'
You can check the current url with
href="{% if request.path == '' %}/cookies{% endif %}"
I want to pass a string, in the url, and then catch in in the view
<a href="{% url 'dolgozo:dolgozo-detail' orderby=nev %} ">
I want to pass "nev" string.
url(r'^orderby=(?P<nev>.+)$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
What is the regex for this, and how can i catch it in the view?
Why not try the simple one...
HTML
<a href="{% url 'dolgozo:dolgozo-detail' %}/?orderby={{ nev }}">
URL
url(r'^orderby/$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
And in the view simply get the orderby using GET
orderby = request.GET.get('orderby')
html, no need for / before ?
<a href="{% url 'dolgozo:dolgozo-detail' %}?orderby={{ nev }}">
urls.py
url(r'^orderby/$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
views.py
orderby = request.GET.get('orderby')
I am using django 1.7 & python 3.4
I am trying to implement follow and unfollow a user into my website but i am stuck.
urls.py
url(r'^user/', include('myuserprofile.urls'),),
myuserprofile.urls.py
urlpatterns = patterns('',
url(r'^(?P<slug>[^/]+)/$', 'myuserprofile.views.profile', name='profile'),
url(r'^(?P<slug>[^/]+)/follow/$', 'myuserprofile.views.follow', name='follow'),
url(r'^(?P<slug>[^/]+)/unfollow/$', 'myuserprofile.views.unfollow', name='unfollow'),
views.py
#login_required
def follow(request):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(id=request.POST['to_user'])
rel, created = Relationship.objects.get_or_create(
from_user=myuser.myuserprofile,
to_user=to_user,
defaults={'status': 'following'}
)
else:
return HttpResponseRedirect(reverse('/'))
if not created:
rel.status = 'following'
rel.save()
and the template portion is like this:
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
Reverse for 'follow' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['user/(?P[^/]+)/follow/$']
You should add the username of the user to follow/unfollow:
{% url 'follow' user_to_follow.username %}
Change the urls.py to:
urlpatterns = patterns('myuserprofile.views',
url(r'^(?P<username>[^/]+)/$', 'profile', name='profile'),
url(r'^(?P<username>[^/]+)/follow/$', 'follow', name='follow'),
url(r'^(?P<username>[^/]+)/unfollow/$', 'unfollow', name='unfollow'),
)
And the signature of the view should accept the username argument:
#login_required
def follow(request, username):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(username=username)
...
You need to use namespaced URL.
In your case the URL unfollow should be referenced as <app_name>:unfollow.
URL namespaces allow you to uniquely reverse named URL patterns even
if different applications use the same URL names. It’s a good practice
for third-party apps to always use namespaced URLs (as we did in the
tutorial). Similarly, it also allows you to reverse URLs if multiple
instances of an application are deployed. In other words, since
multiple instances of a single application will share named URLs,
namespaces provide a way to tell these named URLs apart
have a look on this Here
you have to use URL namespaces
here is my
polls/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
...
)
so we have use like
{% url 'polls:index' %}
You were missing the slug in your template.
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
It should be
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' user.username %}{% else %}{% url 'follow' user.username %}{% endif %}" method="POST">
urls.py:
url(r'^signin_client$', views.signin_client, name='signin_client') # normal view,
url(r'^signup_owner$', SignupOwnerWizard.as_view()), # wizard view
In a 'normal view' case: <a href='{% url "myapp.views.signin_client" %}'> works.
But I can't figure how to do the same thing for my "wizard view". Of course, I don't want to hardcode the url.
Add name to the pattern:
url(r'^signup_owner$', SignupOwnerWizard.as_view(), name='signup_owner'),
And than use the name in {% url %} tag:
<a href='{% url "signup_owner" %}'>
If you use namespaces you would need a namespace prefix:
<a href='{% url "mynamespace:signup_owner" %}'>
I'm trying to use this app in my project: https://github.com/s1n4/django-favorite
but it has old url syntax and I did not understand how to transform it to new url syntax.
The url that I wanna transform:
{% url favorite.views.add_or_remove target_model target_object_id %}
Its exact form in html:
<button class="btn favorite" href="{% url 'favorite.views.add_or_remove' %}" model="{{ target_model }}" id="target_{{ target_object_id }}">
I know the syntax has changed with Django 1.5 and I tried to use this version:
{% url 'favorite.views.add_or_remove' target_model target_object_id %}
It also did not work.
It says:
Reverse for 'favorite.views.add_or_remove' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How can I fix this? Thanks.
Edit:
Its template tag can help to understand:
#register.simple_tag(takes_context=True)
def favorite_button(context, target):
user = context['request'].user
# do nothing when user isn't authenticated
if not user.is_authenticated():
return ''
target_model = '.'.join((target._meta.app_label, target._meta.object_name))
target_content_type = ContentType.objects.get_for_model(target)
target_object_id = target.id
fav_count = Favorite.objects.filter(target_content_type=target_content_type,
target_object_id=target_object_id).count()
undo = False
if user.favorite_set.filter(target_content_type=target_content_type,
target_object_id=target_object_id):
undo = True
return render_to_string('favorite/button.html',
{'target_model': target_model, 'target_object_id': target_object_id,
'fav_count': fav_count, 'undo': undo})
Since that application is in use in production (in which we used Django 1.4) I cannot patch the application to make it compatible with Django 1.5+. But there is a pull-request that I kept open for such a situation. Here it is: https://github.com/s1n4/django-favorite/pull/1 It might solve the problem.
The URL has no arguments:
url(r'^add-or-remove$', 'add_or_remove'),
and neither does that view:
def add_or_remove(request):
The URL tag in the HTML there also has no arguments:
{% url favorite.views.add_or_remove %}
To convert that to the new syntax it would be:
{% url 'favorite.views.add_or_remove' %}
After that you can use the template tag as described in the README:
{% load favorite_tags %}
{% for comment in post.comments %}
{% favorite_button comment %}
{% endfor %}