0

I'm making a blog using Django 1.9 and I've run into problem while implememting a upvote system in the website.

<form action="{% url 'blogapp:vote_handler' id=instance.get_id %}" method="POST" id="upvote_form_post">
{% csrf_token %}
<input type="hidden" name="user" value="{{request.user}}">
<input type="hidden" name="content_type_upvote" value="{{ instance.get_content_type }}">                                                
<input type="hidden" name="object_id_upvote" value="{{ instance.get_id }}">                 
<button type="submit" class="icon fa-heart button-link" name="upvote_form_post" value="Submit"> &nbsp{{ instance.vote_count }}</button>
</form>

This form has been giving me problems, particularly the action attribute with the url.

enter image description here

my url file :

urlpatterns = [
# url(r'^$', views.IndexView.as_view(), name='list'),
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/delete/$', views.DeleteView.as_view()),
url(r'^(?P<slug>[\w-]+)/edit/$', views.UpdateView.as_view(), name='edit'),
url(r'^vote/(?P<id>[0-9]+)/$', views.vote_handler, name='vote_handler'),

]

I've no idea why this isn't working. It works fine when I hardcode the url as follows:

<form action="/vote/{{instance.id}}/" method="POST" id="upvote_form_post">

I have absolutely no idea why instance.id or instance.get_id is evaluating to a empty string.Does anyone have any idea about this?

Deven
  • 55
  • 8
  • 2
    Does the model have a `get_id` method? Maybe you should post it. – Daniel Roseman Jul 17 '16 at 12:36
  • @Daniel Yes the Post model has a get_id method: \@property def get_id(self): return self.id . This was mostly me trying different solutions but putting instance.id gives the same results. – Deven Jul 17 '16 at 12:42
  • 1
    I think you need to post the full view. – Daniel Roseman Jul 17 '16 at 12:47
  • @Daniel Full template : http://pastebin.com/J9L4837J Full view: http://pastebin.com/SdWcGfLK Sorry for pastebin. Thank you for replying, much appreciated. Also, this is the hardcoded url version, only that linewas changed. – Deven Jul 17 '16 at 12:51
  • 1
    I meant the view that's rendering this page. – Daniel Roseman Jul 17 '16 at 12:55
  • Also, I think I need to elaborate on the models a bit. The models are Post, Comment, Vote. I'm using the Vote model on both the Posts and he comments and that's why you'll see a similar upvote form for the comment s near the bottom of the template. That one is working completely fine, the form for upvoting posts does not seem to work even though it is similar to the form for upvoting comments. – Deven Jul 17 '16 at 12:58
  • @DanielRoseman The detail view : http://pastebin.com/8EP6Ncgk Thanks again. – Deven Jul 17 '16 at 13:00
  • @Daniel I'm completely stumped. Why is instance.id or instance.get_id evaluating to an empty string? Using {{ instance.id }} outside the form is giving the correct id but not within the {% %} tags. I guess I'll just have to go with the hardcoded url. – Deven Jul 17 '16 at 14:37
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Jul 17 '16 at 15:09

0 Answers0