On Saturday, April 5, 2014 8:13:04 AM UTC-4:30, Christo bale wrote:
>
> I would like to make it possible to add a poll on the index.html, so one
> don't need the admin.
> I added a forms.py.
> Right now when I click enter after typing the new poll, the sites goes
> blank and I need to refresh it. And no polls have been added. I am pretty
> new to django, so I hope that you can help me.
>
> forms.py
> class ComposePoll(forms.ModelForm):
> class Meta:
> fieldsets = [
> (None, {'fields': ['question']}),
> ('Date information', {'fields': ['pub_date'], 'classes':
> ['collapse']}),
> ]
>
> index.html
>
> <form action='' method='post'> {% csrf_token %}
> {{ form.add_poll }}
> <input class='btn btn-default' type='text'>
> </form>
>
> views.py
> class IndexView(generic.ListView):
> template_name = 'polls/index.html'
> context_object_name = 'latest_poll_list'
>
> def get_queryset(self):
> """
> Return the last five published polls (not including those set to be
> published in the future).
> """
> return Poll.objects.filter(
> pub_date__lte=timezone.now()
> ).order_by('-pub_date')[:5]
>
> add_poll = ComposePoll(request.POST or None)
> if add_poll.is_valid():
> create_poll = Poll.save(commit=False)
> create_poll.sent = datetime.datetime.now()
> create_poll.save()
> return HttpResponseRedirect('/polls/')
>
> poll = Poll.objects.filter()
>
> return render_to_response('polls/index.html', locals(),
> context_instance=RequestContext(request))
>
Hello,
You should add a 'post' method to your view to get the data from the form
and store it in your models.
Regards,
Camilo.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/4ce00fee-daf9-498e-b502-46c68b62f95d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.