S
S
Sama Samsonov2017-02-22 12:11:14
Django
Sama Samsonov, 2017-02-22 12:11:14

How to fix this search?

model.py

class Author(models.Model):
    gender = (('M', 'Male'), ('F', 'Female'))

    name = models.CharField(max_length=100)
    pol = models.CharField(max_length=1, choices=gender)
    birth = models.DateField(max_length=100)

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

шаблон
form action="{% url 'knig-avtor' %}" method="GET"> {%csrf_token %}
         <p>Книги
            select name="bok_avt">
                {% for ba in books %}
                    option value="{{ ba.id }}">{{ ba.name }} </option>
                {% endfor %}
            /select>
            input type="submit" value="Найти">
         </p>
    /form>

views.py
def knig_avtor(request):
    a_id = request.GET['bok_avt']
    booksa = Author.objects.get(id=a_id)
    return render(request, 'app/author_book.html', {'booksa':booksa})

Throws MultiValueDictKeyError how to fix?
Is it necessary to display all the authors of this book when choosing books?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Keofaste, 2017-02-22
@Keofaste

a_id = request.GET.get('bok_avt')
booksa = get_object_or_404(Author, id=a_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question