Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让Django使用参数运行脚本?_Python_Django - Fatal编程技术网

Python 让Django使用参数运行脚本?

Python 让Django使用参数运行脚本?,python,django,Python,Django,我无法让Django运行带有参数的Tweepy脚本。当我启动服务器并创建实例时,我似乎能够让脚本运行print语句,但是我很难让它从HTML输入中接收标记 HTML按钮: <div class="col s12"> <div class="input-field inline"> <label for="User">Enter a term</label> <form action = "submit/

我无法让Django运行带有参数的Tweepy脚本。当我启动服务器并创建实例时,我似乎能够让脚本运行print语句,但是我很难让它从HTML输入中接收标记

HTML按钮:

<div class="col s12">
      <div class="input-field inline">
      <label for="User">Enter a term</label>
        <form action = "submit/" method = "post">
        {% csrf_token %}
       <input id="Country" type="text" name="Country" maxlength="100" required value="{{ tag }}" />
       <button class="btn waves-effect red" type="submit" name="action">Submit
        <i class="material-icons right">send</i>
      </button>
       </form>
      </div>
forms.py

from django import forms

#Search for the tag
class GetTweets(forms.Form):
    user = forms.CharField(label='User', max_length=30)
最后是(临时)tweepy脚本:

import tweepy
import time
from collections import defaultdict
import json

consumer_key = "??"
consumer_secret = "??"
access_key = "??"
access_secret = "??"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


def GetTweets(tag):
  placeholder = []
  for tweet in tweepy.Cursor(api.search, q=tag).items(20):
    placeholder.append(tweet.created_at)
    placeholder.append(tweet.text)
  #created_at
  print '   placeholder 0 \n'
  print placeholder[0] 
  print placeholder[1]
  print ' placeholder 1\n'
  print placeholder[2]
  print placeholder[3] 
网址


您必须将
tag
传递给模板,否则
{{tag}
将为空。更改您的
index()
视图(iirc,在Django的旧版本中,您需要在
模板中包装上下文dict。上下文(context)

更改您的html以发布到正确的url,例如:

<form action="submit/" method="post">
您的urls.py还需要与视图定义相匹配。如果您将视图定义为使用
标记
参数:

def Tweets(request, tag):
然后url.py需要捕获一个名为
标记的字段,例如:

url(r'^submit/(?P<tag>\w+)/$', views.Tweets),
并且您的视图应该在没有标记参数的情况下定义

def Tweets(request):
    if request.method == 'POST':
        country = request.POST['Country']  # from the name= in the html input element
        # call twitter
        # redirect to a response page
    # setup form
    # render GET version of page..

您必须将
标记
传递给模板,否则
{{tag}
将为空。更改
索引()
视图(iirc,在Django的旧版本中,您需要将上下文dict包装在
模板中。上下文(上下文)

更改您的html以发布到正确的url,例如:

<form action="submit/" method="post">
您的urls.py还需要与视图定义相匹配。如果您将视图定义为使用
标记
参数:

def Tweets(request, tag):
然后url.py需要捕获一个名为
标记的字段,例如:

url(r'^submit/(?P<tag>\w+)/$', views.Tweets),
并且您的视图应该在没有标记参数的情况下定义

def Tweets(request):
    if request.method == 'POST':
        country = request.POST['Country']  # from the name= in the html input element
        # call twitter
        # redirect to a response page
    # setup form
    # render GET version of page..

你的url.py在哪里?@thebjorn我把它们添加到了帖子中。我认为这与这些无关。
Tweets
视图没有连接到url(在url.py中)这意味着您无法通过单击按钮来执行它。html表单的
action
属性中的url必须与url.py中定义的url匹配才能映射到
视图。py:Tweet
Thank you@thebjorn,我已将更新url添加到此帖子。但是,它似乎还没有运行从html表单输入的任何标记。您在哪里是你的url.py吗?@thebjorn我把它们添加到了帖子中。我认为这与这些无关。
Tweets
视图没有连接到url(在url.py中)这意味着你无法通过点击按钮来执行它。html表单的
action
属性中的url必须与url.py中定义的url匹配才能映射到
视图。py:Tweet
谢谢@thebjorn,我已将更新url添加到此帖子中。但是,它似乎还没有运行从html表单输入的任何标记。也许吧s索引不需要上下文?它似乎只是将hello world文本放入我的输入框中。将HTML表单操作更改为提交/单击提交后仅更改我的URL(并在单击时继续执行此操作),我尝试将其重定向回索引,但无法获取。我不确定是否需要该url?我将在主帖子中再次更新我的代码。此外,我的live.py似乎没有静止运行,因为我在控制台中没有收到打印消息。我不明白你的问题是什么。如果你的
live.GetTweets
没有被调用,则必须可能是因为
if info.is\u valid():
不正确——可能是因为html没有用户字段(这是
info
指向的表单中唯一的字段?)也许索引不需要上下文?它似乎只是将hello world文本放入我的输入框中。将HTML表单操作更改为提交/只是在单击“提交”后更改我的URL(并在单击时继续执行此操作),我尝试将其重定向回索引,但无法获取。我不确定是否需要该url?我将在主帖子中再次更新我的代码。此外,我的live.py似乎没有静止运行,因为我在控制台中没有收到打印消息。我不明白你的问题是什么。如果你的
live.GetTweets
没有被调用,则必须可能是因为
if info.is\u valid():
不正确——可能是因为html没有用户字段(这是
info
指向的表单中唯一的字段?)
def Tweets(request):
    if request.method == 'POST':
        country = request.POST['Country']  # from the name= in the html input element
        # call twitter
        # redirect to a response page
    # setup form
    # render GET version of page..