简体   繁体   中英

Django 1.6: ValueError invalid literal for int() with base 10

I'm trying to filter by doctor objects with a specialisation input i get from the last page but I keep get this error

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "views.py" in doclistings
  87.     doctors = Doctor.objects.filter(specialization = s).order_by('-likes')


Exception Type: ValueError at /doclistings/
Exception Value: invalid literal for int() with base 10: 'Dentist'

Here is the view where I'm trying to filter them

def doclistings(request):
    d = getVariables(request)
    s = request.session.get('selection')
    d['userselection'] = s
    doctors = Doctor.objects.filter(specialization = s).order_by('-likes')
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator

    return render_to_response('meddy1/doclistings.html',d)

Here is the Doctor model

class Doctor(models.Model):
    name = models.CharField(max_length=100)
    specialization = models.ForeignKey(Specialization)
    clinic = models.ForeignKey(Clinic)
    seekers = models.ManyToManyField(User, through='UserContent')
    likes = models.IntegerField(default=0)

Here is the specialization model

class Specialization(models.Model):
    name = models.CharField(max_length=30)

Here is the index template where I've the form

<div class="signup">
          <div class="form-group">
            <form action="" method="post" >
            <select class="form-control" id="selection" name="selection">
              <option><b>Find a Doctor...</b></option>
              {% for value, text in form.selection.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
              {% csrf_token %}
            </select>
<span class="input-group-btn">
              <button class="btn btn-primary" type="submit"  name="submit" id="ss-submit">Find Doctors</button>
            </span>
          </div>
        </div>

I'm printing it in the doclisting.html to check the selection value I get

  <h2>{{userselection}}</h2> 

I'm just trying to show filter the doctor objects with a specialization and order by highest likes.

I think you should do this in your views.py:

def doclistings(request):
    d = getVariables(request)
    s_name = request.session.get('selection')  # Change variable name
    d['userselection'] = s_name  # Update this for new variable name s_name
    spec = Specialization.objects.get(name=s_name)  # Get spec object
    # Now this should work:
    doctors = Doctor.objects.filter(specialization = spec).order_by('-likes')  
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:....

As @Rohan said you could also do:

def doclistings(request):
    d = getVariables(request)
    s_name = request.session.get('selection')  # Change variable name
    d['userselection'] = s_name  # Update this for new variable name s_name
    # Now this should work:
    doctors = Doctor.objects.filter(specialization__name = s_name).order_by('-likes')  
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:....

In the second way you don't need to get the specialization object, you use specialization__name to say to Django that gets the doctors wich foreign key to specialization has the name equals to s_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM