python 2.7 - Using <form> to update BooleanField in Django -


using django 1.6 python 2.7.

my model has booleanfield variable, , want user able change via post clicking button change false true, or vice versa. having issues rendering template.

model currently:

class pic(models.model):     name = models.charfield(max_length=100)     = models.booleanfield()     image = models.filefield(upload_to="images/")      def __unicode__(self):         return self.name 

app urls is:

url(r'^(?p<pic_id>\d+)/$', views.single_picture, name='single_picture'), 

in template have:

<form action="{% url 'single_picture' pic.good %}" method="post">     {% csrf_token %}     {% if pic.good %}     <input type="checkbox" name="choice" id="{{ pic.good }}" value="false" />     <label for="{{ pic.good }}">false</label><br />     {% else %}     <input type="checkbox" name="choice" id="{{ pic.good }}" value="true" />     <label for="{{ pic.good }}">true</label><br />     {% endif %}     <input type="submit" value="good" /> </form> 

and view have:

def single_picture(request, pic_id):  if request.method == 'get':     pic = get_object_or_404(pic, pk=pic_id)     latest_pictures_list = pic.objects.all()     return render(request, 'pictures/single_picture.html', {'pic': pic, 'latest_pictures_list': latest_pictures_list}) elif request.method == 'post':     pic = get_object_or_404(pic, pk=pic_id)     latest_pictures_list = pic.objects.all()     try:         selected_choice = p.choice_set.get(pk=request.post['choice'])     except (keyerror, pic.doesnotexist):         return render(request, 'pictures/single_picture.html', {'pic': pic, 'error_message': 'uhhhh...',         })     else:         selected_choice.save()         return httpresponseredirect(reverse('pictures/single_picture.html', {'pic': pic})) 

i'm not sure if there multiple errors @ point or not. when trying view template, get

reverse 'single_picture' arguments '(true,)' , keyword arguments '{}' not found. 0 pattern(s) tried: [] 

for '< form >' line. i'm guessing it's view?

i think issue in template, in line

<form action="{% url 'single_picture' pic.good %}" method="post">

your regex in urls.py r'^(?p<pic_id>\d+)/$', i'm guessing it's expecting id of picobject return in get request , not boolean, in case line should be

<form action="{% url 'single_picture' pic.id %}" method="post">

likewise, in lines underneath, there isid="{{ pic.good }}" display html id=true or id=false, suppose don't want. need replace bit id="{{pic.id}}"


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -