2017-04-09 7 views
0

여러 양식을 포함하는 페이지를 만들려고하고 있으며 페이지의 일부 게시물을 "좋아할"수 있도록 "좋아하는 단추"와 같은 것을 만들고 싶습니다. 나는 또한 모든 게시물을 좋아할 수 있도록 몇 가지 제한을두기를 원하지만 각 게시물을 한 번만 쓸 수 있습니다.여러 양식을 게시 할 때 장고 csrf 토큰

이제 게시물을 "좋아"할 수 있다는 문제가 있습니다. 다른 게시물에 대해 "좋아요"를 클릭하면 csrf 오류가 발생합니다 (CSRF 확인에 실패했습니다. 요청이 중단되었습니다.). 한 페이지에 여러 게시물을 동시에 표시하는 방법을 알고 싶습니다.

{% csrf_token %}을 (를) 어디에 어떻게 배치 할 것인가? 이 기사 (How Will the Inclusion of Two Forms Affect my CSRF Token Use?)는 {% csrf_token %}을 모든 양식에 넣어야한다고 말하지만 작동하지 않습니다. 여기

내 코드입니다 :

models.py

class Restaurant(models.Model): 
    name = models.CharField(max_length=20) 
    phone_number = models.CharField(max_length=15) 
    address = models.CharField(max_length=50, blank=True) 
    likes = models.DecimalField(max_digits=2,decimal_places=0, default=0) 

views.py

<!doctype html> 
<html> 
<head> 
    <title> Menu </title> 
    <meta charset='utf-8'> 
</head> 
<body> 
    <h2>餐廳列表</h2> 

    <table> 
     <tr> 
      <th>ID</th> 
      <th>NAME</th> 
      <th>PHONE</th> 
      <th>ADDRESS</th> 
      <th>LIKES</th> 
      <th>LIKE IT!</th> 
     </tr> 
     {% for r in restaurants %} 
      <tr> 
       <td> {{ r.id }} </td> 
       <td> {{ r.name }} </td> 
       <td> {{ r.phone_number }} </td> 
       <td> {{ r.address }} </td> 
       <td> {{ r.likes }} </td> 
       <td> 
       <form id={{ r.id }} action="" method="post"> 
        {% csrf_token %} 
        <input type="hidden" name="ok" value="yes"> 
        <input class="submit" type="submit" value="Upvote"> 
       </form> 
       </td> 

      </tr> 
     {% endfor %} 
    </table> 

    </form> 
</body> 
</html> 

views.py

def list_restaurants(request): 
    restaurants = Restaurant.objects.all() 

    if request.method == "POST": 
     post = Restaurant.objects.get(id=request.POST['id']) 
     post.likes += 1 
     post.save() 
     return render_to_response('restaurants_list.html',locals()) 
    else: 
     return render(request, 'restaurants_list.html',locals()) 

답변

0

변화

return render_to_response('restaurants_list.html',locals()) 

에 제안을
return render(request, 'restaurants_list.html',locals()) 
+0

감사합니다,하지만 난 모든 포스트를 좋아 할 수 있도록 몇 가지 제한을 넣어하려는 경우 수 있지만 한 번만 각 게시물처럼? – CYH16

+0

다른 논리. 어떤 사용자가 어떤 게시물을 저장해야합니다. 당신은 그것을 건설해야합니다 – itzMEonTV

+0

고마워요. 나는 그것에 대해 연구 할 것이다 / – CYH16