Add reddit / hacker news style votes in Class Based Views

0

I would like to add something similar to the voting system that many web pages have like StackOverflow, Reddit, Hacker News, etc. I wanted to create a button to do this task in the main template that I have, trying to create a FormView but I realize that maybe it does not work, because I'm using a ModelForm that does not have the field vote that has the model because it grabs it by default.

template

{% for new in News %}
    <li class="list-group-item">
       <form action="{% url 'New:vote' %} new.id" method="POST">
           <input type="submit" value="upvote">
           {{ new.vote }}
       </form>
        <a href="{{ new.url }}">{{ new.title }}</a>
    </li>
{% endfor %}

deal with this view

class NewVote(FormView):
    form_class = 'NewForm'

def get_success_url(self):
    return reverse_lazy('New:list', args=(self.object.id, ))

and I have these url, currently in the list, which is the main page, is giving me an error

  

Reverse for 'vote' with no arguments not found. 1 pattern (s) tried: ['vote / (? P \ d +) / $']

url(r'^$', NewList.as_view(), name='list'),
url(r'^add/', NewAdd.as_view(), name='add'),
url(r'^edit/(?P<pk>\d+)/$', NewEdit.as_view(), name='edit'),
url(r'^detail/(?P<pk>\d+)/$', NewDetail.as_view(), name='detail'),
url(r'^vote/(?P<pk>\d+)/$', NewVote.as_view(), name='vote'),

And this is my form where I had thought to add the field for the button, but honestly I do not know if it is the right thing to do it

class NewForm(ModelForm):

    error_message = {
        'url_exists': 'The url alredy exists'
    }

    class Meta:
        model = New
        fields = ('title', 'url')
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'url': forms.TextInput(attrs={'class': 'form-control'})
        }

    def clean(self):
        cleaned_data = super(NewForm, self).clean()
        print(cleaned_data)

    def clean_url(self):
        url = self.cleaned_data['url'] 
        if New.objects.filter(url=url).exists():
            raise forms.ValidationError('The URL alredy exists')
        return url

Maybe I can create a new form, but would it be from a ModelForm or can I access it in a different way?

It's a long post, but I hope someone can help me.

    
asked by ikenshu 06.09.2017 в 23:39
source

1 answer

1

Asking about the subject of the vote, and how to do it, is enough to get your question closed, because it is initially based on opinions, and it is too broad a subject, now, to solve the only problem that I see of code What you have is the following:

You have this line:

<form action="{{% url 'New:vote' %} new.id" method="POST">

Which, from the beginning, you have a { more then action=" and you should know that the arguments to the urls from the {% url ... %} tag go inside the tag, so that line should be like this :

<form action="{% url 'New:vote' new.id %}" method="POST">

I hope I have helped you with your question, I can only solve that error because helping you with the other is to do a whole backend to make a voting system.

    
answered by 07.09.2017 / 15:49
source