複数のユーザー入力を受け取り、ユーザーがサブミットすると別のページに表示する簡単な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)
]
あなたのフォームは 'searched'に行くでしょう、私はあなたが' tweet_search'に行く必要があると思います。そのメソッドはフォームを扱っていますから –
tweet_searchは検索フォームを扱うメソッドであり、 –
それは正しいです、あなたのフォームの提出は 'tweet_search'に行かなければなりません –