like query django

61 views
Skip to first unread message

Xristos Xristoou

unread,
Jan 19, 2016, 10:35:09 AM1/19/16
to Django users
hello,


i want to create a query for likes in my post details

my html tags

<p>{{movies.likes}} peaple liked this article</p>
<p><a href="/like/{{post.id}}">Like</a></p>

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'),

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)

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 ?

Florian Schweikert

unread,
Jan 19, 2016, 11:08:10 AM1/19/16
to django...@googlegroups.com
On 19/01/16 16:35, Xristos Xristoou wrote:
> url(r'^view/(?P<slug>[^\.]+)/$', views.view_post, name='view_post'),
> url(r'^(?P<pk>[0−9]+)/$', views.like_post, name='like_post'),

this defines urls like /1/

> The current URL, |like/1/|, didn't match any of these.

of course it doesn't, you didn't define a like/... url pattern

--
Florian

Xristos Xristoou

unread,
Jan 19, 2016, 11:15:28 AM1/19/16
to Django users
not work again florian same error again

James Schneider

unread,
Jan 19, 2016, 12:45:05 PM1/19/16
to django...@googlegroups.com
<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>
 

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. 
 

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:


 
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.


-James

Xristos Xristoou

unread,
Jan 19, 2016, 1:56:12 PM1/19/16
to Django users
i follow you not work again first i cant use {% url %} show me template error,i thing so i am is very noob
must necesery tu urls for that ?

Τη Τρίτη, 19 Ιανουαρίου 2016 - 5:35:09 μ.μ. UTC+2, ο χρήστης Xristos Xristoou έγραψε:

James Schneider

unread,
Jan 21, 2016, 12:47:32 PM1/21/16
to django...@googlegroups.com
On Tue, Jan 19, 2016 at 10:56 AM, Xristos Xristoou <sax...@gmail.com> wrote:
i follow you not work again first i cant use {% url %} show me template error,i thing so i am is very noob
must necesery tu urls for that ?

Can you post your template with the changes you made? And the errors that you are receiving?

-James 

Xristos Xristoou

unread,
Jan 22, 2016, 4:08:18 AM1/22/16
to Django users
look my version django cant not be use url 'like_post' because show me errow only 1.5 version django can use that


Τη Τρίτη, 19 Ιανουαρίου 2016 - 5:35:09 μ.μ. UTC+2, ο χρήστης Xristos Xristoou έγραψε:

James Schneider

unread,
Jan 23, 2016, 5:32:54 AM1/23/16
to django...@googlegroups.com


On Jan 22, 2016 1:08 AM, "Xristos Xristoou" <sax...@gmail.com> wrote:
>
> look my version django cant not be use url 'like_post' because show me errow only 1.5 version django can use that
>

I have no idea what you are trying to say here, sorry.

Are you using Django 1.5? If so, you should upgrade immediately.

Without any further information posted, I'm afraid I can't be of much help.

-James

Xristos Xristoou

unread,
Jan 23, 2016, 5:58:23 AM1/23/16
to Django users
i have 1.8 version


Τη Τρίτη, 19 Ιανουαρίου 2016 - 5:35:09 μ.μ. UTC+2, ο χρήστης Xristos Xristoou έγραψε:
Reply all
Reply to author
Forward
0 new messages