Django提交按钮没有响应

时间:2017-07-14 13:53:01

标签: python html 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})

最后,请找到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)

]

3 个答案:

答案 0 :(得分:1)

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

标签。当此html不正确并被复制时,提交也不起作用。起初我没有看到它。我如何解决问题是我删除了所有的html并运行了Submit按钮,并且它起作用了。然后我将html一次放回一个控件中,我看到了错误。我希望这可以帮助别人。谢谢

答案 1 :(得分:0)

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

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

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

    <form action="/tweet_search/" method="POST">
    
  2. 处理searched中的表单,方法与tweet_search相同。

  3. 此外,您的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的默认模板引擎中使用括号。

    希望这有帮助。

答案 2 :(得分:0)

我有类似的问题。我有正确的<form></form>,但是由于HTML中存在细微错误,因此浏览器(Firefox)选择删除</form>标签。直到我做了一个视图源,我才意识到这一点。如果没有您的帮忙,马克,我绝对不会想要的。

相关问题