2017-07-14 53 views
0

複数のユーザー入力を受け取り、ユーザーがサブミットすると別のページに表示する簡単なWebページを作成しようとしています。残念ながら私の提出ボタンは応答していません。私がクリックすると、何も起こりません。Djangoサブミットボタンが応答しない

以下

は、検索結果のHTMLコードされています。ここでは検索フォームのコードだ

<html> 
<head> 
    <title>Search results</title> 
</head> 
<body> 
    {% if hashtags.type()==None and user.type()==None and before_date.type()==None and since_date.type()==None %} 
     <p>You searched for tweets.</p> 
    {% elif hashtags.type()==None and before_date.type()==None and since_date.type()==None %} 
     <p>You searched for tweets from user <strong> {{ user }}</strong>.</p>  
    {% elif hashtags.type()==None and user.type()==None and before_date.type()==None %} 
     <p>You searched for tweets dated no earlier than <strong>{{ since_date }}</strong>.</p> 
    {% elif hashtags.type()==None and user.type()==None and since_date.type()==None %} 
     <p>You searched for tweets dated no later than <strong> {{before_date}}</strong>.</p> 
    {% elif hashtags.type()==None and before_date.type()==None %} 
     <p>You searched for tweets from user <strong> {{user}}</strong> dated no earlier than <strong>{{since_date}}</strong>.</p> 
    {% elif user.type()==None and since_date.type()==None %} 
     <p>You searched for tweets with hashtags <strong> {{hashtags}} </strong>dated no later than <strong> {{before_date}} </strong>.</p> 
    {% elif since_date.type()==None and before_date.type()==None %} 
     <p<You searched for tweets with hashtags <strong> {{hashtags}} </strong> from user <strong>{{user}}</strong>.</p> 
    {% elif hashtags.type()==None and user.type()==None %} 
     <p>You searched for tweets dated between<strong> {{since_date}} </strong> and <strong>{{before_date}}</strong>.</p> 
    {% elif hashtags.type()==None and since_date.type()==None %} 
     <p>You searched for tweets from user<strong> {{user}} </strong> dated no later than <strong>{{before_date}} </strong>.</p> 
    {% elif user.type() == None and before_date.type()==None %} 
     <p>You searched for tweets with hashtags <strong>{{hashtags}} </strong> dated no earlier than <strong> {{since_date}} </strong>.</p> 
    {% elif hashtags.type()==None %} 
     <p>You searched for tweets from user <strong> {{user}} </strong> dated between <strong>{{since_date}}</strong> and <strong>{{before_date}}</strong>.</p> 
    {% elif user.type()==None %} 
     <p>You searched for tweets with hashtags <strong>{{hashtags}}</strong> dated between <strong>{{since_date}}</strong> and <strong>{{before_date}}</strong>.</p> 
    {% elif since_date.type()==None%} 
     <p>You searched for tweets with hashtags <strong>{{hashtags}}</strong> from user <strong>{{user}}</strong> dated no later than <strong>{{before_date}}</strong>.</p> 
    {% elif before_date.type()==None%} 
     <p>You searched for tweets with hashtags <strong> {{hashtags}}</strong> from user <strong>{{user}}</strong> dated no earlier than <strong>{{since_date}}</strong>.</p> 
    {% else %} 
     <p>You searched for tweets with hashtags <strong> {{hashtags}}</strong> from user <strong>{{user}}</strong> dated between <strong>{{since_date}}</strong> and <strong>{{before_date}}</strong>.</p> 
    {%endif %}  
</body> 
</html> 

ここにコードを入力します。ここでは

<html> 
<head> 
    <title>Search for tweets</title> 
</head> 
<body> 
    <form action="/searched/" method = "post"> 
     {{form.as_p}} 
     {%csrf_token%} 
     <button type="submit">Submit</button> 
    </form> 
</body> 
</html> 

は、フォームのコードです。 py

from django import forms 

class TweetSearchForm(forms.Form): 
    hashtags = forms.CharField(required = False, label = "Please enter the hashtags you want to search for.", initial="") 
    user = forms.CharField(required = False, label = "Please enter the user whose tweets you want to search.", initial="") 
    since_date = forms.CharField(required=False, label = "Please enter the oldest you want your tweets to be.", initial="") 
    before_date = forms.CharField(required = False, label = "Please enter the youngest you want your tweets to be."initial="") 

ここにはviews.py:

enter code here 
from django.http import HttpResponse, HttpResponseRedirect 
from django.shortcuts import render, render_to_response, redirect 
from tweetsearch.forms import * 
from django.core.urlresolvers import reverse 
import datetime 

def tweet_search(request): 
    if request.method == "POST": 
     form = TweetSearchForm(request.POST) 
     if form.is_valid(): 
      cd = form.cleaned_data 
      request.session['cleaneddata']=cd 
      #print(cd['hashtags']) 
      return HttpResponseRedirect('/searched/') 
    else: 
     form = TweetSearchForm() 
    return render(request, 'tweet_search_form.html', {'form':form}) 
def searched(request): 
    search_data = request.session.get('cd') 
    hashtags = search_data['hashtags'] 
    user = search_data['user'] 
    since_date = search_data['since_date'] 
    before_date = search_data['before_date'] 

    return render(request, 'tweet_search_result_form.html', {'hashtags':hashtags, 'user':user, 'since_date':since_date, 'before_date':before_date}) 

最後に、urlconfを見つけてください。

enter code here 
"""tweetsearch URL Configuration 

The `urlpatterns` list routes URLs to views. For more information please see: 
    https://docs.djangoproject.com/en/1.11/topics/http/urls/ 
Examples: 
Function views 
    1. Add an import: from my_app import views 
    2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 
Class-based views 
    1. Add an import: from other_app.views import Home 
    2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 
Including another URLconf 
    1. Import the include() function: from django.conf.urls import url, include 
    2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 
""" 
from django.conf.urls import url 
from django.contrib import admin 
from tweetsearch.views import * 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^tweet_search/$', tweet_search), 
    url(r'^searched/$', searched) 

] 
+0

あなたのフォームは 'searched'に行くでしょう、私はあなたが' tweet_search'に行く必要があると思います。そのメソッドはフォームを扱っていますから –

+0

tweet_searchは検索フォームを扱うメソッドであり、 –

+0

それは正しいです、あなたのフォームの提出は 'tweet_search'に行かなければなりません –

答えて

0

フォームはPOSTを経由して/searched/に提出され、そしてあなたのurls.pyそのパターンにviews.pysearched方法にマッピングされています。ここまでは順調ですね。

しかし、searchedメソッドでは、そのフォームやPOSTメソッドを処理していません。あなたは行うことができます。

  1. があるので、あなたがPOSTでフォームを扱う、tweet_searchにフォームを送信:

    <form action="/tweet_search/" method="POST"> 
    
  2. は、あなたがtweet_searchで行うのと同じ方法で、searchedでフォームをハンドル。それが唯一のテンプレートレンダリングエンジンでセッションの要素を置くので、

また、あなたのsearchedハンドラは、言うまでもないです。

def tweet_search(request): 
    form = TweetSearchForm(request.POST or None) 
    if request.POST: 
     if form.is_valid(): 
      cd = form.cleaned_data 
      return render(
       request, 'tweet_search_result_form.html', {'cd': cd} 
      ) 
    return render(
     request, 'tweet_search_form.html', {'form': form} 
    ) 

cd辞書用tweet_search_result_form.html表情で:

{% if not (cd.hashtags and cd.user and before_date and since_date) %} 
    <p>You searched for tweets.</p> 
{% endif %} 
{# and so on... #} 

Djangoのデフォルトテンプレートエンジンに括弧を使用しないことに注意してくださいあなたは1つのハンドラ内でそれを行うことができます。

これが役に立ちます。

+0

「cd.hashtagsではなくcd.userではなく、cd.before_dateではなくcd.since_dateでない」ということはありますか? –

+0

私はそれがうまくいくと思います! –

+0

素晴らしい!うれしかった –

関連する問題