James L.
James L.

Reputation: 1153

Django 1.6: Button clicked in template not recognised in views

I'm trying to implement a review form where users can submit reviews on doctors. I've a like/dislike button in the template along with other fields of the the review form. But the Like/Dislike button is not being recognised in the views. So when I click the "Submit Review" to submit the form nothing gets saved in the database. It doesn't even give me any errors.

Here is the template

  <form action="" method="post" id="user_uploader" > {% csrf_token %}

  <input type="radio" name="Like" value="Like">Like<br>
  <input type="radio" name="Like" value="Dislike">Dislike

    <input type="hidden" name="user" value="{{ user.id }}" />
    <input type="hidden" name="doctor" value="{{ doctor.id }}" />

    <select class="form-control" id="s1" name="time">
      <option><b>How long did you have to wait?</b></option>
      {% for value, text in form.time.field.choices %}
      <option value="{{ value }}">{{ text }}</option>
      {% endfor %}
    </select>
    <textarea type="text" class="form-control" id="comment" placeholder="Comment" name="comment" rows="8"></textarea>

    <button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Submit Review</button>

  </form>

</div>

Here is the views. I tried testing if it goes inside the if statement by using print statement, but nothing gets printed.

def addContent(request, id):
d = getVariables(request)
doctor = Doctor.objects.get(id=id)

if request.user.is_authenticated():
    user = request.user
    ds = DoctorSeeker.objects.get(user=user)
    d['doctorseeker'] = ds


if request.method == "POST":
    form = UserContentForm(request.POST)

    if form.is_valid():
        time = form.cleaned_data['time']
        comment = form.cleaned_data['comment']

        if request.POST.get('Like'):
            con = UserContent(time=time, comment = comment, liked = True, disliked = False, doctor_id = doctor.id, user_id = request.user.id)
            doctor.likes += 1
            doctor.netlikes = doctor.likes - doctor.dislikes
            con.save()

        elif request.POST.get('Dislike'):
            con = UserContent(time=time, comment = comment, liked = False, disliked = True,  doctor_id = doctor.id, user_id = request.user.id)
            doctor.dislikes +=1
            doctor.netlikes = doctor.likes - doctor.dislikes
            con.save()

        url = '/docprofile/%s' % str(doctor.id)
        return HttpResponseRedirect(url)

else:
    form = UserContentForm()

d.update({'doctor': profile, 'UGC': UserContent.objects.all(),
      'form': form })
return render(request, 'meddy1/usercontent.html',d)

Upvotes: 0

Views: 136

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599796

The main problem is that the like and dislike buttons are not inside the form. Only elements inside the form element will be submitted to the server.

However it's is not clear what you are expecting the buttons to do. Button elements are not like checkboxes or radio buttons that you can select and then submit along with the rest of the form: they work as actual submit buttons themselves.

Upvotes: 1

Related Questions