>
> <p>{{movies.likes}} peaple liked this article</p>
> <p><a href="/like/{{post.id}}">Like</a></p>
>
>
> This is not how you should generate URL's within your templates. You
should be using the {% url %} tag to automatically generate them:
<p><a href="{% url 'like_post' post.id %}">Like</a></p>
If using the slug in your urls.conf, your {% url %} tag would look
something like this:
<p><a href="{% url 'like_post' post.slug %}">Like</a></p>
See
https://docs.djangoproject.com/en/dev/intro/tutorial03/#removing-hardcoded-urls-in-templates
my urls
>
>
> url(r'^view/(?P<slug>[^\.]+)/$', views.view_post, name='view_post'),
>
> url(r'^(?P<pk>[0−9]+)/$', views.like_post, name='like_post'),
>
>
Your urls.conf should also be modified to avoid clashes down the road. I
would recommend changing this line to imply that you are viewing the post,
rather than prefixing the verb 'view' on the URL with no reference as to
what type of object you are viewing:
url(r'^post/(?P<slug>[^\.]+)/$', views.view_post, name='view_post'),
As of now, you are using mysite.com/5 to indicate that you want to like a
post, which makes it difficult to discern that is what you are doing.
Something like this would work.
url(r'^post/(?P<pk>[0−9]+)/like/$', views.like_post, name='like_post'),
You can also do this using the slug if you want to keep your URL's looking
the same:
url(r'^post/(?P<slug>[^\.]+)/like/$', views.like_post, name='like_post'),
I would recommend always using the slug, or always using the PK, but
not intermixing the two if you can avoid it (keeps things easier to
remember when doing more advanced operations). If you care about SEO,
you'll probably want to use the slug.
See
https://docs.djangoproject.com/en/dev/intro/tutorial03/#writing-more-views
You'll notice they have the verb (action word) after the identifier for the
item that is being acted upon. In psuedo-code English, this translates to
'here is the post that I want to like' rather than 'I am liking something,
here is a post'. Not sure if that translates well to other languages if
English isn't your first language.
You'll want to set things up this way because it will help keep your URL's
together in one configuration file, and will make your URL scheme much
simpler. Much easier to keep track of all the actions that you can perform
for a particular post (create, update, delete, comment, like, etc.) rather
than having an extensively long list of actions that have actionable
objects. If you add more content types, like a 'news article', then you'll
have to modify two sets of urls.py files or stanzas, one for viewing the
article, and another for the extra actions that can be taken on that
article, which gets cumbersome and difficult to manage.
>
> my view
>
>
> def like_post(request,pk=1):
> a=Movies.objects.get(pk=pk)
> count=a.likes
> count+=1
> a.likes=count
> a.save()
> return HttpResponseRedirect ( '/posts/view/%s' % pk)
>
>
>
You should be using reverse() here rather than the actual URL. There are
examples of this in the tutorial for Django:
https://docs.djangoproject.com/en/1.9/intro/tutorial01/
> but i have error not work
>
>
> Page not found (404)
> Request Method: GET
> Request URL: http://127.0.0.1:8000/like/1/
>
> Using the URLconf defined in categories1.urls, Django tried these URL
> patterns, in this order:
>
> 1. ^admin/
> 2. ^$ [name='index']
> 3. ^view/(?P<slug>[^\.]+)/$[name='view_post']
> 4. ^category/(?P<slug>[\w-]+)/$[name='view_category']
> 5. ^(?P<pk>[0−9]+)/$ [name='like_post']
> 6. ^media\/(?P<path>.*)$
>
> The current URL, like/1/, didn't match any of these.
>
>
>
> any idea ?
>
> You are trying to reach /like/1/, but your urls.py file is configured only
to look for /1/ (line 5).
Please run through the tutorial, which will show you step by step how these
processes work together, and should give you a good basis for URL design.
https://docs.djangoproject.com/en/1.9/intro/tutorial01/
-James
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVWAmZwiTTk36txn1pe8MsXMqnne3W0OvCiV5puBLTvdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.