2

This is what I have at the moment:

views.py

def activation_signupcount(request):

    if 'datestart' not in request.GET:

        form = SegmentForm(request.GET)
        #form2 = Period(request.GET)

        return render_to_response('activation/activation_signupcount.html', {'datestart':'', 'form':form})

template

<form method="get" action="">
<h3>1. Choose segment</h3>

{{ form.as_p }}

forms.py

from django import forms
from django.forms.widgets import RadioSelect
from django.forms.extras.widgets import SelectDateWidget

usersegment = [['non-paying','Non-paying'],['paying','Paying'], ['all', 'All']]

class SegmentForm(forms.Form):
    radio = forms.ChoiceField(required = True, label= False, widget=RadioSelect(), choices=usersegment)

The output looks like this

enter image description here

Questions:

  1. How do I set a initial or default value?
  2. How do I remove the bullet points
  3. How do I get rid of the text 'This field is required.'

I had a good look at the documentation on djangoprojects but there doesn't seem to be anything on it unless I missed something?

super9
  • 29,181
  • 39
  • 119
  • 172

2 Answers2

4

One Option for removing the bullet points and the "this field is required" is to set some css styles.

No bullet points:

list-style-type: none;

remove the field required text:

display: none;

Unfortunately i can't remember off the top of my head which classes by default you'll need. That shouldn't be hard to find though.

For the default value add into your form choice field initial={'non-paying': 'Non-Paying'} after the choices=usersegment and that should fix that ;)

PS. Let me know if I'm not clear enough.

edit found this similar question for the defaults Setting the selected value on a Django forms.ChoiceField

Edit 2:- as Glen pointed out in comments you'll have to remove the margin also. I'd suggest targeting the <li> elements and apply

margin: 0;
Community
  • 1
  • 1
James Khoury
  • 21,330
  • 4
  • 34
  • 65
  • the bullet points and default values are not working.. i've tried different combinations for the initial value but it doesnt seem to work. the class is errorlist btw – super9 Apr 27 '11 at 07:51
  • @nai The bullet points have to be in a different class. Default Values isn't working? its been a while for me... it might need to be something like `=[0]` or `='non-paying'` for a multiple choice field. – James Khoury Apr 28 '11 at 02:54
  • @nai i should have mentioned the `list-style-type: none` should be applied to the `
      `
    – James Khoury Apr 28 '11 at 03:05
  • I've figured it out since. Thanks! – super9 Apr 28 '11 at 06:20
  • 1
    However, `list-style-type:none` still leaves padding to the left of the list, to remove that "space" set a negative margin in the ul tag like so: `
      ...
    `.
    – Glenn Dayton Aug 02 '12 at 16:01
2

"This field is required" is being displayed because you are passing in a data parameter when you instantiate the form - you should only do this on POST, not GET. If you show your view code I can advise further on how to set initial values.

Updated Well, it's clear that this is indeed your problem. For some reason, you are passing the request.GET as the first positional parameter, which is data, thus binding the form. Don't do that. That's why you're getting that unwanted "This field is required". What is datestart supposed to be, anyway?

The way to pass initial data is well documented.

def activation_signupcount(request):
    if request.POST:
        form = SegmentForm(request.POST)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect('/somewhere/')
    else:
        form = SegmentForm(initial=request.GET)

    return render_to_response('activation/activation_signupcount.html',
                              {'datestart': request.GET.get('datestart', '')
                               'form': form})

Here the initial parameter is passed to SegmentForm only on GET, and data is passed only on POST.

To get rid of the bullet points, you just use CSS, but again you haven't shown us how you're doing that so it's impossible to say why it doesn't work.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895