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.