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>

这是 forms.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})

最后,请找到网址。

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)
]

您的表单通过 POST 提交给/searched/,并且在您的urls.py中,该模式映射到views.py中的searched方法。目前为止,一切都好。

但是,在searched方法中,您不处理该表单,甚至不处理 POST 方法。你可以做:

  1. 将表单提交给tweet_search,因为您可以在 POST 中处理表单:

    <form action="/tweet_search/" method="POST">
    
  2. searched中处理表单,就像在tweet_search中一样。

此外,您的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}
)

tweet_search_result_form.html查找cd字典:

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

请注意,在 Django 的默认模板引擎中不使用括号。

希望这有帮助。

我在 html 页面上遇到了同样的问题,问题最终是我有两个

标签。提交也不起作用,当此 html 不正确且重复时。起初我没有看到它。我的故障排除方式是我删除了所有 html 并运行了提交按钮,它起作用了。然后我一次将 html 放回一个控件中,我看到了错误。我希望这对某人有所帮助。谢谢

我也有类似的问题。我有适当的<form></form>但由于HTML中存在轻微错误,浏览器(Firefox(选择删除</form>标签。 直到我做了一个视图源,我才意识到这一点。 如果不是你的线索,我永远不会去找那个,马克。

最新更新