Доделать web интерфейс - Python

Узнай цену своей работы

Формулировка задачи:

Python 3.4.4/3.5.1 + Django 1.9.5 + PostgreSQL 9.5.2, проект и бекап БД во вложении.
Листинг программы
  1. conn = psycopg2.connect("dbname = 'Books' user = 'postgres' host = 'localhost' password = '111'")
На данный момент работает только админка. Нужно исправить ошибки. Задание: Измените файл конфигурации «.\books\views.py»: • импортируйте модели Book, Author; • добавляем представления.
Листинг программы
  1. from django.shortcuts import render_to_response
  2. from books.models import Book
  3. from books.models import Author
  4. def select_author(request):
  5. if 'Name' in request.GET and request.GET['Name']:
  6. Name1 = request.GET['Name']
  7. Authors = Authors.objects.filter(Name__startswith = Name1)
  8. return render_to_response('Select_authors.html', {'rows' : Authors, 'Author' : True})
  9. else:
  10. return render_to_response('Select_authors.html', {'Author' : False})
  11. def select_book(request):
  12. Books = Book.objects.filter(author_id = request.GET['author_id'])
  13. return render_to_response('Select_Books.html', {'Books' : Books})
  14. def save_book(request):
  15. ID = Book.objects.get(id = request.GET['id'])
  16. D = ID.downloads
  17. ID.downloads = 1 + D
  18. ID.save()
  19. Books = Book.objects.filter(author_id = request.GET['author_id'])
  20. return render_to_response('Select_Books.html', {'Books' : Books})
Отредактируйте список URL «.\mysite\urls.py»:
Листинг программы
  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from mysite.views import views
  4. from django.conf import settings
  5. from books.views import select_author
  6. from books.views import select_book
  7. from books.views import save_book
  8. admin.autodiscover()
  9. urlpatterns = [
  10. url(r'^select_author/$', select_author),
  11. url(r'^select_book/$', select_book),
  12. url(r'^save_book/$', save_book),
  13. url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT}),
  14. url(r'^$', views),
  15. url(r'^admin/', admin.site.urls),
  16. ]
Создаем шаблон «.\templates\Select_authors.html»:
Листинг программы
  1. {% load staticfiles %}
  2. <html>
  3. <head>
  4. <title>Книги</title>
  5. <meta charset = "utf-8">
  6. <link rel = "stylesheet" type = "text/css"
  7. href = "{% static 'mysite/style.css' %}" />
  8. </head>
  9. <body>
  10. <div><h1>Писатель</h1></div>
  11. <form action = "/select_author/" method = "get">
  12. <input name = Name>
  13. <input type = "submit" value = "Найти">
  14. </form>
  15. {% if Author %}
  16. {% for a in rows %}{% endfor %}
  17. {% for a in rows %}
  18. <table>
  19. <tr><th colspan = 2>
  20. <tr>
  21. <td>
  22. <a href = '/select_book/?author_id = {{a.id}}'>
  23. <img src = {{a.img.url}} align = left>
  24. </a>
  25. </td>
  26. <td valign = top>
  27. <div class = "container">
  28. <h2>{{a}}</h2>
  29. <a href = "#demo{{a.id}}" class = "btn btn-success"
  30. data-toggle = "collapse">Биография
  31. </a>
  32. <div id = "demo{{a.id}}" class = "collapse"> {{a.Text}}
  33. </div>
  34. </div>
  35. </td>
  36. </tr>
  37. </table>
  38. {% endfor %}
  39. {% endif %}
  40. </body>
  41. </html>
Создаем шаблон «.\templates\Select_books.html»:
Листинг программы
  1. <html>
  2. <head> {% load staticfiles %}
  3. <title>Книги</title>
  4. <meta charset="utf-8">
  5. <link rel = "stylesheet" type = "text/css"
  6. href = "{% static 'mysite/style.css' %}" />
  7. </head>
  8. <body> {% for a in books %}
  9. <table>
  10. <tr>
  11. <th colspan = 2>
  12. <p align = "center"><H1> {{a}} </H1></p>
  13. </th>
  14. </tr>
  15. <tr>
  16. <td>
  17. <a href = {{a.book.url}}>
  18. <img src = {{a.img.url}} align = left>
  19. </a><br>
  20. <a href = '/like_book/?id = {{a.id}}&
  21. author_id = {{a.author_id}}'> интересно:
  22. </a> {{a.like}}
  23. </td>
  24. <td valign = top> {{a.Text}} <br></td>
  25. </tr>
  26. </table> {% endfor %}
  27. </body>
  28. </html>

Решение задачи: «Доделать web интерфейс»

textual
Листинг программы
  1. ОШИБКА:  отношение "books_author" не существует

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.667 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут