如何将模板中用户输入的值传递给django中作为参数的函数?

时间:2017-04-12 08:37:33

标签: django forms django-forms django-templates django-views

我想取用户给出的输入值,即在这种情况下的评级,并希望调用指定的函数,但该函数不返回任何内容。所以我想在点击提交后留在同一页面。{{3 }}

这是详细信息页面的代码。

<h4>Hai this is the details page for the movies {{ plot }}</h4>
<form action="#" method="post">
     {% csrf_token %}
  Ratings: <input type="number" name="quantity" step="0.5" min="1" max="5"><br>
  <input type="submit" value="Click">
</form>

这是我需要通过传递用户给出的值来调用的函数,该函数不返回任何内容,而是将值存储到csv文件中。

def rate_movie(user_id , movie_id , rating_value):
    pc = ratings.movie_id[ratings.user_id == user_id]  # gets all the movies rated by this user
    ad = pc.tolist() #converts to list
    s = 0
    # This loop finds the exact location in the table to store the rating.
    for p in ad:
        if movie_id < p:
            s =  ad.index(p)
            break
    #Splits the table at this point.
    seperate = list(pc[pc == s].index)[0]
    #The row which will be added to the table.
    bla = [user_id,movie_id,rating_value,665345]
    #Splitting the table
    pq = ratings[:seperate+1]
    rs = ratings[seperate+1:len(ratings)]
    pq.loc[len(pq)] = bla
    pq.index = pq.index + 1
    pq = pq.append(rs , ignore_index=True)
    pq.to_csv("/home/iffu/Documents/ratingsss.csv",index = False)

0 个答案:

没有答案