2016-04-04 15 views
0

私はDjangoを学び始めています。クラスベースのビューを定義しました。タイトル、著者などの手動で書籍の詳細を入力できるビューを作成しました...また、その同じビューでは、apiやwebのスクレイピングによって自動的に本の詳細を見つけるはずです。結果は新しいビューで表示する必要があります。新しいURLはユーザーの入力に基づいて変更され、複数の結果でユーザーは最適に選択されます。htmlテンプレートからキーワードを取得する方法 - Django

問題は、定義された検索ボックスからの入力を受け取り、それを操作する別のビュー(別のクラス)に渡す方法はまだわかりません。テンプレートで

検索ボックス:

<div class="col-sm-offset-2 col-sm-10"> 
    <form id="custom-search-input" action="{% url 'find_book' %}" method="get" accept-charset="utf-8"> 
     <div class="input-group col-sm-10" > 
     <input type="text" class="form-control input-lg" placeholder="Find books" /> 
     <span class="input-group-btn"> 
      <button class="btn btn-info btn-lg" type="submit"> 
      <i class="glyphicon glyphicon-search"></i> 
      <i class="fa fa-search"></i> 
      </button> 
     </span> 
     </div> 
     </form> 
    </div> 
ユーザが入力することができます

定義されたクラス:私は結果を表示したい

class BookCreateView (CreateView, SuccessMessageMixin,): 
    model = Appointment 
    fields = ['title', 'author', 'time'] 
    success_message = 'Appointment successfully created.' 

    def get_context(self, **kwargs): 
     context = super(BookCreateView, self).get_context_data(**kwargs) 

クラス(か見当がつかない):

class BookFindView (View): 
    response_template = 'book_find.html' 

ヒントやチュートリアル、またはアドバイスされたドキュメントは非常に高く評価されます。

+0

これはDjango Viewsがユーザー入力を受け入れる方法についての質問ですか? よりシンプルなビューでは、[this solution](http://stackoverflow.com/a/21207496/1842146)で作業を行う必要があります(また、これらのよりシンプルなビューには、 'class'の代わりに' def'を使用すると思います。 BookFindViewはもっと良い選択肢になりそうです)。 –

答えて

1

いずれのビューでも、「取得」処理と「ポスト」処理の両方を実装できます。あなたは何かのようにすることができます

class BookCreateView(views.View): 
    # subclass the basic form and add an hidden input named search 
    form = your_custom_search_form 
    # subclass the create model form 
    confirmation_form = your_custom_create_model_form 

def get(request): 
    # show the empty search page 
    # in your template use the form to implement search 

def post(request): 
    # handle the book search; there is a data in the payload named search 
    # you now have a payload to use; here you make a database query 
    # and return the similar books; every book is printed in the page 
    # using the your_custom_create_model_form that you can prepopulate 

    # handle the confirmation of the book otherwise; the post 
    # payload will contain the book information 
関連する問題