У меня есть форма на HTML странице с input type="text"
, которую я заменяю на textarea
. Теперь форма больше не работает. Когда я пытаюсь отправить его, я получаю сообщение об ошибке «UnboundLocalError: локальная переменная« updated_details », на которую ссылаются до назначения», ссылаясь на мой код python (я вообще не изменил python).
Старая строка в HTML
<input type="text" name="comments" id="comments" placeholder="Write stuff here." style="height:150px"> </input>
Новая строка в HTML
<textarea name="comments" id="comments" placeholder="Write stuff here" </input>
</textarea>
Полная HTML форма
<form action = "/insert_vote" method = "post" onsubmit="">
<div id="vote-form" action = "/insert_vote" method = "post" onsubmit="">
<div class="smalltext">
{% for dict_item in vote_choices %}
<input type="radio" name="options" padding="10px" margin="10px" id="{{ dict_item['id'] }}"
value="{{ dict_item['id'] }}"> {{ dict_item['choice'] }} </input><br>
{% endfor %}
</div>
<br>
<div class="mediumlefttext">
Why did you make that choice?
</div>
<!-- <input type="text" name="comments" id="comments" placeholder="Write stuff here." style="height:150px"> </input> <br>-->
<textarea name="comments" id="comments" placeholder="Write stuff here" </input>
</textarea>
<!--<button onclick="javascript:login();" size="large" type="submit" value="Submit" scope="public_profile,email" returnscopes="true" onlogin="checkLoginState();">Submit</button>-->
<input type="text" name="user_id" id="user_id" style="display:none;">
<input type="submit" value="Submit">
</div>
</form>
Python
@app.route('/insert_vote', methods=['GET', 'POST'])
def insert_vote():
posted = 1
global article, user_id
print ("insert_vote", "this should be the facebook user id", user_id)
if request.method == 'POST' or request.method == 'GET':
if not request.form['options'] or request.form['comments']:
flash('Please enter all the fields', 'error')
else:
rate = 0 # rate of votes protection against no votes
vote_choice_id = int(request.form['options'])
comments = request.form['comments']
# user_id = request.form['user_id']
#user_id = 1
av_obj = ArticleVote(user_id, article.id, vote_choice_id, comments)
db.session.add(av_obj)
try:
db.session.commit()
except exc.SQLAlchemyError:
flash('User has already voted on this article.')
posted = 0
if posted == 1:
flash('Record was successfully added')
else:
db.session.rollback()
a_obj = article # this is the current global article
avs_obj = retrieve_article_vote_summary(a_obj.id) # vote_summary is a list of [tuples('True', numOfTrue), etc]
total_votes = avs_obj.getTotalVotes()
vote_choice_list = VoteChoice.getVoteChoiceList()
vote_choices = []
for item in vote_choice_list: # looping over VoteChoice objects
num = avs_obj.getVoteCount(item.choice)
if total_votes > 0:
rate = num / total_votes
vote_choices.append([item.choice, item.color, num, rate*100, total_votes])
details = avs_obj.getVoteDetails() # 10/02 - retrieve array of tuples [(user, VoteChoice, Comments)]
details_count = 0
for detail in details:
details_count += 1
return redirect('/results/' + str(article.id))
...
@app.route('/results/<int:id>')
def results(id):
rate = 0 # either 0 or num/total
article_list_of_one = Article.query.filter_by(id=id)
a_obj = article_list_of_one[0]
avs_obj = retrieve_article_vote_summary(a_obj.id) # vote_summary is a list of [tuples('True', numOfTrue), etc]
total_votes = avs_obj.getTotalVotes()
vote_choices = []
vote_choice_list = VoteChoice.getVoteChoiceList()
for item in vote_choice_list: # looping over VoteChoice objects
num = avs_obj.getVoteCount(item.choice)
if total_votes > 0: # protecting against no votes
rate = num/total_votes
vote_choices.append([item.choice, item.color, num, rate*100, total_votes])
details = avs_obj.getVoteDetails() # 10/02 - retrieve array of tuples [(user, VoteChoice, Comments)]
print("Inside results(" + str(id) + "):")
details_count = 0
for detail in details:
updated_details = [(user, VoteChoice, Comments, User.query.filter_by(name=user).first().fb_pic)
for (user, VoteChoice, Comments) in details]
#print(" " + str(details_count) + ": " + details[0] + " " + details[1] + " " + details[2])
# details_count += 1
return render_template('results.html', title=a_obj.title, id=id,
image_url=a_obj.image_url, url=a_obj.url,
vote_choices=vote_choices, home_data=Article.query.all(),
vote_details=updated_details)