【问题标题】:unable to edit mongodb document in flask无法在烧瓶中编辑 mongodb 文档
【发布时间】:2020-10-13 18:05:59
【问题描述】:

我正在使用烧瓶和 pymongo 开展一个项目,其中我有一个电影收藏,我的电影收藏的一个实例如下:

movie = {"title":"Hobbit" , "year":"2013"}

我有一个 html jinja 2 页面,我在其中提交了一个表单,我可以更改电影的标题或年份。两者都不需要更改。

我的 html 模板:

    <h1> Select a field in the movie to update  </h1>
    <form action = "{{url_for('execute_movie_update')}}" method = "post">
       <label="update-title">Change the title</label>
       <input type = "text" id="new-title" name = "new-title" >
       <label="update-year"> Change year of publish  </label>  
       <input type = "text" id = "new-year" name = "new-year"> 
       <label="update-plot"> Change plot of movie  </label>
       <input type = "text" id = "new-plot" name = "new-plot">
       <button type = "submit">Submit changes</button> 
    </form>

如果我使用我将在下面包含的烧瓶端点来更改我在会话中拥有的电影的标题和年份,则更新成功。但是,如果我只更改一个字段

(ex. if I change the title only the year I have is set to " " instead of being the orginal year )

我的烧瓶端点在这里。它会更新我存储在会话中的特定电影:

@app.route('/executemovieupdate' , methods = ['GET', 'POST'])
def execute_movie_update():
    if 'Email' in session and 'User' in session:
        email = session['Email']
        user = session['User']
        if user == 'Admin':
            if 'Movie' in session and 'Movie_year' in session and 'Movie_plot' in session:
                movie = session['Movie']
                year = session['Movie_year']
                plot = session['Movie_plot']
                tainia = movies.find_one({'title':movie , "year":year}) #the movie that I want to update
                if request.method == 'POST':
                    new_title = request.form.get('new-title') #get the fields I submitted in html
                    new_year = request.form.get('new-year')

                    if new_title!=None: #if I submit only title year is set to " "
                        print("update the title")
                        movies.update_one({"title":movie , "year":year} , {"$set":{"title":new_title} } )
                        session['Movie'] = new_title
                        movie = session['Movie']
                        print(session)

                    if new_year != None: #if i submit both title and year the program works 
                        print("update the year")
                        movies.update_one({"title":movie , "year":year} , {"$set": {"year":new_year}}) 
                        session['Movie_year']=new_year
                        year = session['Movie_year']
                        print(session)

                  

  
                    return ''' movie has been updated '''
                                                                                                          
                else:
                    return render_template('movie-update.html' , movie = tainia)        
            else:
                return redirect(url_for('admin.html'))    
        else:
            return redirect(url_for('login')) 
    else:
        return redirect(url_for('login'))   

感谢您帮助指导我解决此问题。提前谢谢你。

编辑:似乎问题出在我的 if 语句中,因为即使未提交项目,我也总是同时输入它们(例如,我没有提交标题)

【问题讨论】:

    标签: python mongodb flask jinja2 pymongo


    【解决方案1】:

    已解决: 通过将标题 if 语句更改为:if new_title: 和 year 语句更改为 if new_year:

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多