Strange the global variable behavior

28 views
Skip to first unread message

Максим Королёв

unread,
Jan 23, 2018, 7:28:16 AM1/23/18
to Django users
Hi there.
Could someone explain what happens. I had a little project on Python 2.7 and decided to move it on 3.6. Everything has gone by perfectly. The project works well as before but there is one exception noticed by me: the global statement works not always. The code below doesn't update the global variable zip_file_name and I can't grasp why. Any suggestions.

zip_file_name = os.path.join(MEDIA_ROOT, 'wb`s.zip')
file_list = []


@permission_required("wb_search.change_wbsearch", login_url='access_denied')
@csrf_exempt
def input_data_form(request):
global zip_file_name, file_list
if request.method == 'POST':
form = WbSearchForm(request.POST)
if form.is_valid():
text = request.POST['wbs_nums']
picked = form.cleaned_data.get('category')
try:
wb_nums = process_input(text, LENGTH_OF_STR_TO_PROCESS)
zip_file_name, file_list = get_wb_pdf_as_zip(wb_nums, picked)
m = SearchHistory(user=str(request.user), curr_date=datetime.now(), list_desired_docs=', '.join([str(rec[0]) for rec in file_list]))
m.save()
return HttpResponseRedirect('/wb_search/search_finished/')
except NoDataToSearch:
return HttpResponseRedirect('/wb_search/wb_search_incorrect_data/')
else:
return HttpResponseRedirect('/wb_search/wb_search_incorrect_data/')
else:
form = WbSearchForm()
# context = context.update(csrf(request))
return render_to_response('wb_search_input_data_form.html',
{'form': form, 'title': 'Номера для поиска',
'header': 'Поиск PDF файлов вэйбилов',
'text': 'Вставте в это поле номера вэйбилов PDF файлы которых необходимо найти.',
'textarea_title': 'Вставте в это поле номера вэйбилов PDF файлы которых необходимо '
'найти.',
'submit_title': 'Начать поиск',
'element_id': 'find_button', 'element_method': 'click',
'spinner_message': 'Ожидайте окончания поиска...', },
RequestContext(request))

Максим Королёв

unread,
Jan 23, 2018, 7:28:16 AM1/23/18
to Django users
Hi there.
Could someone explain what happens. I had a little project on Python 2.7 and decided to move it on 3.6. Everything has gone by perfectly. The project works well as before but there is one exception noticed by me: the global statement works not always. The code below doesn't update the global variable zip_file_name and I can't grasp why. On Python 2.7 everything worked properly. Any suggestions

zip_file_name = ''

Matthew Pava

unread,
Jan 23, 2018, 9:37:01 AM1/23/18
to django...@googlegroups.com

I think the primary suggestion would be to not use global variables.  Is there any way you could re-implement the function without using those global variables?  Maybe creating a variable in the settings file for you to use?

If you can’t, is there possibly a problem with the get_wb_pdf_as_zip function?

 

As an aside, I also suggest using render(request, template_name, context_dict) instead of render_to_response().

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e833541c-0b75-486c-a06e-5aa8ccb5d446%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Costja Covtushenko

unread,
Jan 23, 2018, 7:52:43 PM1/23/18
to django...@googlegroups.com
Hi,

Sorry for asking but how exactly you decided that those `global` variables was not updated?

Regards,
C

Antonis Christofides

unread,
Jan 24, 2018, 2:29:43 AM1/24/18
to django...@googlegroups.com

Global variables might work with runserver (because it starts threads, not processes, but I'm not certain about that so please correct me), but they're unlikely to work in production, because typically Django is run as at least two WSGI processes.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com
Reply all
Reply to author
Forward
0 new messages